Class: Fizzy::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/fizzy/formatter.rb

Constant Summary collapse

WIDE_RANGES =
[
  0x1100..0x115F, 0x2E80..0xA4CF, 0xAC00..0xD7AF, 0xF900..0xFAFF,
  0xFE10..0xFE6F, 0xFF00..0xFF60, 0x1F000..0x1FFFF, 0x20000..0x2FA1F
].freeze

Class Method Summary collapse

Class Method Details

.detail(pairs, io: $stdout) ⇒ Object



24
25
26
27
28
29
# File 'lib/fizzy/formatter.rb', line 24

def self.detail(pairs, io: $stdout)
  width = pairs.map { |k, _| k.to_s.length }.max
  pairs.each do |key, value|
    io.puts "#{key.to_s.rjust(width)}  #{value}"
  end
end

.json(data, io: $stdout) ⇒ Object



20
21
22
# File 'lib/fizzy/formatter.rb', line 20

def self.json(data, io: $stdout)
  io.puts JSON.pretty_generate(data)
end

.table(rows, headers:, io: $stdout) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fizzy/formatter.rb', line 5

def self.table(rows, headers:, io: $stdout)
  return if rows.empty?

  all_rows = [headers] + rows
  widths = headers.each_index.map do |i|
    all_rows.map { |r| display_width(r[i].to_s) }.max
  end

  io.puts headers.each_with_index.map { |h, i| display_ljust(h.to_s, widths[i]) }.join("  ")
  io.puts widths.map { |w| "-" * w }.join("  ")
  rows.each do |row|
    io.puts row.each_with_index.map { |c, i| display_ljust(c.to_s, widths[i]) }.join("  ")
  end
end

.truncate(str, max) ⇒ Object



31
32
33
34
35
# File 'lib/fizzy/formatter.rb', line 31

def self.truncate(str, max)
  return "" unless str

  str.length > max ? "#{str[0...(max - 1)]}" : str
end