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.



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

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.



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

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.



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

def height
  @height
end

#rowsObject (readonly)

Returns the value of attribute rows.



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

def rows
  @rows
end

#widthObject (readonly)

Returns the value of attribute width.



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

def width
  @width
end

#winObject (readonly)

Returns the value of attribute win.



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

def win
  @win
end

Class Method Details

.clear(win) ⇒ Object



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

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
96
# File 'lib/rubytext.rb', line 74

def self.main(fg: nil, bg: nil)
  @main_win = X.init_screen
debug(@main_win.inspect)
  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



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

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



287
288
289
290
291
292
293
294
# File 'lib/rubytext.rb', line 287

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



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

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

#boxmeObject



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

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

#center(row, str) ⇒ Object



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

def center(row, str)
  n = @win.maxx - str.length
  rcprint row, n/2, str
end

#clearObject



272
273
274
275
276
277
278
279
# File 'lib/rubytext.rb', line 272

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

#crlfObject



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

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

#delegate_output(sym, *args) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rubytext.rb', line 200

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

#downObject



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

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

#go(r, c) ⇒ Object



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

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



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

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

#p(*args) ⇒ Object



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

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


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

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

#puts(*args) ⇒ Object



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

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

#rcObject



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

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

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



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

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

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



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

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

#refreshObject



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

def refresh
  @win.refresh
end