Class: ExcADG::Tui::Block
- Inherits:
-
Object
- Object
- ExcADG::Tui::Block
- Includes:
- Format
- Defined in:
- lib/excadg/tui/block.rb
Overview
basic TUI building block; in a nutshell, it’s a column, as Strings in it are printed vertically one by one (see #to_s); it could be a row only semantically if it’s a part of a vertically composed array (see Block.column)
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: nil, &block) ⇒ Object
transform array or “rows” to a single column;.
-
.row(*cols, align: nil, &block) ⇒ Object
squash a row of columns (blocks) to a single block.
Instance Method Summary collapse
-
#<<(other) ⇒ Object
add extra lines from the supplied array to the block; no auto-alignment is performed, see #v_align to make width even.
-
#>>(other) ⇒ Object
add extra lines to the start of the block.
-
#collect!(&block) ⇒ Object
main inline modification method.
-
#height ⇒ Object
column’s height is its size.
-
#to_s ⇒ Object
make printable.
Methods included from Format
#box!, #fit!, #h_pad!, #pad!, #v_align!, #v_pad!
Instance Attribute Details
#array ⇒ Object (readonly)
Returns the value of attribute array.
10 11 12 |
# File 'lib/excadg/tui/block.rb', line 10 def array @array end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
10 11 12 |
# File 'lib/excadg/tui/block.rb', line 10 def width @width end |
Class Method Details
.column(*rows, align: nil, &block) ⇒ Object
transform array or “rows” to a single column;
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/excadg/tui/block.rb', line 26 def self.column *rows, align: nil, &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? # allow to 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: nil, &block) ⇒ Object
squash a row of columns (blocks) to a single block
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/excadg/tui/block.rb', line 52 def self.row *cols, align: nil, &block cols.collect! { |col| col.is_a?(Block) ? col : Block.new(col) } cols.collect!(&block) if block_given? # allow to 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 #v_align to make width even
78 79 80 81 82 |
# File 'lib/excadg/tui/block.rb', line 78 def << other other.is_a?(Array) ? @array += other : @array << other @width = @array.collect(&:size).max self end |
#>>(other) ⇒ Object
add extra lines to the start of the block
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/excadg/tui/block.rb', line 89 def >> other case other when Array other.reverse_each { |i| @array.unshift i } when String @array.unshift other end @width = @array.collect(&:size).max self end |
#collect!(&block) ⇒ Object
main inline modification method
108 109 110 111 |
# File 'lib/excadg/tui/block.rb', line 108 def collect! &block @array.collect!(&block) @width = @array.collect(&:size).max end |
#height ⇒ Object
column’s height is its size
103 104 105 |
# File 'lib/excadg/tui/block.rb', line 103 def height @array.size end |
#to_s ⇒ Object
make printable
71 72 73 |
# File 'lib/excadg/tui/block.rb', line 71 def to_s @array.join "\n" end |