Module: ExcADG::Tui::Format

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

Overview

collection of low-level Block formatting methods; most methods mutate an object they called for to avoid re-copying data each time; the module isn’t expected to be used directly and exists just to de-couple formatting methods and basic blocks positioning (see Block); all methods could be chained: ‘block.v_pad!(10).h_pad!(2).box!`

Instance Method Summary collapse

Instance Method Details

#box!(corners: :round) ⇒ Object

adds a square box around the block; auto-aligns the block, so use #v_align! if you want custom alignment for the block



60
61
62
63
64
65
66
67
68
# File 'lib/excadg/tui/format.rb', line 60

def box! corners: :round
  corners = Assets::CORNERS[corners]
  v_align!
  @array.collect! { |line| "#{line}" }
  @array.unshift "#{corners[0]}#{'' * width}#{corners[1]}"
  @array << "#{corners[2]}#{'' * 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 content; actual content width and height will be 1 char less to store cropping symbols; filling does not align content, #v_align! does

Parameters:

  • width (defaults to: nil)

    width to fit, nil means don’t touch width

  • height (defaults to: nil)

    height to fit, nil means don’t touch height

  • fill (defaults to: false)

    whether to fill column for the sizes provided



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/excadg/tui/format.rb', line 77

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?
    @array.collect! { |line|
      if line.size > width
        "#{line[...(width - 1)]}"
      elsif fill && line.size < width
        line << ' ' * (width - line.size)
      else
        line
      end
    }
  end
  self
end

#h_pad!(size = 1) ⇒ Object

add horizontal padding to the block

Parameters:

  • size (defaults to: 1)

    number of spaces to add



12
13
14
15
16
17
18
# File 'lib/excadg/tui/format.rb', line 12

def h_pad! size = 1
  @array.collect! { |row|
    "#{' ' * size}#{row}#{' ' * 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



33
34
35
36
# File 'lib/excadg/tui/format.rb', line 33

def pad! size = 1
  v_pad! size
  h_pad! size
end

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

aligns block elements vertically by adding spaces; all lines in block gets changed to have the same # of chars

Parameters:

  • type (Symbol) (defaults to: nil)

    :left (default), :center, :right

  • width (Integer) (defaults to: nil)

    target block width, defaults to the current width



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/excadg/tui/format.rb', line 42

def v_align! type = nil, width: nil
  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

  @width = width unless width.nil? || @width > width
  @array.collect! { |line| line_transformer.call line, @width - line.size }
  self
end

#v_pad!(size = 1) ⇒ Object

add vertical padding to the block

Parameters:

  • size (defaults to: 1)

    number of spaces to add



22
23
24
25
26
27
28
29
# File 'lib/excadg/tui/format.rb', line 22

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