Class: SNMPTableViewer::Formatter::Table

Inherits:
SNMPTableViewer::Formatter show all
Defined in:
lib/snmp_table_viewer/formatter/table.rb

Overview

Formatting class for Table output.

Instance Method Summary collapse

Methods inherited from SNMPTableViewer::Formatter

#initialize

Constructor Details

This class inherits a constructor from SNMPTableViewer::Formatter

Instance Method Details

#output(transpose: false) ⇒ String

Output the data (and headings if provided) as a table which can be displayed in the terminal.

Parameters:

  • transpose (Boolean) (defaults to: false)

    Whether to swap rows and columns - useful for displaying a wide and short table

Returns:

  • (String)

    the text to display in order to draw the table



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/snmp_table_viewer/formatter/table.rb', line 8

def output(transpose: false)
  use_headings = @headings.size > 0
  data = @data

  if transpose
    # transpose the data
    if use_headings
      data = [@headings, *data].transpose
      use_headings = false # Don't add headings twice
    else
      data = data.transpose
    end
  end

  if use_headings
    Terminal::Table.new(rows: data, headings: @headings).to_s
  else
    Terminal::Table.new(rows: data).to_s
  end
end