Class: Tui::Block
Overview
Basic TUI building block.
In a nutshell, all blocks are columns (Block.column) with Strings in it printed vertically one by one (see #to_s). So a row is a way to compose multiple columns to a single "column" (see Block.row), which is an Array of lines anyway.
Instance Attribute Summary collapse
-
#array ⇒ Object
readonly
Returns the value of attribute array.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
-
.column(*rows, align: :center, &block) ⇒ Object
Make a column with several rows in it.
-
.row(*cols, align: :center, &block) ⇒ Object
Compose a row of other Blocks or Strings.
Instance Method Summary collapse
-
#<<(other) ⇒ Object
Add extra lines from the supplied array to the block; no auto-alignment is performed, see Format#v_align! to make width even.
-
#>>(other) ⇒ Object
Add extra lines to the "start" of the block.
-
#collect!(&block) ⇒ Object
Modify each "row" of the Block inline.
- #height ⇒ Object
-
#to_s ⇒ Object
"Render" the Block to print to the console.
Methods included from Format
#align!, #box!, #fit!, #h_align!, #h_pad!, #pad!, #v_align!, #v_pad!
Instance Attribute Details
#array ⇒ Object (readonly)
Returns the value of attribute array.
15 16 17 |
# File 'lib/tui/block.rb', line 15 def array @array end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
15 16 17 |
# File 'lib/tui/block.rb', line 15 def width @width end |
Class Method Details
.column(*rows, align: :center, &block) ⇒ Object
Make a column with several rows in it. Rows could be other Tui::Blocks or just Strings.
The method actually transform array or "rows" to a single column right away.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/tui/block.rb', line 36 def self.column *rows, align: :center, &block # row could be a String, make an array of horizontal lines from it rows.collect! { |col| col.is_a?(Block) ? col : Block.new(col) } rows.collect!(&block) if block_given? # pre-process "rows" max_row_width = rows.collect(&:width).max Block.new rows.collect! { |blk| extra_columns = max_row_width - blk.width case align when :left then blk.collect! { |line| line + ' ' * extra_columns } when :right then blk.collect! { |line| ' ' * extra_columns + line } else blk.h_pad!(extra_columns / 2) extra_columns.odd? ? blk.collect! { |line| line + ' ' } : blk end blk.array # get the array to join using builtin flatten }.flatten! end |
.row(*cols, align: :center, &block) ⇒ Object
Compose a row of other Tui::Blocks or Strings.
The method actually squashes a row of columns (blocks) to a single block (column)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/tui/block.rb', line 65 def self.row *cols, align: :center, &block cols.collect! { |col| col.is_a?(Block) ? col : Block.new(col) } cols.collect!(&block) if block_given? # pre-process columns max_col_height = cols.collect(&:height).max Block.new cols.collect! { |col| extra_lines = max_col_height - col.height case align when :top then col << Array.new(extra_lines, '') when :bottom then col >> Array.new(extra_lines, '') else col.v_pad!(extra_lines / 2) col << '' if extra_lines.odd? end col.v_align! # is needed due to transpose call below col.array # get the array to process using builtin methods }.transpose.collect(&:join) end |
Instance Method Details
#<<(other) ⇒ Object
Add extra lines from the supplied array to the block; no auto-alignment is performed, see Format#v_align! to make width even
93 94 95 96 97 |
# File 'lib/tui/block.rb', line 93 def << other other.is_a?(Array) ? @array += other : @array << other @width = Tools.calc_width @array self end |
#>>(other) ⇒ Object
Add extra lines to the "start" of the block
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/tui/block.rb', line 104 def >> other case other when Array @width = [@width, Tools.calc_width(other)].max other.reverse_each { |i| @array.unshift i } when String @width = [@width, Tools.calc_width(other)].max @array.unshift other end self end |
#collect!(&block) ⇒ Object
Modify each "row" of the Tui::Block inline
125 126 127 128 |
# File 'lib/tui/block.rb', line 125 def collect! &block @array.collect!(&block) @width = Tools.calc_width @array end |
#height ⇒ Object
Get Tui::Block's height in symbols Block is a column => column's height is Tui::Block's array size
120 121 122 |
# File 'lib/tui/block.rb', line 120 def height @array.size end |
#to_s ⇒ Object
"Render" the Block to print to the console. As each block and operation just transforms a list of Strings, the whole "rendering" is as simple as ...
86 87 88 |
# File 'lib/tui/block.rb', line 86 def to_s @array.join "\n\r" end |