class Dialog

Public Class Methods

new(&block) click to toggle source
# 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
run(&block) click to toggle source
# File lib/dialog_tui.rb, line 34
def self.run &block
  new(&block).run
end

Public Instance Methods

before_draw(&block) click to toggle source
# File lib/dialog_tui.rb, line 103
def before_draw &block
  @before_draw ||= []
  @before_draw << block
end
before_draw!() click to toggle source
# File lib/dialog_tui.rb, line 107
def before_draw!
  @before_draw.each &:call if @before_draw
end
chosen?(option) click to toggle source
# File lib/dialog_tui.rb, line 89
def chosen? option
  @current == option  # so current or chosen?)
end
ctrl_c(&block) click to toggle source
# File lib/dialog_tui.rb, line 98
def ctrl_c &block
  @ctrl_c ||= []
  @ctrl_c << block
end
option(text, &reaction) click to toggle source
# File lib/dialog_tui.rb, line 93
def option text, &reaction
  option = Option.new self, text, &reaction  # order?
  @options.push option
end
run() click to toggle source
# File lib/dialog_tui.rb, line 55
def run
  begin
    # print_usage  #...
    before_draw!  # like controller method here...
    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

Private Instance Methods

nexxt() click to toggle source
# File lib/dialog_tui.rb, line 123
def nexxt
  @neighbors.next @current
end
prev() click to toggle source
# File lib/dialog_tui.rb, line 119
def prev
  @neighbors.prev @current
end
print_options() click to toggle source

third => to DRY I guess