lib/menu.rb in rubytext-0.0.89 vs lib/menu.rb in rubytext-0.0.90
- old
+ new
@@ -1,60 +1,61 @@
module RubyText
- def self.menu(win: STDSCR, r: :center, c: :center, items:, curr: 0,
- title: nil, fg: White, bg: Blue)
- RubyText.hide_cursor
- high = items.size + 2
- wide = items.map(&:length).max + 4
- tlen = title.length + 8
- wide = [wide, tlen].max
- r, c = win.coords(r, c)
- win.saveback(high, wide, r, c)
- mr, mc = r+win.r0, c+win.c0
- debug "menu: rc = #{[r,c].inspect} win r0c0 = #{[win.r0, win.c0].inspect}"
-# debug " and mr,mc = #{[mr,mc].inspect}"
- mwin = RubyText.window(high, wide, r: mr, c: mc,
- fg: fg, bg: bg)
+ class Window
+ def menu(r: :center, c: :center, items:, curr: 0,
+ title: nil, fg: White, bg: Blue)
+ RubyText.hide_cursor
+ high = items.size + 2
+ wide = items.map(&:length).max + 5
+ tlen = title.length + 8 rescue 0
+ wide = [wide, tlen].max
+ row, col = self.coords(r, c)
+ row = row - high/2 if r == :center
+ col = col - wide/2 if c == :center
+ r, c = row, col
+ self.saveback(high, wide, r, c)
+ mr, mc = r+self.r0, c+self.c0
+ mwin = RubyText.window(high, wide, r: mr, c: mc,
+ fg: fg, bg: bg)
- where = win == STDSCR ? [r, c+1] : [r-1, c+1] # wtf?
-
- unless title.nil?
- win.go(*where) do # same row as corner but farther right
- win.print fx("[ #{title} ]", :bold, fg, bg: bg)
+ where = self == STDSCR ? [r, c+1] : [r-1, c+1] # wtf?
+ unless title.nil?
+ self.go(*where) do # same row as corner but farther right
+ self.print fx("[ #{title} ]", :bold, fg, bg: bg)
+ end
end
- end
- X.stdscr.keypad(true)
- sel = curr
- max = items.size - 1
- norm = RubyText::Effects.new(:normal)
- rev = RubyText::Effects.new(:reverse)
- loop do
- RubyText.hide_cursor # FIXME should be unnecessary
- items.each.with_index do |item, row|
- mwin.go row, 0
- style = (sel == row) ? :reverse : :normal
- label = (" "*3 + item + " "*8)[0..wide-1]
- mwin.print fx(label, style)
+ X.stdscr.keypad(true)
+ sel = curr
+ max = items.size - 1
+ loop do
+ RubyText.hide_cursor # FIXME should be unnecessary
+ items.each.with_index do |item, row|
+ mwin.go row, 0
+ style = (sel == row) ? :reverse : :normal
+ label = (" "*2 + item + " "*8)[0..wide-1]
+ mwin.print fx(label, style)
+ end
+ ch = getch
+ case ch
+ when X::KEY_UP
+ sel -= 1 if sel > 0
+ when X::KEY_DOWN
+ sel += 1 if sel < max
+ when 27
+ self.restback(high, wide, r, c)
+ RubyText.show_cursor
+ return [nil, nil]
+ when 10
+ self.restback(high, wide, r, c)
+ RubyText.show_cursor
+ return [sel, items[sel]]
+ end
+ RubyText.show_cursor
end
- ch = getch
- case ch
- when X::KEY_UP
- sel -= 1 if sel > 0
- when X::KEY_DOWN
- sel += 1 if sel < max
- when 27
- win.restback(high, wide, r, c)
- RubyText.show_cursor
- return [nil, nil]
- when 10
- win.restback(high, wide, r, c)
- RubyText.show_cursor
- return [sel, items[sel]]
- end
- RubyText.show_cursor
end
+
end
def self.selector(win: STDSCR, r: 0, c: 0, rows: 10, cols: 20,
items:, fg: White, bg: Blue,
win2:, callback:, enter: nil, quit: "q")
@@ -62,11 +63,9 @@
wide = cols
mwin = RubyText.indow(high, wide, r: r, c: c, fg: fg, bg: bg)
handler = callback
X.stdscr.keypad(true)
RubyText.hide_cursor
- norm = RubyText::Effects.new(:normal)
- rev = RubyText::Effects.new(:reverse)
sel = 0
max = items.size - 1
handler.call(sel, items[sel], win2)
loop do
mwin.home