Class: Bogo::Ui::Table

Inherits:
Object
  • Object
show all
Includes:
CommandLineReporter
Defined in:
lib/bogo-ui/table.rb

Overview

Table output helper

Defined Under Namespace

Classes: BufferedRow, BufferedTable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ui, inst = nil) { ... } ⇒ self

Create a new instance

Parameters:

Yields:

  • table content



24
25
26
27
28
29
30
# File 'lib/bogo-ui/table.rb', line 24

def initialize(ui, inst=nil, &block)
  @proxy_to = inst
  @ui = ui
  @base = block
  @content = []
  @printed_lines = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m_name, *args, &block) ⇒ Object

If proxy instance is provided, forward if possible



33
34
35
36
37
38
39
# File 'lib/bogo-ui/table.rb', line 33

def method_missing(m_name, *args, &block)
  if(proxy_to && proxy_to.respond_to?(m_name, true))
    proxy_to.send(m_name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#proxy_toObject (readonly)

Returns:

  • (Object)


17
18
19
# File 'lib/bogo-ui/table.rb', line 17

def proxy_to
  @proxy_to
end

#table(options = {}) ⇒ self (readonly)

Override to provide buffered support

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (self)


15
16
17
# File 'lib/bogo-ui/table.rb', line 15

def table
  @table
end

#uiBogo::Ui (readonly)

Returns:



13
14
15
# File 'lib/bogo-ui/table.rb', line 13

def ui
  @ui
end

Instance Method Details

#displayself

Note:

can be called multiple times to print table updates

Output table to defined UI

Returns:

  • (self)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bogo-ui/table.rb', line 76

def display
  # init the table
  instance_exec(&@base)
  # load the table
  @content.each do |tblock|
    instance_exec(&tblock)
  end
  @table.output
  @table.buffer.rewind
  output = @table.buffer.read.split("\n")
  output.slice!(0, @printed_lines)
  @printed_lines = output.size
  ui.puts output.join("\n")
  self
end

#row(options = {}) ⇒ self

Override to provide buffered support

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (self)


64
65
66
67
68
69
70
# File 'lib/bogo-ui/table.rb', line 64

def row(options={})
  options[:encoding] ||= @table.encoding
  @row = BufferedRow.new(options.merge(:buffer => @table.buffer))
  yield
  @table.add(@row)
  self
end

#update { ... } ⇒ self

Update the table content

Yields:

  • table content

Returns:

  • (self)


45
46
47
48
# File 'lib/bogo-ui/table.rb', line 45

def update(&block)
  @content << block
  self
end