Class: RubyText::Window::Menu2D

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

Defined Under Namespace

Classes: Vertical

Instance Method Summary collapse

Constructor Details

#initialize(win:, r: :center, c: :center, items:, colrow: [0, 0], border: true, title: nil, fg: Green, bg: Black) ⇒ Menu2D

Returns a new instance of Menu2D.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/menu.rb', line 24

def initialize(win:, r: :center, c: :center, items:, colrow: [0, 0], 
               border: true, title: nil, fg: Green, bg: Black)
  @win = win
  @list = []
  @header = @list.map {|x| x.header }
  items.each {|vlist| @list << Vertical.new(vlist) }
  @highest = @list.map {|x| x.height }.max
  @full_width = @list.inject(0) {|sum, vlist| sum += vlist.widest + 2 }
  @nlists = items.size
  @grid = Array.new(@nlists)  # column major order
  @grid.map! {|x| [" "] * @highest }
  @list.each.with_index do |vlist, i|  
    vlist.hash.each_pair.with_index do |kv, j|
      k, v = kv
      @grid[i][j] = [k, v]
    end
  end
  RubyText.hide_cursor
  @high = @highest
  @wide = @full_width
  @high += 2 if border
  @wide += 2 if border

  tlen = title.length + 8 rescue 0
  # wide = [wide, tlen].max
  row, col = @win.coords(r, c)
  row = row - @high/2 if r == :center
  col = col - @wide/2 if c == :center
  r, c = row, col
  @win.saveback(@high+1, @wide, r, c)
  mr, mc = r+@win.r0, c+@win.c0
  title = nil unless border

  @mwin = RubyText.window(@high+1, @wide, r: mr, c: mc, border: true,
                          fg: fg, bg: bg, title: title)
  @header.each {|head| printf "%-#{maxw}s", head }
  puts  # after header
  Curses.stdscr.keypad(true)
  maxcol = items.size - 1
  sizes = items.map {|x| x.size }
  max = sizes.max
  # mwin.go(r, c)
  r += 1   # account for header
  @selc, @selr = colrow
end

Instance Method Details

#handle(r, c) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/menu.rb', line 84

def handle(r, c)
  loop do
    show(r, c)
    ch = getch
    case ch
      when RubyText::Window::Up
        @selr -= 1 if @selr > 0
      when RubyText::Window::Down
#             puts "PAUSE r,c = #@selr #@selc  highest=#@highest"; getch
        @selr += 1 if @selr < @highest - 1
      when RubyText::Window::Left
        @selc -= 1 if @selc > 0
      when RubyText::Window::Right
        @selc += 1 if @selc < @full_width
      when RubyText::Window::Esc
        @win.restback(@high+1, @wide, r-1, c)
        RubyText.show_cursor
        return [nil, nil, nil]
      when RubyText::Window::Enter
        @win.restback(@high+1, @wide, r-1, c)
        RubyText.show_cursor
        choice = @grid[@selc][@selr][1]
        case choice
          when String;   
               puts "Returning #{[@selc, @selr, choice].inspect}"; getch
               return [@selc, @selr, choice]
          when NilClass; return [nil, nil, nil]
        end
        result = choice.call   # should be a Proc
        return [nil, nil, nil] if result.nil? || result.empty?
        return result
      else Curses.beep
    end
  end
  RubyText.show_cursor
end

#show(r, c, colrow: [0, 0]) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/menu.rb', line 70

def show(r, c, colrow: [0, 0])
  @selc, @selr = colrow
  @grid.each.with_index do |column, cix|
    column.each.with_index do |pairs, rix|   # {Jan: ..., Feb: ..., Mar: ..., ...}
      # STDSCR.puts "go: #{r}+#{rix}, #{c}+#{cix}*#{maxw}"
      @mwin.go(rix, cix)  # FIXME wrong?
      style = ([@selc, @selr] == [cix, rix]) ? :reverse : :normal
      key, val = pairs
      label = key.to_s
      @mwin.print label # fx(label, style)
    end
  end
end