Module: Chamomile::Join

Defined in:
lib/chamomile/styling/join.rb

Class Method Summary collapse

Class Method Details

.horizontal(position, *strs) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chamomile/styling/join.rb', line 6

def horizontal(position, *strs)
  strs = strs.flatten
  return "" if strs.empty?

  blocks = strs.map { |s| s.split("\n", -1) }
  max_height = blocks.map(&:length).max

  # Equalize heights using position for vertical alignment
  blocks = blocks.map do |lines|
    if lines.length < max_height
      Align.vertical(lines, max_height, position)
    else
      lines
    end
  end

  # Find max width of each block
  widths = blocks.map do |lines|
    lines.map { |l| ANSI.printable_width(l) }.max || 0
  end

  # Join line by line
  (0...max_height).map do |row|
    blocks.each_with_index.map do |lines, idx|
      line = lines[row] || ""
      # Pad all blocks except the last to their max width
      if idx < blocks.length - 1
        line_width = ANSI.printable_width(line)
        pad = widths[idx] - line_width
        pad.positive? ? "#{line}#{" " * pad}" : line
      else
        line
      end
    end.join
  end.join("\n")
end

.vertical(position, *strs) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chamomile/styling/join.rb', line 43

def vertical(position, *strs)
  strs = strs.flatten
  return "" if strs.empty?

  blocks = strs.map { |s| s.split("\n", -1) }

  # Find max width across all blocks
  max_width = blocks.flat_map { |lines| lines.map { |l| ANSI.printable_width(l) } }.max || 0

  # Align each block's lines horizontally
  all_lines = blocks.flat_map do |lines|
    Align.horizontal(lines, max_width, position)
  end

  all_lines.join("\n")
end