Class: KXI::CLI::Table
- Inherits:
-
Object
- Object
- KXI::CLI::Table
- Defined in:
- lib/kxi/cli/table.rb
Overview
Table renderer
Defined Under Namespace
Classes: Column
Instance Method Summary collapse
-
#column(name) ⇒ KXI::CLI::Table::Column
Defines a column of table.
-
#initialize {|_self| ... } ⇒ Table
constructor
Instantiate the Table class.
-
#render ⇒ Object
Renders the table into stdout.
-
#row(e) ⇒ integer
Adds a row to table.
Constructor Details
#initialize {|_self| ... } ⇒ Table
Instantiate the KXI::CLI::Table class
8 9 10 11 12 |
# File 'lib/kxi/cli/table.rb', line 8 def initialize @cols = KXI::Collections::ArrayCollection.new @rows = 0 yield(self) if block_given? end |
Instance Method Details
#column(name) ⇒ KXI::CLI::Table::Column
Defines a column of table
17 18 19 20 21 |
# File 'lib/kxi/cli/table.rb', line 17 def column(name) col = Column.new(name) @cols.add(col) return col end |
#render ⇒ Object
Renders the table into stdout
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/kxi/cli/table.rb', line 32 def render @rows.times do |row| f = true @cols.foreach do |col| print(' ') unless f f = false col.write(row) end puts end end |
#row(e) ⇒ integer
Adds a row to table
26 27 28 29 |
# File 'lib/kxi/cli/table.rb', line 26 def row(e) e.each_key { |c| @cols.first { |i| i.name == c }&.row(e[c].to_s.strip.chomp) } @rows += 1 end |