Class: RubyText::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytext.rb,
lib/rubytext.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(high = nil, wide = nil, r0 = 1, c0 = 1, border = false, fg = :white, bg = :black) ⇒ Window

Returns a new instance of Window.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/rubytext.rb', line 155

def initialize(high=nil, wide=nil, r0=1, c0=1, border=false, fg=:white, bg=:black)
  debug "RT::Win.init: #{[high, wide, r0, c0, border]}"
  @wide, @high, @r0, @c0 = wide, high, r0, c0
  @border = border
  @fg, @bg = fg, bg
  @win = X::Window.new(high, wide, r0, c0)
  debug "outer = #{@win.inspect}"
  debug "@border = #@border"
  if @border
    @win.box(Vert, Horiz)
    @outer = @win
    @outer.refresh
    debug "About to call again: params = #{[high-2, wide-2, r0+1, c0+1]}"
    @win = X::Window.new(high-2, wide-2, r0+1, c0+1) # , false, fg, bg)  # relative now??
  else
    @outer = @win
  end
  @rows, @cols = @win.maxy, @win.maxx  # unnecessary really...
  @width, @height = @cols + 2, @rows + 2 if @border
  @win.refresh
end

Instance Attribute Details

#bgObject (readonly)

Returns the value of attribute bg.



153
154
155
# File 'lib/rubytext.rb', line 153

def bg
  @bg
end

#colsObject (readonly)

Returns the value of attribute cols.



153
154
155
# File 'lib/rubytext.rb', line 153

def cols
  @cols
end

#fgObject (readonly)

Returns the value of attribute fg.



153
154
155
# File 'lib/rubytext.rb', line 153

def fg
  @fg
end

#heightObject (readonly)

Returns the value of attribute height.



153
154
155
# File 'lib/rubytext.rb', line 153

def height
  @height
end

#rowsObject (readonly)

Returns the value of attribute rows.



153
154
155
# File 'lib/rubytext.rb', line 153

def rows
  @rows
end

#widthObject (readonly)

Returns the value of attribute width.



153
154
155
# File 'lib/rubytext.rb', line 153

def width
  @width
end

#winObject (readonly)

Returns the value of attribute win.



153
154
155
# File 'lib/rubytext.rb', line 153

def win
  @win
end

Class Method Details

.mainObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubytext.rb', line 49

def self.main
  debug "Entering Window.main"
  @main_win = X.init_screen
  X.start_color
#     X.init_pair(1, X::COLOR_BLACK, X::COLOR_WHITE)
#     X.stdscr.bkgd(X.color_pair(1)|X::A_NORMAL)
  rows, cols = @main_win.maxy, @main_win.maxx
  debug "About to call .make"
  @screen = self.make(@main_win, rows, cols, 0, 0, false, "white", "black")
  @screen
end

.make(cwin, high, wide, r0, c0, border, fg, bg) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubytext.rb', line 61

def self.make(cwin, high, wide, r0, c0, border, fg, bg)
  debug "make: #{[cwin, high, wide, r0, c0, border, fg, bg]}"
  obj = self.allocate
  debug "Allocate returned a #{obj.class}"
  obj.instance_eval do 
    debug "  Inside instance_eval..."
    @outer = @win = cwin
    @wide, @high, @r0, @c0 = wide, high, r0, c0
    @border = border
    @rows, @cols = high, wide
    @width, @height = @cols + 2, @rows + 2 if @border
  end
  obj
end

Instance Method Details

#[](r, c) ⇒ Object



241
242
243
244
245
246
247
248
# File 'lib/rubytext.rb', line 241

def [](r, c)
  save = self.rc
  @win.setpos r, c
  ch = @win.inch
  @win.setpos *save
  ch
#   go(r, c) { ch = @win.inch }
end

#[]=(r, c, char) ⇒ Object



250
251
252
253
# File 'lib/rubytext.rb', line 250

def []=(r, c, char)
  @win.setpos(r, c)
  @win.addstr(char[0])
end

#boxmeObject



255
256
257
258
# File 'lib/rubytext.rb', line 255

def boxme
  @outer.box(Vert, Horiz)
  @outer.refresh
end

#clearObject



231
232
233
# File 'lib/rubytext.rb', line 231

def clear
  @win.clear
end

#delegate_output(sym, *args) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/rubytext.rb', line 177

def delegate_output(sym, *args)
  args = [""] if args.empty?
#   debug "#{sym}: args = #{args.inspect}"
  if sym == :p
    args.map!(&:inspect) 
  else
    args.map!(&:to_s) 
  end
  str = sprintf(*args)
  str << "\n" if sym != :print && str[-1] != "\n"
  # color-handling code here
  @win.addstr(str)
  @win.refresh
end

#downObject



222
223
224
225
# File 'lib/rubytext.rb', line 222

def down
  r, c = rc
  go r+1, c
end

#go(r, c) ⇒ Object



213
214
215
216
217
218
219
220
# File 'lib/rubytext.rb', line 213

def go(r, c)
  save = self.rc
  @win.setpos(r, c)
  if block_given?
    yield
    go(*save)   # No block here!
  end
end

#output(&block) ⇒ Object



235
236
237
238
239
# File 'lib/rubytext.rb', line 235

def output(&block)
  $stdscr = self
  block.call
  $stdscr = STDSCR
end

#p(*args) ⇒ Object



200
201
202
# File 'lib/rubytext.rb', line 200

def p(*args)
  delegate_output(:p, *args)
end


196
197
198
# File 'lib/rubytext.rb', line 196

def print(*args)
  delegate_output(:print, *args)
end

#puts(*args) ⇒ Object



192
193
194
# File 'lib/rubytext.rb', line 192

def puts(*args)
  delegate_output(:puts, *args)
end

#rcObject



227
228
229
# File 'lib/rubytext.rb', line 227

def rc
  [@win.cury, @win.curx]
end

#rcprint(r, c, *args) ⇒ Object



204
205
206
# File 'lib/rubytext.rb', line 204

def rcprint(r, c, *args)
  self.go(r, c) { self.print *args }
end

#rcprint!(r, c, *args) ⇒ Object



208
209
210
211
# File 'lib/rubytext.rb', line 208

def rcprint!(r, c, *args)
  @win.setpos(r, c)  # Cursor isn't restored
  self.print *args
end

#refreshObject



260
261
262
# File 'lib/rubytext.rb', line 260

def refresh
  @win.refresh
end