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.



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/rubytext.rb', line 251

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

#bg ⇒ Object

Returns the value of attribute bg.



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

def bg
  @bg
end

#cols ⇒ Object (readonly)

Returns the value of attribute cols.



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

def cols
  @cols
end

#fg ⇒ Object

Returns the value of attribute fg.



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

def fg
  @fg
end

#height ⇒ Object (readonly)

Returns the value of attribute height.



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

def height
  @height
end

#rows ⇒ Object (readonly)

Returns the value of attribute rows.



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

def rows
  @rows
end

#width ⇒ Object (readonly)

Returns the value of attribute width.



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

def width
  @width
end

#win ⇒ Object (readonly)

Returns the value of attribute win.



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

def win
  @win
end

Class Method Details

.clear(win) ⇒ Object



415
416
417
418
419
420
421
# File 'lib/rubytext.rb', line 415

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



61
62
63
64
65
# File 'lib/rubytext.rb', line 61

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



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

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



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

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



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

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



438
439
440
441
442
443
444
445
# File 'lib/rubytext.rb', line 438

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



447
448
449
450
451
# File 'lib/rubytext.rb', line 447

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

#bottom ⇒ Object



377
378
379
380
381
# File 'lib/rubytext.rb', line 377

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

#boxme ⇒ Object



453
454
455
456
# File 'lib/rubytext.rb', line 453

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

#center(str) ⇒ Object



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

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

#clear ⇒ Object



423
424
425
426
427
428
429
430
# File 'lib/rubytext.rb', line 423

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

#crlf ⇒ Object



406
407
408
409
# File 'lib/rubytext.rb', line 406

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

#delegate_output(sym, *args) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/rubytext.rb', line 293

def delegate_output(sym, *args)
  args = [""] if args.empty?
  RubyText::Window.colors(@win, @fg, @bg)  # FIXME?
  if sym == :p
    args.map!(&:inspect) 
  else
    args.map! do |x|
      if RubyText::Colors.include? x
        x
      else
        x.to_s
      end
    end
  end
# STDOUT.puts "again: #{args.inspect}"
  flag = need_crlf?(sym, args)
  # Limitation: Can't print color symbols!
  args.each do |arg|  
    if arg.is_a? Symbol # must be a color
      RubyText::Window.colors(@win, arg, @bg)  # FIXME?
    else
      arg.each_char {|ch| ch == "\n" ? crlf : @win.addch(ch) }
    end
  end
  crlf if flag
  RubyText::Window.colors(@win, @fg, @bg)  # FIXME?
  @win.refresh
end

#down(n = 1) ⇒ Object



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

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

#down! ⇒ Object



387
388
389
# File 'lib/rubytext.rb', line 387

def down!
  bottom
end

#go(r, c) ⇒ Object



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

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

#home ⇒ Object



402
403
404
# File 'lib/rubytext.rb', line 402

def home
  go 0, 0
end

#left(n = 1) ⇒ Object



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

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

#left! ⇒ Object



391
392
393
394
# File 'lib/rubytext.rb', line 391

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

#need_crlf?(sym, args) ⇒ Boolean

Returns:

  • (Boolean)


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

def need_crlf?(sym, args)
  sym != :print &&      # print doesn't default to crlf
  args[-1][-1] != "\n"  # last char is a literal linefeed
end

#output(&block) ⇒ Object



432
433
434
435
436
# File 'lib/rubytext.rb', line 432

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

#p(*args) ⇒ Object



330
331
332
# File 'lib/rubytext.rb', line 330

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


326
327
328
# File 'lib/rubytext.rb', line 326

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

#putch(ch) ⇒ Object



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

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

#puts(*args) ⇒ Object



322
323
324
# File 'lib/rubytext.rb', line 322

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

#rc ⇒ Object



411
412
413
# File 'lib/rubytext.rb', line 411

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

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



334
335
336
# File 'lib/rubytext.rb', line 334

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

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



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

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

#refresh ⇒ Object



458
459
460
# File 'lib/rubytext.rb', line 458

def refresh
  @win.refresh
end

#right(n = 1) ⇒ Object



367
368
369
370
# File 'lib/rubytext.rb', line 367

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

#right! ⇒ Object



396
397
398
399
400
# File 'lib/rubytext.rb', line 396

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

#top ⇒ Object



372
373
374
375
# File 'lib/rubytext.rb', line 372

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

#up(n = 1) ⇒ Object



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

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

#up! ⇒ Object



383
384
385
# File 'lib/rubytext.rb', line 383

def up!
  top
end