Class: Legion::CLI::Output::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/cli/output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json: false, color: true) ⇒ Formatter

Returns a new instance of Formatter.



73
74
75
76
# File 'lib/legion/cli/output.rb', line 73

def initialize(json: false, color: true)
  @json_mode = json
  @color_enabled = color && $stdout.tty? && !json
end

Instance Attribute Details

#color_enabledObject (readonly)

Returns the value of attribute color_enabled.



71
72
73
# File 'lib/legion/cli/output.rb', line 71

def color_enabled
  @color_enabled
end

#json_modeObject (readonly)

Returns the value of attribute json_mode.



71
72
73
# File 'lib/legion/cli/output.rb', line 71

def json_mode
  @json_mode
end

Instance Method Details



106
107
108
# File 'lib/legion/cli/output.rb', line 106

def banner(version: nil)
  puts Theme.render_banner(version: version, color: @color_enabled)
end

#bold(text) ⇒ Object



84
85
86
87
88
# File 'lib/legion/cli/output.rb', line 84

def bold(text)
  return text.to_s unless @color_enabled

  "#{COLORS[:bold]}#{COLORS[:heading]}#{text}#{COLORS[:reset]}"
end

#colorize(text, color) ⇒ Object



78
79
80
81
82
# File 'lib/legion/cli/output.rb', line 78

def colorize(text, color)
  return text.to_s unless @color_enabled

  "#{COLORS[color]}#{text}#{COLORS[:reset]}"
end

#detail(hash, indent: 0) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/legion/cli/output.rb', line 120

def detail(hash, indent: 0)
  if @json_mode
    puts Output.encode_json(hash)
    return
  end

  pad = ' ' * indent
  max_key = hash.keys.map { |k| k.to_s.length }.max || 0

  hash.each do |key, value|
    label = colorize("#{key.to_s.ljust(max_key)}:", :label)
    val = case value
          when true  then colorize('yes', :accent)
          when false then colorize('no', :muted)
          when nil   then colorize('(none)', :disabled)
          else value.to_s
          end
    puts "#{pad}  #{label} #{val}"
  end
end

#dim(text) ⇒ Object



90
91
92
93
94
# File 'lib/legion/cli/output.rb', line 90

def dim(text)
  return text.to_s unless @color_enabled

  "#{COLORS[:gray]}#{text}#{COLORS[:reset]}"
end

#error(message) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/legion/cli/output.rb', line 190

def error(message)
  if @json_mode
    puts Output.encode_json(error: true, message: message)
  else
    warn "  #{colorize('»', :critical)} #{colorize(message, :critical)}"
  end
end

#header(text) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/legion/cli/output.rb', line 110

def header(text)
  return if @json_mode

  if @color_enabled
    puts "#{COLORS[:bold]}#{COLORS[:heading]}#{text}#{COLORS[:reset]}"
  else
    puts text
  end
end

#info(message) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/legion/cli/output.rb', line 182

def info(message)
  if @json_mode
    puts Output.encode_json(info: true, message: message)
  else
    puts "  #{colorize('»', :accent)} #{message}"
  end
end

#json(data) ⇒ Object



198
199
200
# File 'lib/legion/cli/output.rb', line 198

def json(data)
  puts Output.encode_json(data)
end

#spacerObject



202
203
204
# File 'lib/legion/cli/output.rb', line 202

def spacer
  puts unless @json_mode
end

#status(text) ⇒ Object



102
103
104
# File 'lib/legion/cli/output.rb', line 102

def status(text)
  colorize(text, status_color(text))
end

#status_color(status) ⇒ Object



96
97
98
99
100
# File 'lib/legion/cli/output.rb', line 96

def status_color(status)
  key = status.to_s.downcase.tr('.', '_').to_sym
  color_name = STATUS_ICONS[key] || 'disabled'
  color_name.to_sym
end

#success(message) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/legion/cli/output.rb', line 166

def success(message)
  if @json_mode
    puts Output.encode_json(success: true, message: message)
  else
    puts "  #{colorize('»', :accent)} #{message}"
  end
end

#table(headers, rows, title: nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/legion/cli/output.rb', line 141

def table(headers, rows, title: nil)
  if @json_mode
    json_rows = rows.map { |row| headers.zip(row).to_h }
    puts Output.encode_json(title ? { title: title, data: json_rows } : json_rows)
    return
  end

  return puts dim('  (no results)') if rows.empty?

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

  puts if title
  header_line = headers.each_with_index.map { |h, i| colorize(h.to_s.upcase.ljust(widths[i]), :heading) }.join('  ')
  puts "  #{header_line}"
  puts "  #{widths.map { |w| colorize('' * w, :border) }.join('  ')}"

  rows.each do |row|
    line = row.each_with_index.map { |cell, i| cell.to_s.ljust(widths[i]) }.join('  ')
    puts "  #{line}"
  end
end

#warn(message) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/legion/cli/output.rb', line 174

def warn(message)
  if @json_mode
    puts Output.encode_json(warning: true, message: message)
  else
    puts "  #{colorize('»', :caution)} #{message}"
  end
end