Class: RubyText::Window
- Inherits:
-
Object
- Object
- RubyText::Window
- Includes:
- Keys
- Defined in:
- lib/menu.rb,
lib/menu.rb,
lib/color.rb,
lib/output.rb,
lib/window.rb,
lib/rubytext.rb,
lib/navigation.rb
Overview
Reopening: Coordinate handling (1-based!)
Defined Under Namespace
Constant Summary collapse
- ScreenStack =
[]
Constants included from Keys
Keys::BS, Keys::Back, Keys::CtlD, Keys::DEL, Keys::Del, Keys::Delete, Keys::Down, Keys::EOF, Keys::ESC, Keys::Enter, Keys::Esc, Keys::Escape, Keys::F1, Keys::F10, Keys::F11, Keys::F12, Keys::F2, Keys::F3, Keys::F4, Keys::F5, Keys::F6, Keys::F7, Keys::F8, Keys::F9, Keys::LF, Keys::Left, Keys::NL, Keys::Right, Keys::TAB, Keys::Tab, Keys::Up
Instance Attribute Summary collapse
-
#bg ⇒ Object
Returns the value of attribute bg.
-
#c0 ⇒ Object
readonly
Returns the value of attribute c0.
-
#cols ⇒ Object
readonly
Returns the value of attribute cols.
-
#cwin ⇒ Object
readonly
Returns the value of attribute cwin.
-
#fg ⇒ Object
Returns the value of attribute fg.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#r0 ⇒ Object
readonly
Returns the value of attribute r0.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
-
#scrolling(flag = true) ⇒ Object
readonly
FIXME refactor bad code.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
-
.clear(win) ⇒ Object
delete this?.
-
.colorize!(cwin, fg, bg) ⇒ Object
Set up a window with fg/bg.
- .main(fg: White, bg: Blue, scroll: false) ⇒ Object
-
.make(cwin, high, wide, r0, c0, border: true, fg: White, bg: Black, scroll: false) ⇒ Object
FIXME try again to inline this.
Instance Method Summary collapse
-
#[](r, c) ⇒ Object
def go(r0, c0) r, c = coords(r0, c0) save = self.rc goto r, c if block_given? yield goto *save end end.
- #[]=(r, c, char) ⇒ Object
- #_putch(ch) ⇒ Object
- #add_title(str, align = :center) ⇒ Object
- #background(high = STDSCR.rows, wide = STDSCR.cols, r = 0, c = 0) ⇒ Object
- #beep ⇒ Object
-
#bottom ⇒ Object
Move cursor to bottom of window.
- #boxme ⇒ Object
- #center(str) ⇒ Object
- #clear ⇒ Object
-
#colorize!(fg, bg) ⇒ Object
Set up a window with fg/bg.
-
#coords(r, c) ⇒ Object
Handle special coordinate names (symbols).
-
#crlf ⇒ Object
Technically not output…
- #delegate_output(sym, *args) ⇒ Object
-
#down(n = 1) ⇒ Object
Move cursor down.
-
#down! ⇒ Object
Move cursor to bottom of window.
-
#effect?(arg) ⇒ Boolean
FIXME Please refactor the Hal out of this.
- #flash ⇒ Object
- #gets(history: [], limit: nil, tab: [], default: "", capture: []) ⇒ Object
-
#go(r0, c0) ⇒ Object
Go to specified row/column in current window, execute block, and return cursor.
-
#goto(r, c) ⇒ Object
Go to specified row/column in current window.
-
#home ⇒ Object
Move cursor to home (upper left).
-
#initialize(high = nil, wide = nil, r0 = 0, c0 = 0, border = false, fg = White, bg = Blue, scroll = false) ⇒ Window
constructor
Better to use Window.window IRL.
-
#left(n = 1) ⇒ Object
Move cursor left.
-
#left! ⇒ Object
Move cursor to far left of window.
-
#menu(r: :center, c: :center, items:, curr: 0, border: true, sticky: false, title: nil, fg: Green, bg: Black, wrap: false) ⇒ Object
Simple menu with rows of strings (or Procs).
-
#multimenu(r: :center, c: :center, items:, curr: 0, selected: [], title: nil, sel_fg: Yellow, fg: White, bg: Blue) ⇒ Object
Menu for multiple selections (buggy/unused?).
- #output(&block) ⇒ Object
- #p(*args) ⇒ Object
- #print(*args) ⇒ Object
- #putch(ch, r: nil, c: nil, fx: nil) ⇒ Object
- #puts(*args) ⇒ Object
-
#radio_menu(r: :center, c: :center, items:, curr: 0, border: true, title: nil, fg: Green, bg: Black) ⇒ Object
Menu to choose a single setting and retain it.
-
#rc ⇒ Object
Return current row/column.
- #rcprint(r, c, *args) ⇒ Object
- #rcprint!(r, c, *args) ⇒ Object
- #rectmenu(r: :center, c: :center, items:, colrow: [0, 0], border: true, title: nil, fg: Green, bg: Black) ⇒ Object
- #refresh ⇒ Object
- #restback(high = STDSCR.rows, wide = STDSCR.cols, r = 0, c = 0) ⇒ Object
-
#right(n = 1) ⇒ Object
Move cursor right.
-
#right! ⇒ Object
Move cursor to far left of window.
- #saveback(high = STDSCR.rows, wide = STDSCR.cols, r = 0, c = 0) ⇒ Object
-
#screen_text(file = nil) ⇒ Object
rename?.
- #scroll(n = 1) ⇒ Object
-
#set_colors(fg, bg) ⇒ Object
Assign color pair to curses window.
-
#top ⇒ Object
Move cursor to top of window.
-
#topmenu(items:, curr: 0, fg: Green, bg: Black) ⇒ Object
One-line menu at top of window.
-
#up(n = 1) ⇒ Object
Move cursor up.
-
#up! ⇒ Object
Move cursor to top of window.
-
#yesno ⇒ Object
Simple yes/no decision.
Constructor Details
#initialize(high = nil, wide = nil, r0 = 0, c0 = 0, border = false, fg = White, bg = Blue, scroll = false) ⇒ Window
Better to use Window.window IRL
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/window.rb', line 24 def initialize(high=nil, wide=nil, r0=0, c0=0, border=false, fg=White, bg=Blue, scroll=false) @wide, @high, @r0, @c0 = wide, high, r0, c0 @border, @fg, @bg = border, fg, bg @cwin = Curses::Window.new(high, wide, r0, c0) colorize!(fg, bg) if @border @cwin.box(Vert, Horiz) @outer = @cwin @outer.refresh @cwin = Curses::Window.new(high-2, wide-2, r0+1, c0+1) colorize!(fg, bg) else @outer = @cwin end @rows, @cols = @cwin.maxy, @cwin.maxx # unnecessary really... @width, @height = @cols + 2, @rows + 2 if @border @scrolling = scroll @cwin.scrollok(scroll) @cwin.refresh end |
Instance Attribute Details
#bg ⇒ Object
Returns the value of attribute bg.
20 21 22 |
# File 'lib/window.rb', line 20 def bg @bg end |
#c0 ⇒ Object (readonly)
Returns the value of attribute c0.
19 20 21 |
# File 'lib/window.rb', line 19 def c0 @c0 end |
#cols ⇒ Object (readonly)
Returns the value of attribute cols.
18 19 20 |
# File 'lib/window.rb', line 18 def cols @cols end |
#cwin ⇒ Object (readonly)
Returns the value of attribute cwin.
18 19 20 |
# File 'lib/window.rb', line 18 def cwin @cwin end |
#fg ⇒ Object
Returns the value of attribute fg.
20 21 22 |
# File 'lib/window.rb', line 20 def fg @fg end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
18 19 20 |
# File 'lib/window.rb', line 18 def height @height end |
#r0 ⇒ Object (readonly)
Returns the value of attribute r0.
19 20 21 |
# File 'lib/window.rb', line 19 def r0 @r0 end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
18 19 20 |
# File 'lib/window.rb', line 18 def rows @rows end |
#scrolling(flag = true) ⇒ Object (readonly)
FIXME refactor bad code
91 92 93 |
# File 'lib/window.rb', line 91 def scrolling @scrolling end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
18 19 20 |
# File 'lib/window.rb', line 18 def width @width end |
Class Method Details
.clear(win) ⇒ Object
delete this?
103 104 105 106 107 108 109 |
# File 'lib/output.rb', line 103 def self.clear(win) # delete this? num = win.maxx * win.maxy - 1 win.setpos(0, 0) win.addstr(' '*num) win.setpos(0, 0) win.refresh end |
.colorize!(cwin, fg, bg) ⇒ Object
Set up a window with fg/bg
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/color.rb', line 45 def self.colorize!(cwin, fg, bg) cp = RubyText::Color.pair(fg, bg) cwin.color_set(cp) num = cwin.maxx * cwin.maxy cwin.setpos 0,0 cwin.addstr(' '*num) cwin.setpos 0,0 cwin.refresh rescue => err File.open("/tmp/#{__method__}.out", "w") do |f| f.puts err f.puts err.backtrace end end |
.main(fg: White, bg: Blue, scroll: false) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/window.rb', line 46 def self.main(fg: White, bg: Blue, scroll: false) debug "Starting #main..." main_win = Curses.init_screen Curses.start_color self.colorize!(main_win, fg, bg) rows, cols = main_win.maxy, main_win.maxx win = self.make(main_win, rows, cols, 0, 0, border: false, fg: fg, bg: bg, scroll: scroll) debug "...started #main" win rescue => err File.open("/tmp/main.out", "w") {|f| f.puts err.inspect; f.puts err.backtrace } end |
.make(cwin, high, wide, r0, c0, border: true, fg: White, bg: Black, scroll: false) ⇒ Object
FIXME try again to inline this
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/window.rb', line 62 def self.make(cwin, high, wide, r0, c0, border: true, fg: White, bg: Black, scroll: false) obj = self.allocate obj.instance_eval do @outer = @cwin = cwin @wide, @high, @r0, @c0 = wide, high, r0, c0 @fg, @bg = fg, bg @border = border @rows, @cols = high, wide @width, @height = @cols + 2, @rows + 2 if @border end obj.scrolling(scroll) obj end |
Instance Method Details
#[](r, c) ⇒ Object
def go(r0, c0)
r, c = coords(r0, c0)
save = self.rc
goto r, c
if block_given?
yield
goto *save
end
end
138 139 140 141 142 143 144 145 |
# File 'lib/output.rb', line 138 def [](r, c) r0, c0 = self.rc @cwin.setpos(r, c) ch = @cwin.inch @cwin.setpos(r0, c0) ch %= 256 ch.chr end |
#[]=(r, c, char) ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/output.rb', line 147 def []=(r, c, char) r0, c0 = self.rc @cwin.setpos(r, c) @cwin.addch(char[0].ord|Curses::A_NORMAL) @cwin.setpos(r0, c0) @cwin.refresh end |
#_putch(ch) ⇒ Object
63 64 65 |
# File 'lib/output.rb', line 63 def _putch(ch) @cwin.addch(ch) end |
#add_title(str, align = :center) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/window.rb', line 76 def add_title(str, align = :center) raise "No border" unless @border len = str.length # What if it's too long? start = case align when :left; 1 when :center; (@outer.maxx - len)/2 when :right; @outer.maxx - len - 1 end @outer.setpos 0, start @outer.addstr str @outer.refresh end |
#background(high = STDSCR.rows, wide = STDSCR.cols, r = 0, c = 0) ⇒ Object
124 125 126 127 128 |
# File 'lib/window.rb', line 124 def background(high=STDSCR.rows, wide=STDSCR.cols, r=0, c=0) saveback(high, wide, r, c) yield restback(high, wide, r, c) end |
#beep ⇒ Object
173 174 175 |
# File 'lib/window.rb', line 173 def beep Curses.beep end |
#bottom ⇒ Object
Move cursor to bottom of window
89 90 91 92 93 |
# File 'lib/navigation.rb', line 89 def bottom r, c = rc rmax = self.rows - 1 go rmax, c end |
#boxme ⇒ Object
155 156 157 158 |
# File 'lib/output.rb', line 155 def boxme @outer.box(Vert, Horiz) @outer.refresh end |
#center(str) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/output.rb', line 8 def center(str) r, c = self.rc n = @cwin.maxx - str.length go r, n/2 self.puts str end |
#clear ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/output.rb', line 111 def clear cwin.setpos(cwin.maxx, cwin.maxy) cwin.addstr(' ') num = cwin.maxx * cwin.maxy - 1 cwin.addstr(' '*num) cwin.setpos(0, 0) cwin.refresh end |
#colorize!(fg, bg) ⇒ Object
Set up a window with fg/bg
69 70 71 72 73 74 75 |
# File 'lib/color.rb', line 69 def colorize!(fg, bg) set_colors(fg, bg) num = @cwin.maxx * @cwin.maxy self.home self.go(0, 0) { @cwin.addstr(' '*num) } @cwin.refresh end |
#coords(r, c) ⇒ Object
Handle special coordinate names (symbols)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/navigation.rb', line 7 def coords(r, c) r = case when r == :center self.rows / 2 when r == :top 0 when r == :bottom self.rows - 1 else r end c = case when c == :center self.cols / 2 when c == :left 0 when c == :right self.cols - 1 else c end [r, c] end |
#crlf ⇒ Object
Technically not output…
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/output.rb', line 85 def crlf # Technically not output... r, c = rc if @scrolling if r == @rows - 1 # bottom row scroll left! else go r+1, 0 end else if r == @rows - 1 # bottom row left! else go r+1, 0 end end end |
#delegate_output(sym, *args) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/output.rb', line 21 def delegate_output(sym, *args) self.cwin.attrset(0) args = [""] if args.empty? args += ["\n"] if sym == :puts set_colors(@fg, @bg) meth = sym == :p ? :inspect : :to_s args.map! {|x| effect?(x) ? x : x.send(meth) } args.each do |arg| if arg.is_a?(RubyText::Effects) arg.set(self) else arg.effect.set(self) if arg.respond_to? :effect arg.each_char {|ch| ch == "\n" ? crlf : @cwin.addch(ch) } @cwin.refresh end end crlf if sym == :p # no implicit newline set_colors(@fg, @bg) @cwin.refresh end |
#down(n = 1) ⇒ Object
Move cursor down
61 62 63 64 |
# File 'lib/navigation.rb', line 61 def down(n=1) r, c = rc go r+n, c end |
#down! ⇒ Object
Move cursor to bottom of window
103 104 105 |
# File 'lib/navigation.rb', line 103 def down! bottom end |
#effect?(arg) ⇒ Boolean
FIXME Please refactor the Hal out of this.
17 18 19 |
# File 'lib/output.rb', line 17 def effect?(arg) arg.is_a?(RubyText::Effects) end |
#flash ⇒ Object
177 178 179 |
# File 'lib/window.rb', line 177 def flash Curses.flash end |
#gets(history: [], limit: nil, tab: [], default: "", capture: []) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/output.rb', line 267 def gets(history: [], limit: nil, tab: [], default: "", capture: []) # needs improvement # echo assumed to be OFF, keypad ON @history = history gs = GetString.new(self, default, history: history, limit: limit, tab: tab, capture: capture) count = 0 loop do count += 1 # Escape and 'capture' chars have special meaning if first char ch = self.getch case ch when *capture return ch if count == 1 gs.add(ch) when Escape return Escape if count == 1 gs.enter break when CtlD return CtlD if count == 1 gs.enter break when Enter gs.enter break when BS, DEL, 63 # backspace, del, ^H (huh?) gs.backspace when Tab gs.complete when Left gs.left_arrow when Right gs.right_arrow when Up next if @history.nil? # move this? gs.history_prev when Down next if @history.nil? # move this? gs.history_next when Integer Curses.beep else gs.add(ch) end end gs.value rescue => err str = err.to_s + "\n" + err.backtrace.join("\n") raise str end |
#go(r0, c0) ⇒ Object
Go to specified row/column in current window,
execute block, and return cursor
42 43 44 45 46 47 48 49 50 |
# File 'lib/navigation.rb', line 42 def go(r0, c0) r, c = coords(r0, c0) save = self.rc goto r, c if block_given? yield goto *save end end |
#goto(r, c) ⇒ Object
Go to specified row/column in current window
33 34 35 36 |
# File 'lib/navigation.rb', line 33 def goto(r, c) # only accepts numbers! @cwin.setpos(r, c) @cwin.refresh end |
#home ⇒ Object
Move cursor to home (upper left)
124 125 126 |
# File 'lib/navigation.rb', line 124 def home go 0, 0 end |
#left(n = 1) ⇒ Object
Move cursor left
68 69 70 71 |
# File 'lib/navigation.rb', line 68 def left(n=1) r, c = rc go r, c-n end |
#left! ⇒ Object
Move cursor to far left of window
109 110 111 112 |
# File 'lib/navigation.rb', line 109 def left! r, c = rc go r, 0 end |
#menu(r: :center, c: :center, items:, curr: 0, border: true, sticky: false, title: nil, fg: Green, bg: Black, wrap: false) ⇒ Object
Simple menu with rows of strings (or Procs)
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/menu.rb', line 383 def (r: :center, c: :center, items:, curr: 0, border: true, sticky: false, title: nil, fg: Green, bg: Black, wrap: false) RubyText.hide_cursor if items.is_a?(Hash) results = items.values items = items.keys hash_flag = true else results = items end high = items.size wide = items.map(&:length).max + 3 high += 2 if border wide += 2 if border 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 title = nil unless border mwin = RubyText.window(high, wide, r: mr, c: mc, border: border, fg: fg, bg: bg, title: title) Curses.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.to_s + " "*8)[0..wide-1] mwin.print fx(label, style) end ch = getch case ch when Up if sel > 0 sel -= 1 else sel = max if wrap # asteroids mode :) end when Down, " " # let space mean down? if sel < max sel += 1 else sel = 0 if wrap # asteroids mode :) end when Esc self.restback(high, wide, r, c) RubyText.show_cursor return [nil, nil] when Enter self.restback(high, wide, r, c) unless sticky RubyText.show_cursor choice = results[sel] return [sel, choice] if choice.is_a? String result = choice.call return [nil, nil] if result.nil? || result.empty? return result else Curses.beep end RubyText.show_cursor end end |
#multimenu(r: :center, c: :center, items:, curr: 0, selected: [], title: nil, sel_fg: Yellow, fg: White, bg: Blue) ⇒ Object
Menu for multiple selections (buggy/unused?)
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/menu.rb', line 456 def (r: :center, c: :center, items:, curr: 0, selected: [], title: nil, sel_fg: Yellow, 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, title: title) Curses.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 color = selected.include?(row) ? sel_fg : fg label = (" "*2 + item + " "*8)[0..wide-1] mwin.print fx(label, color, style) end ch = getch case ch when Up sel -= 1 if sel > 0 when Down sel += 1 if sel < max when Esc self.restback(high, wide, r, c) RubyText.show_cursor return [] when Enter self.restback(high, wide, r, c) RubyText.show_cursor return selected.map {|i| items[i] } when " " selected << sel sel += 1 if sel < max else Curses.beep end RubyText.show_cursor end end |
#output(&block) ⇒ Object
120 121 122 123 124 |
# File 'lib/output.rb', line 120 def output(&block) $stdscr = self block.call $stdscr = STDSCR end |
#p(*args) ⇒ Object
50 51 52 |
# File 'lib/output.rb', line 50 def p(*args) delegate_output(:p, *args) end |
#print(*args) ⇒ Object
46 47 48 |
# File 'lib/output.rb', line 46 def print(*args) delegate_output(:print, *args) end |
#putch(ch, r: nil, c: nil, fx: nil) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/output.rb', line 67 def putch(ch, r: nil, c: nil, fx: nil) debug("putch: #{[ch, r, c, fx].inspect}") if r.nil? && c.nil? && fx.nil? _putch(ch) else r0, c0 = self.rc r ||= r0 c ||= c0 go(r, c) do fx.set(self) if fx val = fx.value rescue 0 @cwin.addch(ch.ord|val) end fx.reset(self) if fx end @cwin.refresh end |
#puts(*args) ⇒ Object
42 43 44 |
# File 'lib/output.rb', line 42 def puts(*args) delegate_output(:puts, *args) end |
#radio_menu(r: :center, c: :center, items:, curr: 0, border: true, title: nil, fg: Green, bg: Black) ⇒ Object
Menu to choose a single setting and retain it
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
# File 'lib/menu.rb', line 518 def (r: :center, c: :center, items:, curr: 0, # Handle current value better? border: true, title: nil, fg: Green, bg: Black) RubyText.hide_cursor if items.is_a?(Hash) results = items.values items = items.keys hash_flag = true else results = items end high = items.size wide = items.map(&:length).max + 3 high += 2 if border wide += 2 if border 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 title = nil unless border mwin = RubyText.window(high, wide, r: mr, c: mc, border: border, fg: fg, bg: bg, title: title) Curses.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| mark = row == curr ? ">" : " " mwin.go row, 0 style = (sel == row) ? :reverse : :normal label = "#{mark} #{item}" mwin.print fx(label, style) end ch = getch case ch when Up sel -= 1 if sel > 0 when Down sel += 1 if sel < max when Esc self.restback(high, wide, r, c) RubyText.show_cursor return [nil, nil] when " " mwin[curr, 0] = " " mwin[sel, 0] = ">" curr = sel when Enter self.restback(high, wide, r, c) RubyText.show_cursor choice = results[sel] return [sel, choice] if choice.is_a? String result = choice.call return [nil, nil] if result.nil? || result.empty? return result else Curses.beep end RubyText.show_cursor end end |
#rc ⇒ Object
Return current row/column
130 131 132 |
# File 'lib/navigation.rb', line 130 def rc [@cwin.cury, @cwin.curx] end |
#rcprint(r, c, *args) ⇒ Object
54 55 56 |
# File 'lib/output.rb', line 54 def rcprint(r, c, *args) self.go(r, c) { self.print *args } end |
#rcprint!(r, c, *args) ⇒ Object
58 59 60 61 |
# File 'lib/output.rb', line 58 def rcprint!(r, c, *args) self.go(r, c) # Cursor isn't restored self.print *args end |
#rectmenu(r: :center, c: :center, items:, colrow: [0, 0], border: true, title: nil, fg: Green, bg: Black) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/menu.rb', line 123 def (r: :center, c: :center, items:, colrow: [0, 0], border: true, title: nil, fg: Green, bg: Black) RubyText.hide_cursor maxh, maxw = (items) header, stuff = _rect_hash2array(items, maxh, maxw) wide = items.size * maxw high = maxh 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 loop do RubyText.hide_cursor # FIXME should be unnecessary stuff.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+1, cix*maxw) style = ([selc, selr] == [cix, rix]) ? :reverse : :normal key, val = pairs label = key.to_s # mwin.print fx(label, style) mwin.print label # fx(label, style) end end ch = getch case ch when Up selr -= 1 if selr > 0 when Down selr += 1 if selr < maxh - 1 when Left selc -= 1 if selc > 0 when Right selc += 1 if selc < maxcol when Esc self.restback(high+1, wide, r-1, c) RubyText.show_cursor return [nil, nil, nil] when Enter self.restback(high+1, wide, r-1, c) RubyText.show_cursor choice = stuff[selc][selr][1] case choice when String; 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 RubyText.show_cursor end end |
#refresh ⇒ Object
160 161 162 |
# File 'lib/output.rb', line 160 def refresh @cwin.refresh end |
#restback(high = STDSCR.rows, wide = STDSCR.cols, r = 0, c = 0) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/window.rb', line 144 def restback(high=STDSCR.rows, wide=STDSCR.cols, r=0, c=0) save = ScreenStack.pop pos = save.shift 0.upto(high-1) do |h| line = "" 0.upto(wide-1) {|w| line << save.shift } row, col = h+r-1, c-1 row += 1 if self == STDSCR # wtf? col += 1 if self == STDSCR self.go row, col self.print line end self.go *pos @cwin.refresh rescue => err puts "Error!" puts err puts err.backtrace.join("\n") sleep 8 end |
#right(n = 1) ⇒ Object
Move cursor right
75 76 77 78 |
# File 'lib/navigation.rb', line 75 def right(n=1) r, c = rc go r, c+n end |
#right! ⇒ Object
Move cursor to far left of window
116 117 118 119 120 |
# File 'lib/navigation.rb', line 116 def right! r, c = rc cmax = self.cols - 1 go r, cmax end |
#saveback(high = STDSCR.rows, wide = STDSCR.cols, r = 0, c = 0) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/window.rb', line 130 def saveback(high=STDSCR.rows, wide=STDSCR.cols, r=0, c=0) debug "saveback: #{[high, wide, r, c].inspect}" save = [self.rc] 0.upto(high-1) do |h| 0.upto(wide-1) do |w| row, col = h+r-1, w+c-1 row += 1 if self == STDSCR # wtf? col += 1 if self == STDSCR save << self[row, col] end end ScreenStack.push save end |
#screen_text(file = nil) ⇒ Object
rename?
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/window.rb', line 111 def screen_text(file = nil) # rename? lines = [] 0.upto(self.rows-1) do |r| line = "" 0.upto(self.cols-1) do |c| line << self[r, c] end lines << line end File.open(file, "w") {|f| f.puts lines } if file lines end |
#scroll(n = 1) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/window.rb', line 96 def scroll(n=1) if n < 0 @cwin.scrl(n) (-n).times {|i| rcprint i, 0, (' '*@cols) } else n.times do |i| @cwin.scroll scrolling(false) rcprint @rows-1, 0, (' '*@cols) scrolling end end @cwin.refresh end |
#set_colors(fg, bg) ⇒ Object
Assign color pair to curses window
62 63 64 65 |
# File 'lib/color.rb', line 62 def set_colors(fg, bg) cp = RubyText::Color.pair(fg, bg) @cwin.color_set(cp) end |
#top ⇒ Object
Move cursor to top of window
82 83 84 85 |
# File 'lib/navigation.rb', line 82 def top r, c = rc go 0, c end |
#topmenu(items:, curr: 0, fg: Green, bg: Black) ⇒ Object
One-line menu at top of window
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/menu.rb', line 321 def (items:, curr: 0, fg: Green, bg: Black) r, c, high = 0, 0, 1 RubyText.hide_cursor hash_flag = false results = items if items.is_a?(Hash) results, items = items.values, items.keys hash_flag = true end width = 0 # total width cols = [] # start-column of each item items.each do |item| cols << width iwide = item.to_s.length + 2 width += iwide end r, c = self.coords(r, c) self.saveback(high, width, r, c) mr, mc = r+self.r0, c+self.c0 mwin = RubyText.window(high, width, r: mr, c: mc, fg: fg, bg: bg, border: false, title: nil) Curses.stdscr.keypad(true) sel = curr max = items.size - 1 loop do items.each.with_index do |item, num| item = " #{item} " mwin.go 0, cols[num] style = (sel == num) ? :reverse : :normal mwin.print fx(item, style) end ch = getch case ch when Left sel -= 1 if sel > 0 when Right sel += 1 if sel < max when Esc, " " # spacebar also quits self.restback(high, width, r, c) RubyText.show_cursor STDSCR.go r, c return [nil, nil] when Down, Enter self.restback(high, width, r, c) RubyText.show_cursor STDSCR.go r, c choice = results[sel] return [sel, choice] if choice.is_a? String result = choice.call return [nil, nil, nil] if result.nil? || result.empty? # next if result.nil? # next if result.empty? # return result else Curses.beep end RubyText.show_cursor end end |
#up(n = 1) ⇒ Object
Move cursor up
54 55 56 57 |
# File 'lib/navigation.rb', line 54 def up(n=1) r, c = rc go r-n, c end |
#up! ⇒ Object
Move cursor to top of window
97 98 99 |
# File 'lib/navigation.rb', line 97 def up! top end |
#yesno ⇒ Object
Simple yes/no decision
509 510 511 512 513 514 |
# File 'lib/menu.rb', line 509 def yesno # TODO: Accept YyNn r, c = STDSCR.rc num, str = STDSCR.(r: r, c: c+6, items: ["yes", "no"]) num == 0 end |