Class: Umbra::Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/umbra/menu.rb

Instance Method Summary collapse

Constructor Details

#initialize(title, hash, config = {}) ⇒ Menu

Returns a new instance of Menu.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/umbra/menu.rb', line 29

def initialize title, hash, config={}

  @list = hash.values
  @keys = hash.keys.collect { |x| x.to_s }
  @hash = hash
  bkgd = config[:bkgd] || FFI::NCurses.COLOR_PAIR(14) | BOLD
  @attr = BOLD
  @color_pair = config[:color_pair] || 14
  ht = @list.size+2
  wid = config[:width] || 40
  top = (FFI::NCurses.LINES - ht)/2
  left = (FFI::NCurses.COLS - wid)/2
  @window = Window.new(ht, wid, top, left)
  @window.wbkgd(bkgd)
  @window.box
  @window.title(title)
  @current = 0
  print_items @hash
end

Instance Method Details

#getkeyObject



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
87
88
89
90
91
# File 'lib/umbra/menu.rb', line 58

def getkey
  ch = 0
  char = nil
  begin
    while (ch = @window.getkey) != FFI::NCurses::KEY_CTRL_C
      break if ch == 27 # ESC
      tmpchar = FFI::NCurses.keyname(ch) rescue '?'
      if @keys.include? tmpchar
        $log.debug "  menu #{tmpchar.class}:#{tmpchar} "
        char = ch.chr
        $log.debug "  menu #{ch.class}:#{char} "
        #char = tmpchar
        break
      end
      case ch
      when FFI::NCurses::KEY_DOWN
        @current += 1
      when FFI::NCurses::KEY_UP
        @current -= 1
      when FFI::NCurses::KEY_RETURN
        char = @keys[@current]
        break
      end
      @current = 0 if @current < 0
      @current = @list.size-1 if @current >= @list.size
      print_items @hash

      # trap arrow keys here
    end
  ensure
    @window.destroy
  end
  return char
end


48
49
50
51
52
53
54
55
56
57
# File 'lib/umbra/menu.rb', line 48

def print_items hash
  ix = 0
  hash.each_pair {|k, val|
    attr = @attr
    attr = REVERSE if ix == @current
    @window.printstring(ix+1 , 2, "#{k}    #{val}", @color_pair, attr )
    ix += 1
  }
  @window.refresh
end