Class: Terminal::Join
- Inherits:
-
Object
- Object
- Terminal::Join
- Defined in:
- lib/terminal/join.rb,
lib/terminal/join/version.rb
Constant Summary collapse
- VERSION =
'1.0.1'
Class Method Summary collapse
- .horizontal(*strings, separator: nil, align: :center, &block) ⇒ Object
- .style(string, *ansi, halign: :center, valign: :center, width: 0, height: 0, margin: 0) ⇒ Object
- .vertical(*strings, separator: nil, align: :center, &block) ⇒ Object
Class Method Details
.horizontal(*strings, separator: nil, align: :center, &block) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/terminal/join.rb', line 45 def self.horizontal(*strings, separator: nil, align: :center, &block) max_height = strings.flatten.map { _1.to_s.lines.size }.max separator = block.call max_height if block if separator.nil? strings.flatten else strings.flat_map { [_1, separator] }[0...-1] end .map { self.style _1.to_s, halign: align, height: max_height } .map { _1.lines.map(&:chomp) } .transpose.map(&:join).join("\n") end |
.style(string, *ansi, halign: :center, valign: :center, width: 0, height: 0, margin: 0) ⇒ Object
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 42 43 |
# File 'lib/terminal/join.rb', line 10 def self.style(string, *ansi, halign: :center, valign: :center, width: 0, height: 0, margin: 0) margin = [margin, margin] unless margin.is_a?(Array) widths = Paint.unpaint(string).lines.map { Unicode::DisplayWidth.of _1.chomp } max_width = [width, *widths].max pad_width = max_width - widths.max + margin[0] * 2 pad_height = [(height - string.lines.size), 0].max + margin[1] * 2 string .lines .map.with_index do |line, index| (' ' * pad_width) .insert( { left: margin[0], right: -1 - margin[0], center: pad_width / 2 }[valign], Paint[line.chomp, *ansi] + Paint[' ' * (widths.max - widths[index]), *ansi || nil] ) end .then do ([' ' * max_width] * pad_height) .insert( { top: margin[1], bottom: -1 - margin[1], center: pad_height / 2 }[halign], *_1 ) end .join("\n") end |
.vertical(*strings, separator: nil, align: :center, &block) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/terminal/join.rb', line 59 def self.vertical(*strings, separator: nil, align: :center, &block) Paint.unpaint(strings.flatten.join("\n")) .lines.map { Unicode::DisplayWidth.of _1 }.max => max_width separator = block.call max_width if block if separator.nil? strings.flatten else strings.flat_map { [_1, separator] }[0...-1] end .map { self.style _1.to_s, valign: align, width: max_width } .join("\n") end |