Class: Window

Inherits:
Object show all
Defined in:
lib/drydock/console.rb

Overview

:nodoc:all

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Window

Returns a new instance of Window.



226
227
228
229
230
231
232
233
234
235
# File 'lib/drydock/console.rb', line 226

def initialize(*args)
  @row = 1
  @col = 1
  @width = 10
  @height = 5
  @text = ""
  @fg = :default
  @bg = :default
  @threads = []
end

Instance Attribute Details

#bgObject

Returns the value of attribute bg.



223
224
225
# File 'lib/drydock/console.rb', line 223

def bg
  @bg
end

#colObject

Returns the value of attribute col.



223
224
225
# File 'lib/drydock/console.rb', line 223

def col
  @col
end

#fgObject

Returns the value of attribute fg.



223
224
225
# File 'lib/drydock/console.rb', line 223

def fg
  @fg
end

#heightObject

Returns the value of attribute height.



223
224
225
# File 'lib/drydock/console.rb', line 223

def height
  @height
end

#rowObject

Returns the value of attribute row.



223
224
225
# File 'lib/drydock/console.rb', line 223

def row
  @row
end

#textObject

Returns the value of attribute text.



223
224
225
# File 'lib/drydock/console.rb', line 223

def text
  @text
end

#threadsObject (readonly)

Returns the value of attribute threads.



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

def threads
  @threads
end

#widthObject

Returns the value of attribute width.



223
224
225
# File 'lib/drydock/console.rb', line 223

def width
  @width
end

Class Method Details

.bar(len, unit = '=') ⇒ Object



246
247
248
# File 'lib/drydock/console.rb', line 246

def self.bar(len, unit='=')
  unit*len
end

Instance Method Details

#every_n_seconds(n) ⇒ Object

Execute the given block every n seconds in a separate thread. The lower limit for n is 1 second. Returns a Thread object.



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/drydock/console.rb', line 254

def every_n_seconds(n)
  #n = 1 if n < 1
  thread = Thread.new do
    
    begin
      while true
        before = Time.now
        yield
        interval = n - (Time.now - before)
        sleep(interval) if interval > 0
      end
    rescue Interrupt
      break
    ensure
      thread
    end
  end
end

#join_threadsObject



300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/drydock/console.rb', line 300

def join_threads
  begin
    @threads.each do |t|
      t.join
    end
  rescue Interrupt
  ensure
    @threads.each do |t|
      t.kill
    end
  end
end

#positionObject



242
243
244
# File 'lib/drydock/console.rb', line 242

def position
  [@row, @col]
end

#position=(x, y = nil) ⇒ Object



237
238
239
240
# File 'lib/drydock/console.rb', line 237

def position=(x,y=nil)
  @x = x
  @y = y if y
end

#static(type, refresh = 2, props = {}, &b) ⇒ Object

Print text to the screen via type every refresh seconds. Print the return value of the block to the screen using the print_type method. refresh is number of seconds to wait props is the hash sent to print_type. Returns a Thread object.

# Print the time in the upper right corner every second
thread1 = Console.static(:right, 1, {:y => 0}) do 
  Time.now.utc.strftime("%Y-%m-%d %H:%M:%S").colour(:blue, :white, :underline)
end


284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/drydock/console.rb', line 284

def static(type, refresh=2, props={}, &b)
  meth = "print_#{type}"
  raise "#{meth} is not supported" unless Console.respond_to?(meth)
  
  refresh ||= 0
  refreh = refresh.to_s.to_i
  
  thread = every_n_seconds(refresh) do 
    Console.send(meth, b.call, props.clone) 
  end
  
  @threads << thread
  
  thread
end