Module: RubyText
- Defined in:
- lib/rubytext.rb,
lib/menu.rb,
lib/version.rb,
lib/widgets.rb,
lib/rubytext.rb,
lib/settings.rb
Overview
Skeleton… Can’t put classes at top because of #initalize
Defined Under Namespace
Modules: Keys Classes: Color, Effects, Window
Constant Summary collapse
- VERSION =
"0.0.93"- Path =
File.(File.join(File.dirname(__FILE__)))
- ValidArgs =
Hmm, all these are module-level…?
[:raw, :_raw, :echo, :_echo, :cbreak, :_cbreak, :keypad, :_keypad, :cursor, :_cursor]
Class Method Summary collapse
- .flags ⇒ Object
- .hide_cursor ⇒ Object
-
.inverse_flag(flag) ⇒ Object
FIXME Refactor the Hal out of this.
-
.method_missing(name, *args) ⇒ Object
For passing through arbitrary method calls to the lower level…
- .reset ⇒ Object
- .rest_flags ⇒ Object
- .save_flags ⇒ Object
- .selector(win: STDSCR, r: 0, c: 0, rows: 10, cols: 20, items:, fg: White, bg: Blue, win2:, callback:, enter: nil, quit: "q") ⇒ Object
-
.set(*args) ⇒ Object
Allow a block?.
- .show_cursor ⇒ Object
- .show_cursor! ⇒ Object
- .start(*args, log: "/tmp/rubytext.log", fg: White, bg: Blue, scroll: false) ⇒ Object
- .started ⇒ Object
- .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) ⇒ Object
Why did I put this here?.
Class Method Details
.flags ⇒ Object
34 35 36 37 |
# File 'lib/settings.rb', line 34 def self.flags @flags.uniq! @flags end |
.hide_cursor ⇒ Object
134 135 136 |
# File 'lib/settings.rb', line 134 def self.hide_cursor X.curs_set(0) end |
.inverse_flag(flag) ⇒ Object
FIXME Refactor the Hal out of this.
41 42 43 44 45 46 47 48 |
# File 'lib/settings.rb', line 41 def self.inverse_flag(flag) sflag = flag.to_s if sflag[0] == "_" sflag[1..-1].to_sym else ("_" + sflag).to_sym end end |
.method_missing(name, *args) ⇒ Object
For passing through arbitrary method calls to the lower level…
116 117 118 119 120 121 122 123 |
# File 'lib/settings.rb', line 116 def self.method_missing(name, *args) debug "method_missing: #{name} #{args.inspect}" if name[0] == '_' X.send(name[1..-1], *args) else raise "#{name} #{args.inspect}" # NoMethodError end end |
.reset ⇒ Object
90 91 92 |
# File 'lib/settings.rb', line 90 def self.reset rest_flags end |
.rest_flags ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/settings.rb', line 100 def self.rest_flags @flags = @fstack.pop @flags.uniq! self.set(*@flags) rescue @flags = @defaults end |
.save_flags ⇒ Object
94 95 96 97 98 |
# File 'lib/settings.rb', line 94 def self.save_flags @fstack ||= [] @flags.uniq! @fstack.push @flags end |
.selector(win: STDSCR, r: 0, c: 0, rows: 10, cols: 20, items:, fg: White, bg: Blue, win2:, callback:, enter: nil, quit: "q") ⇒ Object
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 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/menu.rb', line 59 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.indow(high, wide, r: r, c: c, fg: fg, bg: bg) handler = callback X.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 X::KEY_UP if sel > 0 sel -= 1 handler.call(sel, items[sel], win2) end when X::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 quit # parameter exit end end rescue retry end |
.set(*args) ⇒ Object
Allow a block?
50 51 52 53 54 55 56 57 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 |
# File 'lib/settings.rb', line 50 def self.set(*args) # Allow a block? standard = [:cbreak, :raw, :echo] @defaults = [:cbreak, :_echo, :keypad] @flags = @defaults.dup save_flags args.each do |arg| @flags += [arg] inv = inverse_flag(arg) @flags -= [inv] @flags.uniq! flag = arg.to_s if standard.include?(flag.to_sym) || standard.include?(flag.sub(/no/, "_").to_sym) X.send(flag) elsif flag[0] == "_" && standard.include?(flag[1..-1].to_sym) flag.sub!(/^_/, "no") X.send(flag) else case flag.to_sym when :cursor X.curs_set(1) when :_cursor, :nocursor X.curs_set(0) when :keypad STDSCR.cwin.keypad(true) when :_keypad STDSCR.cwin.keypad(false) else # self.stop rest_flags # prevent propagating error in test raise RTError("flag = #{flag.inspect}") end end end if block_given? yield rest_flags end end |
.show_cursor ⇒ Object
138 139 140 |
# File 'lib/settings.rb', line 138 def self.show_cursor X.curs_set(1) end |
.show_cursor! ⇒ Object
142 143 144 |
# File 'lib/settings.rb', line 142 def self.show_cursor! X.curs_set(2) # Doesn't work? Device-dependent? end |
.start(*args, log: "/tmp/rubytext.log", fg: White, bg: Blue, scroll: false) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/settings.rb', line 13 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 ValidArgs.include?(arg) } raise "#{fg} is not a color" unless ::Colors.include? fg raise "#{bg} is not a color" unless ::Colors.include? bg 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) self.set(:_echo, :cbreak, :keypad) # defaults self.set(*args) # override defaults @started = true rescue => err debug(err.inspect) debug(err.backtrace) raise RTError("#{err}") end |
.started ⇒ Object
7 8 9 |
# File 'lib/settings.rb', line 7 def self.started @started end |
.stop ⇒ Object
108 109 110 111 |
# File 'lib/settings.rb', line 108 def self.stop @started = false X.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) ⇒ Object
Why did I put this here?
127 128 129 130 131 132 |
# File 'lib/settings.rb', line 127 def self.window(high, wide, r: nil, c: nil, border: true, fg: White, bg: Blue, scroll: false) r ||= (STDSCR.rows - high)/2 c ||= (STDSCR.cols - wide)/2 RubyText::Window.new(high, wide, r, c, border, fg, bg, scroll) end |