Module: Tui::Format

Included in:
Block
Defined in:
lib/tui/format.rb

Overview

Collection of low-level Block formatting methods.

Most methods

  • mutate the object they are called for => no need to re-assign
  • return self => are intended to be chained (block.box!.v_pad!(2))

The module is not to be used directly and exists just to de-couple formatting methods from the rest.

Instance Method Summary collapse

Instance Method Details

#align!(height: nil, width: nil) ⇒ Object

Align content to the center of the specified width and height.

Parameters:

  • height (Number) (defaults to: nil) —

    target height

  • width (Number) (defaults to: nil) —

    target width



92
93
94
95
# File 'lib/tui/format.rb', line 92

def align! height: nil, width: nil
  v_align! :center, width: width
  h_align! :center, height: height
end

#box!(corners: :round) ⇒ Object

Add a square box around the block.

It auto-aligns the block, so use #v_align! beforehand! if you want custom alignment for the block



101
102
103
104
105
106
107
108
109
110
# File 'lib/tui/format.rb', line 101

def box! corners: :round
  corners = Assets::CORNERS[corners]
  lines = Assets::LINES[:single]
  v_align!
  @array.collect! { |line| "#{lines[0]}#{line}#{lines[0]}" }
  @array.unshift "#{corners[0]}#{lines[1] * width}#{corners[1]}"
  @array << "#{corners[2]}#{lines[1] * width}#{corners[3]}"
  @width += 2
  self
end

#fit!(width: nil, height: nil, fill: false) ⇒ Object

Fit the current block to a rectangle by cropping the block and adding a special markers to its content.

Actual content width and height will be 1 char less to store cropping symbols too.

Filling does not align content, #v_align! does.

Parameters:

  • width (defaults to: nil) —

    width to fit, nil => don't touch width

  • height (defaults to: nil) —

    height to fit, nil => don't touch height

  • fill (defaults to: false) —

    whether to fill Block to be of the size of the box



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/tui/format.rb', line 122

def fit! width: nil, height: nil, fill: false
  # pre-calc width to use below
  @width = width unless width.nil? || (@width < width && !fill)

  unless height.nil?
    if @array.size > height
      @array.slice!((height - 1)..)
      @array << ('░' * @width)
    elsif fill && @array.size < height
      @array += Array.new(height - @array.size, ' ' * @width)
    end
  end
  unless width.nil?
    collect! { |line| crop_line line, width, fill }
  end
  self
end

#h_align!(type = :top, height: @height) ⇒ Object

Aligns block elements horisontally (by height) by adding spaces.

New lines get added to the block to have the specified # of lines in total.

Parameters:

  • type (Symbol) (defaults to: :top) —

    :top, :center, :bottom

  • height (Integer) (defaults to: @height) —

    target block height, is ignored if less than @width (look at #fit!)



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/tui/format.rb', line 71

def h_align! type = :top, height: @height
  return self if height.nil? || @array.size > height

  extra_lines_count = height - @array.size
  case type
  when :center
    (extra_lines_count/2).times { @array.prepend ' ' }
    (extra_lines_count/2 + (extra_lines_count.odd? ? 1 : 0)).times { @array.append ' ' }
  when :top
    (extra_lines_count).times { @array.append ' ' }
  else # :bottom
    (extra_lines_count).times { @array.prepend ' ' }
  end

  self
end

#h_pad!(size = 1) ⇒ Object

Add horizontal (to the sides) padding to the block.

Parameters:

  • size (defaults to: 1) —

    number of spaces to add



16
17
18
19
20
21
22
# File 'lib/tui/format.rb', line 16

def h_pad! size = 1
  collect! { |line|
    "#{' ' * size}#{line}#{' ' * size}"
  }
  @width += size * 2
  self
end

#pad!(size = 1) ⇒ Object

Adds spaces around the block

Parameters:

  • size (defaults to: 1) —

    number of spaces to add



37
38
39
40
41
# File 'lib/tui/format.rb', line 37

def pad! size = 1
  # order matters!
  v_pad! size
  h_pad! size
end

#v_align!(type = :left, width: @width) ⇒ Object

Aligns block elements vertically (by width) by adding spaces.

Lines (rows) within the block can be of uneven width. This method changes all lines to have the same # of chars

Parameters:

  • type (Symbol) (defaults to: :left) —

    :left, :center, :right

  • width (Integer) (defaults to: @width) —

    target block width, is ignored if less than @width (look at #fit!)



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tui/format.rb', line 49

def v_align! type = :left, width: @width
  line_transformer = case type
                     when :center
                       ->(line, num_spaces) { ' ' * (num_spaces / 2) + line.to_s + (' ' * (num_spaces / 2)) + (num_spaces.odd? ? ' ' : '') }
                     when :right
                       ->(line, num_spaces) { (' ' * num_spaces) + line.to_s }
                     else # :left
                       ->(line, num_spaces) { line.to_s + (' ' * num_spaces) }
                     end

  return self if width.nil? || @width > width # == case makes all lines width even

  @width = width
  @array.collect! { |line| line_transformer.call line, @width - Tools.calc_width(line) }
  self
end

#v_pad!(size = 1) ⇒ Object

Add vertical padding (before-after) to the block

Parameters:

  • size (defaults to: 1) —

    number of spaces to add



26
27
28
29
30
31
32
33
# File 'lib/tui/format.rb', line 26

def v_pad! size = 1
  filler = ' ' * @width
  size.times {
    self >> filler
    self << filler
  }
  self
end