Class: DialogTui::Dialog

Inherits:
Object
  • Object
show all
Includes:
UserAction
Defined in:
lib/dialog_tui.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UserAction

read_char, show_single_key, user_action

Constructor Details

#initialize(&block) ⇒ Dialog

Returns a new instance of Dialog.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dialog_tui.rb', line 39

def initialize &block
  @options = []

  if block.arity == 0
    instance_eval &block
  else
    block.call self
  end

  raise unless @options.count > 0
  # options list is assumed to be fixed now

  @current = @options.first
  @neighbors = Neighbors.new @options
end

Class Method Details

.run(&block) ⇒ Object



34
35
36
# File 'lib/dialog_tui.rb', line 34

def self.run &block
  new(&block).run
end

Instance Method Details

#chosen?(option) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/dialog_tui.rb', line 88

def chosen? option
  @current == option  # so current or chosen?)
end

#ctrl_c(&block) ⇒ Object



97
98
99
100
# File 'lib/dialog_tui.rb', line 97

def ctrl_c &block
  @ctrl_c ||= []
  @ctrl_c << block
end

#option(text, &reaction) ⇒ Object



92
93
94
95
# File 'lib/dialog_tui.rb', line 92

def option text, &reaction
  option = Option.new self, text, &reaction  # order?
  @options.push option
end

#runObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dialog_tui.rb', line 55

def run
  begin
    # print_usage  #...
    print_options  # would be nice to have a printer...

    done = false
    
    user_action { |key|

      key.up do
        @current = prev
      end

      key.down do
        @current = nexxt
      end

      #key.ctrl_c do  exit 0  end
      #key.esc do     exit 0  end

      key.enter do
        done = true
      end

      key.ctrl_c do
        @ctrl_c.each(&:call) if @ctrl_c
      end
    }
  end until done

  @current.call
end