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.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rubytext.rb', line 175

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.



173
174
175
# File 'lib/rubytext.rb', line 173

def bg
  @bg
end

#colsObject (readonly)

Returns the value of attribute cols.



173
174
175
# File 'lib/rubytext.rb', line 173

def cols
  @cols
end

#fgObject (readonly)

Returns the value of attribute fg.



173
174
175
# File 'lib/rubytext.rb', line 173

def fg
  @fg
end

#heightObject (readonly)

Returns the value of attribute height.



173
174
175
# File 'lib/rubytext.rb', line 173

def height
  @height
end

#rowsObject (readonly)

Returns the value of attribute rows.



173
174
175
# File 'lib/rubytext.rb', line 173

def rows
  @rows
end

#widthObject (readonly)

Returns the value of attribute width.



173
174
175
# File 'lib/rubytext.rb', line 173

def width
  @width
end

#winObject (readonly)

Returns the value of attribute win.



173
174
175
# File 'lib/rubytext.rb', line 173

def win
  @win
end

Class Method Details

.main(fg: nil, bg: nil) ⇒ Object



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

def self.main(fg: nil, bg: nil)
  debug "Entering Window.main (#{fg}, #{bg}) => "
  fg, bg, cp = fb2cp(fg, bg)
  debug "  computed #{fg}, #{bg}, #{cp}"
  debug "  cp|A_NORMAL = #{cp|X::A_NORMAL}"
  @main_win = X.init_screen
  X.start_color
  X.stdscr.bkgd(cp|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)
  @screen
end

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rubytext.rb', line 73

def self.make(cwin, high, wide, r0, c0, border)
  debug "make: #{[cwin, high, wide, r0, c0, border]}"
  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



261
262
263
264
265
266
267
268
# File 'lib/rubytext.rb', line 261

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



270
271
272
273
# File 'lib/rubytext.rb', line 270

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

#boxmeObject



275
276
277
278
# File 'lib/rubytext.rb', line 275

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

#clearObject



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

def clear
  @win.clear
end

#delegate_output(sym, *args) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rubytext.rb', line 197

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



242
243
244
245
# File 'lib/rubytext.rb', line 242

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

#go(r, c) ⇒ Object



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

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



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

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

#p(*args) ⇒ Object



220
221
222
# File 'lib/rubytext.rb', line 220

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


216
217
218
# File 'lib/rubytext.rb', line 216

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

#puts(*args) ⇒ Object



212
213
214
# File 'lib/rubytext.rb', line 212

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

#rcObject



247
248
249
# File 'lib/rubytext.rb', line 247

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

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



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

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

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



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

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

#refreshObject



280
281
282
# File 'lib/rubytext.rb', line 280

def refresh
  @win.refresh
end