Class: RubyText::Window
- Inherits:
-
Object
- Object
- RubyText::Window
- Defined in:
- lib/menu.rb,
lib/color.rb,
lib/output.rb,
lib/window.rb,
lib/rubytext.rb,
lib/navigation.rb
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
- .main(fg: White, bg: Blue, scroll: false) ⇒ Object
- .make(cwin, high, wide, r0, c0, border: true, fg: White, bg: Black, scroll: false) ⇒ Object
Instance Method Summary collapse
- #[](r, c) ⇒ Object
- #[]=(r, c, char) ⇒ Object
- #_putch(ch) ⇒ Object
- #bottom ⇒ Object
- #boxme ⇒ Object
- #center(str) ⇒ Object
- #clear ⇒ Object
- #colorize!(fg, bg) ⇒ Object
- #coords(r, c) ⇒ Object
-
#crlf ⇒ Object
Technically not output…
- #delegate_output(sym, *args) ⇒ Object
- #down(n = 1) ⇒ Object
- #down! ⇒ Object
-
#effect?(arg) ⇒ Boolean
FIXME Please refactor the Hal out of this.
-
#gets ⇒ Object
still needs improvement.
- #go(r0, c0) ⇒ Object
-
#goto(r, c) ⇒ Object
only accepts numbers!.
- #home ⇒ Object
-
#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
- #left! ⇒ Object
- #menu(r: :center, c: :center, items:, curr: 0, title: nil, fg: White, bg: Blue) ⇒ Object
- #noscroll ⇒ Object
- #output(&block) ⇒ Object
- #p(*args) ⇒ Object
- #print(*args) ⇒ Object
- #putch(ch, r: nil, c: nil, fx: nil) ⇒ Object
- #puts(*args) ⇒ Object
- #rc ⇒ Object
- #rcprint(r, c, *args) ⇒ Object
- #rcprint!(r, c, *args) ⇒ Object
- #refresh ⇒ Object
- #restback(high, wide, r, c) ⇒ Object
- #right(n = 1) ⇒ Object
- #right! ⇒ Object
- #saveback(high, wide, r, c) ⇒ Object
-
#screen_text(file = nil) ⇒ Object
rename?.
- #scroll(n = 1) ⇒ Object
- #set_colors(fg, bg) ⇒ Object
- #top ⇒ Object
- #up(n = 1) ⇒ Object
- #up! ⇒ Object
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
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/window.rb', line 10 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 = X::Window.new(high, wide, r0, c0) colorize!(fg, bg) if @border @cwin.box(Vert, Horiz) @outer = @cwin @outer.refresh @cwin = X::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.
6 7 8 |
# File 'lib/window.rb', line 6 def bg @bg end |
#c0 ⇒ Object (readonly)
Returns the value of attribute c0.
5 6 7 |
# File 'lib/window.rb', line 5 def c0 @c0 end |
#cols ⇒ Object (readonly)
Returns the value of attribute cols.
4 5 6 |
# File 'lib/window.rb', line 4 def cols @cols end |
#cwin ⇒ Object (readonly)
Returns the value of attribute cwin.
4 5 6 |
# File 'lib/window.rb', line 4 def cwin @cwin end |
#fg ⇒ Object
Returns the value of attribute fg.
6 7 8 |
# File 'lib/window.rb', line 6 def fg @fg end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
4 5 6 |
# File 'lib/window.rb', line 4 def height @height end |
#r0 ⇒ Object (readonly)
Returns the value of attribute r0.
5 6 7 |
# File 'lib/window.rb', line 5 def r0 @r0 end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
4 5 6 |
# File 'lib/window.rb', line 4 def rows @rows end |
#scrolling(flag = true) ⇒ Object (readonly)
FIXME refactor bad code
62 63 64 |
# File 'lib/window.rb', line 62 def scrolling @scrolling end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
4 5 6 |
# File 'lib/window.rb', line 4 def width @width end |
Class Method Details
.clear(win) ⇒ Object
delete this?
99 100 101 102 103 104 105 |
# File 'lib/output.rb', line 99 def self.clear(win) # delete this? num = win.maxx * win.maxy win.setpos(0, 0) win.addstr(' '*num) win.setpos(0, 0) win.refresh end |
.colorize!(cwin, fg, bg) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/color.rb', line 28 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
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/window.rb', line 32 def self.main(fg: White, bg: Blue, scroll: false) debug "Starting #main..." main_win = X.init_screen X.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
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/window.rb', line 46 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
118 119 120 121 122 123 |
# File 'lib/output.rb', line 118 def [](r, c) ch = nil go(r, c) { ch = @cwin.inch } debug "ch = #{ch} ch.chr = #{ch.chr}" ch.chr end |
#[]=(r, c, char) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/output.rb', line 125 def []=(r, c, char) @cwin.setpos(r, c) @cwin.addch(char[0].ord|X::A_NORMAL) @cwin.setpos(r, c) @cwin.refresh end |
#_putch(ch) ⇒ Object
59 60 61 |
# File 'lib/output.rb', line 59 def _putch(ch) @cwin.addch(ch) end |
#bottom ⇒ Object
66 67 68 69 70 |
# File 'lib/navigation.rb', line 66 def bottom r, c = rc rmax = self.rows - 1 go rmax, c end |
#boxme ⇒ Object
132 133 134 135 |
# File 'lib/output.rb', line 132 def boxme @outer.box(Vert, Horiz) @outer.refresh end |
#center(str) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/output.rb', line 4 def center(str) r, c = self.rc n = @cwin.maxx - str.length go r, n/2 self.puts str end |
#clear ⇒ Object
107 108 109 110 |
# File 'lib/output.rb', line 107 def clear self.home self.scroll self.rows end |
#colorize!(fg, bg) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/color.rb', line 48 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
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/navigation.rb', line 3 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…
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/output.rb', line 81 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
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/output.rb', line 17 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
46 47 48 49 |
# File 'lib/navigation.rb', line 46 def down(n=1) r, c = rc go r+n, c end |
#down! ⇒ Object
76 77 78 |
# File 'lib/navigation.rb', line 76 def down! bottom end |
#effect?(arg) ⇒ Boolean
FIXME Please refactor the Hal out of this.
13 14 15 |
# File 'lib/output.rb', line 13 def effect?(arg) arg.is_a?(RubyText::Effects) end |
#gets ⇒ Object
still needs improvement
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 |
# File 'lib/output.rb', line 141 def gets # still needs improvement # echo assumed to be OFF, keypad ON str = "" i = 0 r0, c0 = self.rc loop do ch = self.getch case ch when 10 # Enter self.crlf break when 8, 127, 63 # backspace, del, ^H (huh?) if i >= 1 str = str[0..-2] i -= 1 self.left self[*self.rc] = ' ' self.left end when 260 # left-arrow i -= 1 self.left when 261 # right-arrow i += 1 self.right else str.insert(i, ch) self.right self.go(r0, c0) { self.print str } i += 1 end end str end |
#go(r0, c0) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/navigation.rb', line 31 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
only accepts numbers!
27 28 29 |
# File 'lib/navigation.rb', line 27 def goto(r, c) # only accepts numbers! @cwin.setpos(r, c) end |
#home ⇒ Object
91 92 93 |
# File 'lib/navigation.rb', line 91 def home go 0, 0 end |
#left(n = 1) ⇒ Object
51 52 53 54 |
# File 'lib/navigation.rb', line 51 def left(n=1) r, c = rc go r, c-n end |
#left! ⇒ Object
80 81 82 83 |
# File 'lib/navigation.rb', line 80 def left! r, c = rc go r, 0 end |
#menu(r: :center, c: :center, items:, curr: 0, title: nil, fg: White, bg: Blue) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 |
# File 'lib/menu.rb', line 4 def (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 = 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 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 end |
#noscroll ⇒ Object
67 68 69 70 |
# File 'lib/window.rb', line 67 def noscroll @scrolling = false @cwin.scrollok(false) end |
#output(&block) ⇒ Object
112 113 114 115 116 |
# File 'lib/output.rb', line 112 def output(&block) $stdscr = self block.call $stdscr = STDSCR end |
#p(*args) ⇒ Object
46 47 48 |
# File 'lib/output.rb', line 46 def p(*args) delegate_output(:p, *args) end |
#print(*args) ⇒ Object
42 43 44 |
# File 'lib/output.rb', line 42 def print(*args) delegate_output(:print, *args) end |
#putch(ch, r: nil, c: nil, fx: nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/output.rb', line 63 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
38 39 40 |
# File 'lib/output.rb', line 38 def puts(*args) delegate_output(:puts, *args) end |
#rc ⇒ Object
95 96 97 |
# File 'lib/navigation.rb', line 95 def rc [@cwin.cury, @cwin.curx] end |
#rcprint(r, c, *args) ⇒ Object
50 51 52 |
# File 'lib/output.rb', line 50 def rcprint(r, c, *args) self.go(r, c) { self.print *args } end |
#rcprint!(r, c, *args) ⇒ Object
54 55 56 57 |
# File 'lib/output.rb', line 54 def rcprint!(r, c, *args) self.go(r, c) # Cursor isn't restored self.print *args end |
#refresh ⇒ Object
137 138 139 |
# File 'lib/output.rb', line 137 def refresh @cwin.refresh end |
#restback(high, wide, r, c) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/window.rb', line 114 def restback(high, wide, r, c) 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 end |
#right(n = 1) ⇒ Object
56 57 58 59 |
# File 'lib/navigation.rb', line 56 def right(n=1) r, c = rc go r, c+n end |
#right! ⇒ Object
85 86 87 88 89 |
# File 'lib/navigation.rb', line 85 def right! r, c = rc cmax = self.cols - 1 go r, cmax end |
#saveback(high, wide, r, c) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/window.rb', line 100 def saveback(high, wide, r, c) debug "saveback: #{[high, wide, r, c].inspect}" @pos = self.rc @save = [] 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 end |
#screen_text(file = nil) ⇒ Object
rename?
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/window.rb', line 87 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/window.rb', line 72 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
43 44 45 46 |
# File 'lib/color.rb', line 43 def set_colors(fg, bg) cp = RubyText::Color.pair(fg, bg) @cwin.color_set(cp) end |
#top ⇒ Object
61 62 63 64 |
# File 'lib/navigation.rb', line 61 def top r, c = rc go 0, c end |
#up(n = 1) ⇒ Object
41 42 43 44 |
# File 'lib/navigation.rb', line 41 def up(n=1) r, c = rc go r-n, c end |
#up! ⇒ Object
72 73 74 |
# File 'lib/navigation.rb', line 72 def up! top end |