class Diakonos::InteractionHandler

Constants

CHOICE_KEYS
CHOICE_STRINGS

Public Class Methods

new( win_main:, win_interaction:, cursor_manager:, testing: false, choice_delay: 3, blink_string: "*" * 60, blink_duration: 0.05 ) click to toggle source

TODO: Move win_interaction creation, etc. into this class.

If necessary, expose it with attr_reader
e.g. for @modes[ 'input' ].window = @win_interaction
# File lib/diakonos/interaction-handler.rb, line 53
def initialize(
  win_main:,
  win_interaction:,
  cursor_manager:,
  testing: false,
  choice_delay: 3,
  blink_string: "*" * 60,
  blink_duration: 0.05
)
  @win_main = win_main
  @win_interaction = win_interaction
  @cursor_manager = cursor_manager
  @testing = testing

  @choice_iterations = 0
  @choice_delay = choice_delay
  @blink_string = blink_string
  @blink_duration = blink_duration
end

Public Instance Methods

get_char(prompt) click to toggle source
# File lib/diakonos/interaction-handler.rb, line 93
def get_char(prompt)
  set_iline prompt
  char = @win_main.getch
  set_iline
  char
end
get_choice( prompt, choices, default = nil ) click to toggle source

choices should be an array of CHOICE_* constants. default is what is returned when Enter is pressed.

# File lib/diakonos/interaction-handler.rb, line 102
def get_choice( prompt, choices, default = nil )
  retval = @iterated_choice
  if retval
    @choice_iterations -= 1
    if @choice_iterations < 1
      @iterated_choice = nil
      $diakonos.do_display = true
    end
    return retval
  end

  @saved_main_x = @win_main.curx
  @saved_main_y = @win_main.cury

  msg = prompt + " "
  choice_strings = choices.collect do |choice|
    CHOICE_STRINGS[ choice ]
  end
  msg << choice_strings.join( ", " )

  if default
    set_iline msg
  else
    show_message msg
  end

  while retval.nil?
    ch = @win_interaction.getch
    if ch
      c = ch.ord
    else
      next
    end

    case c
    when Curses::KEY_NPAGE
      @cursor_manager.page_down
    when Curses::KEY_PPAGE
      @cursor_manager.page_up
    else
      if @message_expiry && Time.now < @message_expiry
        interaction_blink
        show_message msg
      else
        case c
        when ENTER
          retval = default
        when '0'.ord..'9'.ord
          if @choice_iterations < 1
            @choice_iterations = ( c - '0'.ord )
          else
            @choice_iterations = @choice_iterations * 10 + ( c - '0'.ord )
          end
        else
          choices.each do |choice|
            if CHOICE_KEYS[ choice ].include? c
              retval = choice
              break
            end
          end
        end

        if retval.nil?
          interaction_blink( msg )
        end
      end
    end
  end

  terminate_message
  set_iline

  if @choice_iterations > 0
    @choice_iterations -= 1
    @iterated_choice = retval
    $diakonos.do_display = false
  end

  retval
end
set_iline(string = "") click to toggle source

Display text on the interaction line.

# File lib/diakonos/interaction-handler.rb, line 74
def set_iline(string = "")
  return 0  if @testing
  return 0  if $diakonos.readline

  @iline = string
  Curses::curs_set 0
  @win_interaction.setpos( 0, 0 )
  @win_interaction.addstr( "%-#{Curses::cols}s" % @iline )
  @win_interaction.refresh
  Curses::curs_set 1
  string.length
end
set_iline_if_empty(string) click to toggle source
# File lib/diakonos/interaction-handler.rb, line 87
def set_iline_if_empty(string)
  if @iline.nil? || @iline.empty?
    set_iline string
  end
end

Private Instance Methods

show_message( message, non_interaction_duration = @choice_delay ) click to toggle source
# File lib/diakonos/interaction-handler.rb, line 185
def show_message( message, non_interaction_duration = @choice_delay )
  terminate_message

  @message_expiry = Time.now + non_interaction_duration
  @message_thread = Thread.new do
    time_left = @message_expiry - Time.now
    while time_left > 0
      set_iline "(#{time_left.round}) #{message}"
      @win_main.setpos( @saved_main_y, @saved_main_x )
      sleep 1
      time_left = @message_expiry - Time.now
    end
    set_iline message
    @win_main.setpos( @saved_main_y, @saved_main_x )
  end
end
terminate_message() click to toggle source
# File lib/diakonos/interaction-handler.rb, line 202
def terminate_message
  if @message_thread && @message_thread.alive?
    @message_thread.terminate
    @message_thread = nil
  end
end