lib/td/command/common.rb in td-0.10.95 vs lib/td/command/common.rb in td-0.10.96
- old
+ new
@@ -35,12 +35,52 @@
def get_ssl_client(opts={})
opts[:ssl] = true
get_client(opts)
end
+ def set_render_format_option(op)
+ def op.render_format
+ @_render_format
+ end
+ op.on('-f', '--format FORMAT', 'format of the result rendering (tsv, csv, json or table. default is table)') {|s|
+ unless ['tsv', 'csv', 'json', 'table'].include?(s)
+ raise "Unknown format #{s.dump}. Supported format: tsv, csv, json, table"
+ end
+ op.instance_variable_set(:@_render_format, s)
+ }
+ end
+
def cmd_render_table(rows, *opts)
require 'hirb'
- Hirb::Helpers::Table.render(rows, *opts)
+
+ options = opts.first
+ format = options.delete(:render_format)
+
+ case format
+ when 'csv', 'tsv'
+ require 'csv'
+ headers = options[:fields]
+ csv_opts = {}
+ csv_opts[:col_sep] = "\t" if format == 'tsv'
+ CSV.generate('', csv_opts) { |csv|
+ csv << headers
+ rows.each { |row|
+ r = []
+ headers.each { |field|
+ r << row[field]
+ }
+ csv << r
+ }
+ }
+ when 'json'
+ require 'yajl'
+
+ Yajl.dump(rows)
+ when 'table'
+ Hirb::Helpers::Table.render(rows, *opts)
+ else
+ Hirb::Helpers::Table.render(rows, *opts)
+ end
end
def normalized_message
<<EOS
Your event has large number larger than 2^64.