Module: RubyText
- Defined in:
- lib/rubytext.rb,
lib/menu.rb,
lib/window.rb,
lib/widgets.rb,
lib/rubytext.rb,
lib/settings.rb,
lib/rubytext_version.rb
Overview
Skeleton… Can’t put classes at top because of #initalize
Defined Under Namespace
Modules: Keys Classes: Color, Effects, Settings, Window
Constant Summary collapse
- VERSION =
"0.1.15"- Path =
File.(File.join(File.dirname(__FILE__)))
Class Method Summary collapse
- .beep ⇒ Object
- .flash ⇒ Object
-
.hide_cursor ⇒ Object
remove later?.
-
.method_missing(name, *args) ⇒ Object
For passing through arbitrary method calls to the lower level…
- .selector(win: STDSCR, r: 0, c: 0, rows: 10, cols: 20, items:, fg: White, bg: Blue, win2:, callback:, enter: nil, quit: "q") ⇒ Object
-
.show_cursor ⇒ Object
remove later?.
- .show_cursor! ⇒ Object
-
.spinner(label: "", win: STDSCR, &block) ⇒ Object
TODO delay, etc.
- .splash(msg) ⇒ Object
-
.start(*args, log: "/tmp/rubytext.log", fg: White, bg: Blue, scroll: false) ⇒ Object
FIXME refactor save/restore, etc.
-
.started ⇒ Object
remove later.
- .started? ⇒ Boolean
- .stop ⇒ Object
- .ticker(row: STDSCR.rows-1, col: 0, width: STDSCR.cols, fg: White, bg: Blue, text:, delay: 0.1) ⇒ Object
-
.window(high, wide, r: nil, c: nil, border: true, fg: White, bg: Blue, scroll: false, title: nil) ⇒ Object
TODO add title:?.
Instance Method Summary collapse
Class Method Details
.beep ⇒ Object
79 80 81 |
# File 'lib/settings.rb', line 79 def self.beep Curses.beep end |
.flash ⇒ Object
83 84 85 |
# File 'lib/settings.rb', line 83 def self.flash Curses.flash end |
.hide_cursor ⇒ Object
remove later?
138 139 140 |
# File 'lib/settings.rb', line 138 def self.hide_cursor # remove later? Curses.curs_set(0) end |
.method_missing(name, *args) ⇒ Object
For passing through arbitrary method calls to the lower level…
129 130 131 132 133 134 135 136 |
# File 'lib/settings.rb', line 129 def self.method_missing(name, *args) debug "method_missing: #{name} #{args.inspect}" if name[0] == '_' Curses.send(name[1..-1], *args) else raise "#{name} #{args.inspect}" # NoMethodError end end |
.selector(win: STDSCR, r: 0, c: 0, rows: 10, cols: 20, items:, fg: White, bg: Blue, win2:, callback:, enter: nil, quit: "q") ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 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 |
# File 'lib/menu.rb', line 102 def self.selector(win: STDSCR, r: 0, c: 0, rows: 10, cols: 20, items:, fg: White, bg: Blue, win2:, callback:, enter: nil, quit: "q") high = rows wide = cols mwin = RubyText.window(high, wide, r: r, c: c, fg: fg, bg: bg) handler = callback Curses.stdscr.keypad(true) RubyText.hide_cursor sel = 0 max = items.size - 1 handler.call(sel, items[sel], win2) loop do mwin.home items.each.with_index do |item, row| mwin.crlf style = (sel == row) ? :reverse : :normal mwin.print fx(" #{item}", style) end ch = getch case ch when Curses::KEY_UP if sel > 0 sel -= 1 handler.call(sel, items[sel], win2) end when Curses::KEY_DOWN if sel < max sel += 1 handler.call(sel, items[sel], win2) end when 10 # Enter if enter del = enter.call(sel, items[sel], win2) if del items -= [items[sel]] raise end end when 9 # tab Curses.flash when quit # parameter exit else Curses.beep # all else is trash end end rescue retry end |
.show_cursor ⇒ Object
remove later?
142 143 144 |
# File 'lib/settings.rb', line 142 def self.show_cursor # remove later? Curses.curs_set(1) end |
.show_cursor! ⇒ Object
146 147 148 |
# File 'lib/settings.rb', line 146 def self.show_cursor! Curses.curs_set(2) # Doesn't work? Device-dependent? end |
.spinner(label: "", win: STDSCR, &block) ⇒ Object
TODO delay, etc.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/widgets.rb', line 16 def self.spinner(label: "", win: STDSCR, &block) # TODO delay, etc. chars = "-\\|/" RubyText.hide_cursor t0 = Time.now.to_i thread = Thread.new do i=0 loop do t1 = Time.now.to_i elapsed = "0:%02d" % (t1-t0) # FIXME breaks at 60 sec i = (i+1) % 4 win.print " #{label} #{chars[i]} #{elapsed}" win.left! sleep 0.04 end end ret = block.call win.puts Thread.kill(thread) RubyText.show_cursor ret end |
.splash(msg) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/widgets.rb', line 38 def self.splash(msg) lines = msg.split("\n") high = lines.size + 2 wide = lines.map {|x| x.length }.max + 2 STDSCR.saveback(high, wide, 10, 20) win = RubyText.window(high, wide, r: 10, c: 20, fg: White, bg: Red) win.puts msg getch STDSCR.restback(high, wide, 10, 20) end |
.start(*args, log: "/tmp/rubytext.log", fg: White, bg: Blue, scroll: false) ⇒ Object
FIXME refactor save/restore, etc. - rep as binary vector?
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/settings.rb', line 91 def self.start(*args, log: "/tmp/rubytext.log", fg: White, bg: Blue, scroll: false) $debug ||= File.new(log, "w") if log # FIXME remove global args.each {|arg| raise "#{arg} is not valid" unless Settings::ValidArgs.include?(arg) } raise RTError("#{fg} is not a color") unless ::Colors.include? fg raise RTError("#{bg} is not a color") unless ::Colors.include? bg @settings = Settings.new @settings.set(*args) # override defaults main = RubyText::Window.main(fg: fg, bg: bg, scroll: scroll) Object.const_set(:STDSCR, main) unless defined? STDSCR $stdscr = STDSCR # FIXME global needed? Object.include(WindowIO) @started = true # rescue => err # puts(err.inspect) # puts(err.backtrace) # raise RTError("#{err}") end |
.started ⇒ Object
remove later
71 72 73 |
# File 'lib/settings.rb', line 71 def self.started # remove later @started end |
.started? ⇒ Boolean
75 76 77 |
# File 'lib/settings.rb', line 75 def self.started? @started end |
.stop ⇒ Object
113 114 115 116 |
# File 'lib/settings.rb', line 113 def self.stop @started = false Curses.close_screen end |
.ticker(row: STDSCR.rows-1, col: 0, width: STDSCR.cols, fg: White, bg: Blue, text:, delay: 0.1) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/widgets.rb', line 2 def self.ticker(row: STDSCR.rows-1, col: 0, width: STDSCR.cols, fg: White, bg: Blue, text:, delay: 0.1) text = text.gsub("\n", " ") + " " win = RubyText.window(1, width, r: row, c: col, border: false, fg: fg, bg: bg) leader = " "*width + text leader = text.chars.cycle.each_cons(width) width.times { win.rcprint 0, 0, leader.next.join } repeat = text.chars.cycle.each_cons(width) loop do # Warning: loops forever win.rcprint 0, 0, repeat.next.join sleep delay end end |
.window(high, wide, r: nil, c: nil, border: true, fg: White, bg: Blue, scroll: false, title: nil) ⇒ Object
TODO add title:?
4 5 6 7 8 9 10 11 |
# File 'lib/window.rb', line 4 def self.window(high, wide, r: nil, c: nil, border: true, fg: White, bg: Blue, scroll: false, title: nil) r ||= (STDSCR.rows - high)/2 c ||= (STDSCR.cols - wide)/2 win = RubyText::Window.new(high, wide, r, c, border, fg, bg, scroll) win.add_title(title) if title win end |
Instance Method Details
#reset ⇒ Object
122 123 124 |
# File 'lib/settings.rb', line 122 def reset @settings.reset end |
#set(*args) ⇒ Object
118 119 120 |
# File 'lib/settings.rb', line 118 def set(*args) @settings.set(*args) end |