Module: WideEvent::Store::Formatter

Defined in:
lib/wide_event/store/formatter.rb

Overview

Renders a bounded QueryResult for a terminal (table), for machine consumption (json), or for a spreadsheet/pipe (csv). Every format reads the whole (already-capped) result once; none of them stream or re-request data, and none of them ever emit ANSI escape sequences — the table formatter is plain text whether or not io is a TTY.

Constant Summary collapse

NULL_TEXT =
"NULL"

Class Method Summary collapse

Class Method Details

.csv(result, io:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/wide_event/store/formatter.rb', line 29

def self.csv(result, io:)
  io.print(CSV.generate_line(result.columns))
  result.rows.each do |row|
    # Pass `nil` through as-is (CSV renders it as an empty, unquoted
    # field) rather than the string "" (which CSV quotes as `""` to
    # keep it distinct from a genuinely absent field) — matching the
    # store's own CSV null rendering (query/executor.go's csvValue).
    io.print(CSV.generate_line(row.map { |value| value.nil? ? nil : value.to_s }))
  end
end

.json(result, io:) ⇒ Object



25
26
27
# File 'lib/wide_event/store/formatter.rb', line 25

def self.json(result, io:)
  io.puts(JSON.generate(columns: result.columns, rows: result.rows, truncated: result.truncated))
end

.table(result, io:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/wide_event/store/formatter.rb', line 14

def self.table(result, io:)
  header = result.columns
  body_rows = result.rows.map { |row| row.map { |value| value.nil? ? NULL_TEXT : value.to_s } }
  widths = column_widths(header, body_rows)

  io.puts(row_line(header, widths))
  io.puts(separator_line(widths))
  body_rows.each { |row| io.puts(row_line(row, widths)) }
  io.puts("(truncated: the store capped this result)") if result.truncated
end