Class: RogIQ::Formatters

Inherits:
Object
  • Object
show all
Defined in:
lib/rogiq/formatters.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format: "table", quiet: false) ⇒ Formatters

Returns a new instance of Formatters.



10
11
12
13
# File 'lib/rogiq/formatters.rb', line 10

def initialize(format: "table", quiet: false)
  @format = format.to_s
  @quiet = quiet
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



8
9
10
# File 'lib/rogiq/formatters.rb', line 8

def format
  @format
end

#quietObject (readonly)

Returns the value of attribute quiet.



8
9
10
# File 'lib/rogiq/formatters.rb', line 8

def quiet
  @quiet
end

Instance Method Details

#colorize(str, code) ⇒ Object



97
98
99
100
101
# File 'lib/rogiq/formatters.rb', line 97

def colorize(str, code)
  return str unless $stdout.tty?

  "#{code}#{str}\e[0m"
end

#confirm!(prompt, yes: false) ⇒ Object



90
91
92
93
94
95
# File 'lib/rogiq/formatters.rb', line 90

def confirm!(prompt, yes: false)
  return true if yes

  print("#{prompt} [y/N] ")
  $stdin.gets.to_s.strip.downcase.start_with?("y")
end

#error_msg(msg) ⇒ Object



27
28
29
# File 'lib/rogiq/formatters.rb', line 27

def error_msg(msg)
  $stderr.puts(colorize(msg, "\e[31m"))
end

#jsonify(obj) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rogiq/formatters.rb', line 54

def jsonify(obj)
  case obj
  when Hash
    obj.transform_values { |v| jsonify(v) }
  when Array
    obj.map { |v| jsonify(v) }
  when Time, DateTime
    obj.iso8601
  when Date
    obj.iso8601
  else
    if obj.respond_to?(:iso8601)
      obj.iso8601
    else
      obj
    end
  end
end

#output(data) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rogiq/formatters.rb', line 31

def output(data)
  if data.is_a?(Hash) && data[:headers] && data[:rows]
    return table(data[:headers], data[:rows]) if format != "json"

    STDOUT.puts(JSON.pretty_generate({ headers: data[:headers], rows: data[:rows] }))
    return
  end

  if format == "json"
    STDOUT.puts(JSON.pretty_generate(jsonify(data)))
    return
  end

  case data
  when Hash
    say(JSON.pretty_generate(jsonify(data)))
  when String
    say(data)
  else
    say(data.inspect)
  end
end

#say(msg = "") ⇒ Object



15
16
17
# File 'lib/rogiq/formatters.rb', line 15

def say(msg = "")
  Kernel.puts(msg) unless quiet
end

#success(msg) ⇒ Object



19
20
21
# File 'lib/rogiq/formatters.rb', line 19

def success(msg)
  say(colorize(msg, "\e[32m"))
end

#table(headers, rows) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rogiq/formatters.rb', line 73

def table(headers, rows)
  return if quiet

  rows = rows.map { |r| r.map(&:to_s) }
  widths = headers.each_with_index.map do |h, i|
    [h.to_s.length, *rows.map { |r| r[i].to_s.length }].max
  end

  sep = widths.map { |w| "-" * w }.join("-+-")
  header_line = headers.each_with_index.map { |h, i| h.to_s.ljust(widths[i]) }.join(" | ")
  say(header_line)
  say(sep)
  rows.each do |row|
    say(row.each_with_index.map { |cell, i| cell.to_s.ljust(widths[i]) }.join(" | "))
  end
end

#warn_msg(msg) ⇒ Object



23
24
25
# File 'lib/rogiq/formatters.rb', line 23

def warn_msg(msg)
  say(colorize(msg, "\e[33m"))
end