Module: Wokku::Output
- Defined in:
- lib/wokku/output.rb
Class Method Summary collapse
- .puts_json(data) ⇒ Object
-
.render(data) ⇒ Object
Smart printer for read commands: prints raw API response as JSON when the global –json flag is set; otherwise yields to the human formatter.
-
.status(message) ⇒ Object
Suppressible status / hint message.
- .table(rows, headers: nil) ⇒ Object
Class Method Details
.puts_json(data) ⇒ Object
21 22 23 |
# File 'lib/wokku/output.rb', line 21 def puts_json(data) puts JSON.pretty_generate(data) end |
.render(data) ⇒ Object
Smart printer for read commands: prints raw API response as JSON when the global –json flag is set; otherwise yields to the human formatter.
27 28 29 30 31 32 33 |
# File 'lib/wokku/output.rb', line 27 def render(data) if Wokku.json? puts JSON.pretty_generate(data) else yield data end end |
.status(message) ⇒ Object
Suppressible status / hint message. Silenced by –quiet or –json. Errors should NOT use this — they go through ‘abort` which writes to stderr and is never silenced.
38 39 40 41 |
# File 'lib/wokku/output.rb', line 38 def status() return if Wokku.quiet? || Wokku.json? puts end |
.table(rows, headers: nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/wokku/output.rb', line 9 def table(rows, headers: nil) return if rows.empty? cols = (headers || rows.first.keys).map(&:to_s) widths = cols.map(&:length) rows.each { |r| cols.each_with_index { |c, i| widths[i] = [widths[i], r[c].to_s.length].max } } fmt = widths.map { |w| "%-#{w}s" }.join(" ") puts fmt % cols.map(&:upcase) puts widths.map { |w| "-" * w }.join(" ") rows.each { |r| puts fmt % cols.map { |c| r[c] } } end |