Class: Tui::Block

Inherits:
Object
  • Object
show all
Includes:
Format
Defined in:
lib/tui/block.rb

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

Class Method Summary collapse

Instance Method Summary collapse

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.

Examples:

Block.column "some", "other"                   # "some" and "other will be centered in the column by default
Block.column "some", "other", align: :left     # "some" and "other" will be aligned to the left
Block.column
  Block.row("one", "two"),
  "three"
]}                                             # "one" and "two" will be printed in the first row, "three" will be below them centered
Block.column("some", "other") { |el| el.box! } # box both string then place boxes to a column

Parameters:

  • rows —

    array of rows (Tui::Blocks / Strings)

  • align (defaults to: :center) —

    how to align blocks between each other: :center (default), :right, :left

  • block —

    individual row processor, the block is supplied with Tui::Blocks



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)

Examples:

Block.row "some", "other"                         # => "someother"
Block.row "some", ["other", "foo"], aligh: bottom # "some" will be shifted down by 1 line
Block.row("some", "other") { |blk| blk.box! }     # both columns will be enclosed in a box

Parameters:

  • cols —

    array of columns (Tui::Blocks / Strings) to squash

  • align (defaults to: :center) —

    how to align blocks in the row: :center, :top, :bottom

  • block —

    individual column processor



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

Parameters:

  • other —

    either Array or String to push back



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

Examples:

Block.column '1'
block << %w[2 3] # now block has %w[2 3 1]

Parameters:

  • other —

    either Array or String to push_forward



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