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) { ... } ⇒ self

Create a new instance

Parameters:

Yields:

  • table content



22
23
24
25
26
27
# File 'lib/bogo-ui/table.rb', line 22

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

Instance Attribute Details

#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)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bogo-ui/table.rb', line 64

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)


52
53
54
55
56
57
58
# File 'lib/bogo-ui/table.rb', line 52

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)


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

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