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 = nil, bg = nil) ⇒ Window

Returns a new instance of Window.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rubytext.rb', line 169

def initialize(high=nil, wide=nil, r0=1, c0=1, border=false, fg=nil, bg=nil)
  debug "RT::Win.init: #{[high, wide, r0, c0, border]}"
  @wide, @high, @r0, @c0 = wide, high, r0, c0
  @border, @fg, @bg      = border, fg, bg
  @win = X::Window.new(high, wide, r0, c0)
  debug "outer = #{@win.inspect}"
  debug "@border = #@border"
  debug "Calling 'colors': #{[@win, fg, bg]}"
  RubyText::Window.colors!(@win, fg, bg)
#   self.clear(@main_win)
  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??
    RubyText::Window.colors!(@win, fg, bg)
  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.



57
58
59
# File 'lib/rubytext.rb', line 57

def bg
  @bg
end

#colsObject (readonly)

Returns the value of attribute cols.



167
168
169
# File 'lib/rubytext.rb', line 167

def cols
  @cols
end

#fgObject (readonly)

Returns the value of attribute fg.



57
58
59
# File 'lib/rubytext.rb', line 57

def fg
  @fg
end

#heightObject (readonly)

Returns the value of attribute height.



167
168
169
# File 'lib/rubytext.rb', line 167

def height
  @height
end

#rowsObject (readonly)

Returns the value of attribute rows.



167
168
169
# File 'lib/rubytext.rb', line 167

def rows
  @rows
end

#widthObject (readonly)

Returns the value of attribute width.



167
168
169
# File 'lib/rubytext.rb', line 167

def width
  @width
end

#winObject (readonly)

Returns the value of attribute win.



167
168
169
# File 'lib/rubytext.rb', line 167

def win
  @win
end

Class Method Details

.clear(win) ⇒ Object



319
320
321
322
323
324
325
# File 'lib/rubytext.rb', line 319

def self.clear(win)
  num = win.maxx * win.maxy
  win.setpos(0, 0)
  win.addstr(' '*num)
  win.setpos(0, 0)
  win.refresh
end

.colors(win, fg, bg) ⇒ Object



59
60
61
62
63
# File 'lib/rubytext.rb', line 59

def self.colors(win, fg, bg)
  cfg, cbg, cp = fb2cp(fg, bg)
  X.init_pair(cp, cfg, cbg)
  win.color_set(cp|X::A_NORMAL)
end

.colors!(win, fg, bg) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/rubytext.rb', line 65

def self.colors!(win, fg, bg)
  colors(win, fg, bg)
  num = win.maxx * win.maxy
  win.setpos(0, 0)
  win.addstr(' '*num)
  win.setpos(0, 0)
  win.refresh
end

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rubytext.rb', line 74

def self.main(fg: nil, bg: nil)
  @main_win = X.init_screen
  X.start_color
  colors!(@main_win, fg, bg)
  rows, cols = @main_win.maxy, @main_win.maxx
  @screen = self.make(@main_win, rows, cols, 0, 0, false,
                      fg: fg, bg: bg)
# FIXME Why is this hard to inline?
#     @win = @main_win
#     obj = self.allocate
#     obj.instance_eval do 
#       @outer = @win = @main_win
#       @wide, @high, @r0, @c0 = cols, rows, 0, 0
#       @fg, @bg = fg, bg
#       @border = false
#       @rows, @cols = @high, @wide
#       @width, @height = @cols + 2, @rows + 2 if @border
#     end
#     @win = @main_win  # FIXME?
#     obj
  @screen
end

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



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rubytext.rb', line 97

def self.make(cwin, high, wide, r0, c0, border, fg: :white, bg: :black)
  obj = self.allocate
  obj.instance_eval do 
    debug "  Inside instance_eval..."
    @outer = @win = 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
end

Instance Method Details

#[](r, c) ⇒ Object



342
343
344
345
346
347
348
349
# File 'lib/rubytext.rb', line 342

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

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



351
352
353
354
355
# File 'lib/rubytext.rb', line 351

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

#bottomObject



281
282
283
284
285
# File 'lib/rubytext.rb', line 281

def bottom 
  r, c = rc
  rmax = self.rows - 1
  go rmax, c
end

#boxmeObject



357
358
359
360
# File 'lib/rubytext.rb', line 357

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

#center(str) ⇒ Object



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

def center(str)
  r, c = self.rc
  n = @win.maxx - str.length
  go r, n/2
  puts str
end

#clearObject



327
328
329
330
331
332
333
334
# File 'lib/rubytext.rb', line 327

def clear
  win = @win
  num = win.maxx * win.maxy
  win.setpos(0, 0)
  win.addstr(' '*num)
  win.setpos(0, 0)
  win.refresh
end

#crlfObject



310
311
312
313
# File 'lib/rubytext.rb', line 310

def crlf
  r, c = rc
  go r+1, 0
end

#delegate_output(sym, *args) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/rubytext.rb', line 206

def delegate_output(sym, *args)
  args = [""] if args.empty?
debug "delegate: colors are #@fg, #@bg"
  RubyText::Window.colors(@win, @fg, @bg)  # FIXME?
#   debug "#{sym}: args = #{args.inspect}"
  if sym == :p
    args.map!(&:inspect) 
  else
    args.map!(&:to_s) 
  end
  str = sprintf(*args)
  flag = true if sym != :print && str[-1] != "\n"
  # FIXME: color-handling code here
  str.each_char do |ch|
    ch == "\n" ? crlf : @win.addch(ch)
  end
  crlf if flag
  @win.refresh
end

#down(n = 1) ⇒ Object



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

def down(n=1)
  r, c = rc
  go r+n, c
end

#down!Object



291
292
293
# File 'lib/rubytext.rb', line 291

def down!
  bottom
end

#go(r, c) ⇒ Object



247
248
249
250
251
252
253
254
# File 'lib/rubytext.rb', line 247

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

#homeObject



306
307
308
# File 'lib/rubytext.rb', line 306

def home
  go 0, 0
end

#left(n = 1) ⇒ Object



266
267
268
269
# File 'lib/rubytext.rb', line 266

def left(n=1)
  r, c = rc
  go r, c-n
end

#left!Object



295
296
297
298
# File 'lib/rubytext.rb', line 295

def left!
  r, c = rc
  go r, 0
end

#output(&block) ⇒ Object



336
337
338
339
340
# File 'lib/rubytext.rb', line 336

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

#p(*args) ⇒ Object



234
235
236
# File 'lib/rubytext.rb', line 234

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


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

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

#putch(ch) ⇒ Object



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

def putch(ch)
  r, c = self.rc
  self[r, c] = ch[0]
end

#puts(*args) ⇒ Object



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

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

#rcObject



315
316
317
# File 'lib/rubytext.rb', line 315

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

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



238
239
240
# File 'lib/rubytext.rb', line 238

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

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



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

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

#refreshObject



362
363
364
# File 'lib/rubytext.rb', line 362

def refresh
  @win.refresh
end

#right(n = 1) ⇒ Object



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

def right(n=1)
  r, c = rc
  go r, c+n
end

#right!Object



300
301
302
303
304
# File 'lib/rubytext.rb', line 300

def right!
  r, c = rc
  cmax = self.cols - 1
  go r, cmax
end

#topObject



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

def top
  r, c = rc
  go 0, c
end

#up(n = 1) ⇒ Object



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

def up(n=1)
  r, c = rc
  go r-n, c
end

#up!Object



287
288
289
# File 'lib/rubytext.rb', line 287

def up!
  top
end