Class: ExcADG::Tui::Block

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Format

#box!, #fit!, #h_pad!, #pad!, #v_align!, #v_pad!

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



10
11
12
# File 'lib/excadg/tui/block.rb', line 10

def array
  @array
end

#widthObject (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;

Examples:

Block.column "some", "other"               # some and other will be centered in column by default
Block.column "some", "other", align: :left # some and other will be shifted 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! } # enclose both string to boxes before making a row

Parameters:

  • *rows

    array of rows (ExcADG::Tui::Blocks / Strings)

  • align (defaults to: nil)

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

  • &block

    idividual row processor, the block is supplied with ExcADG::Tui::Blocks



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

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 (ExcADG::Tui::Blocks / Strings)

  • align (defaults to: nil)

    how to align blocks between each other: :center (default), :top, :bottom

  • &block

    idividual column processor, the block is supplied with ExcADG::Tui::Blocks



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

Parameters:

  • other

    either Array or String to push back



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

Examples:

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

Parameters:

  • other

    either Array or String to push forward



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

#heightObject

column’s height is its size



103
104
105
# File 'lib/excadg/tui/block.rb', line 103

def height
  @array.size
end

#to_sObject

make printable



71
72
73
# File 'lib/excadg/tui/block.rb', line 71

def to_s
  @array.join "\n"
end