Class: PassStation::Output::Table

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

Overview

Simple table formatter

Direct Known Subclasses

PrettyTable

Class Method Summary collapse

Class Method Details

.colsize_count(table, column) ⇒ Integer

Calculate column size (max item size)

Parameters:

  • table (Array<CSV::Row>)
  • column (Symbol)

    the symbol of the column

Returns:

  • (Integer)

    the column size



88
89
90
# File 'lib/pass_station/output.rb', line 88

def colsize_count(table, column)
  table.map { |i| i[column].nil? ? 0 : i[column].size }.max + 1
end

.colsizes_count(table) ⇒ Hash

Calculate the size of all columns (max item size)

Parameters:

  • table (Array<CSV::Row>)

Returns:

  • (Hash)

    keys are columns name, values are columns size



95
96
97
98
99
100
# File 'lib/pass_station/output.rb', line 95

def colsizes_count(table)
  colsizes = table.first.to_h.keys.each_with_object({}) do |c, h|
    h[c] = colsize_count(table, c)
  end
  correct_min_colsizes(colsizes)
end

.correct_min_colsizes(colsizes) ⇒ Hash

Correct colsizes to be at least of the size of the headers (case when values are shorter than headers and breaks the table display)

Parameters:

  • colsizes (Hash)

    hash containing the column size for each column as returned by colsizes_count

Returns:

  • (Hash)

    fixed colsizes, keys are columns name, values are columns size



106
107
108
109
110
111
112
# File 'lib/pass_station/output.rb', line 106

def correct_min_colsizes(colsizes)
  min_colsizes = {
    productvendor: 14, username: 9, password: 9, modelsoftware_name: 19,
    version: 8, access_type: 12, privileges: 11, notes: 6, vendor: 7
  }
  colsizes.each_with_object({}) { |(k, v), h| h[k] = [v, min_colsizes[k]].max }
end

.format(table) ⇒ Array<String>

Format the Array<CSV::Row> into a simple table with justified columns

Parameters:

  • table (Array<CSV::Row>)

    an Array<CSV::Row>

Returns:

  • (Array<String>)

    the formatted table ready to be printed



74
75
76
77
78
79
80
81
82
# File 'lib/pass_station/output.rb', line 74

def format(table)
  out = []
  colsizes = colsizes_count(table)
  out.push(headers(colsizes))
  table.each do |r|
    out.push(justify_row(r, colsizes))
  end
  out
end

.headers(colsizes) ⇒ String

Generate justified headers

Parameters:

  • colsizes (Hash)

    hash containing the column size for each column as returned by colsizes_count

Returns:

  • (String)

    the justified headers



138
139
140
# File 'lib/pass_station/output.rb', line 138

def headers(colsizes)
  colsizes.map { |k, v| k.to_s.ljust(v) }.join.to_s
end

.justify(row, column, colsizes) ⇒ String

Left justify an element of the column

Parameters:

  • row (CSV::Row)

    CSV::Row

  • column (Symbol)

    the symbol of the column

  • colsizes (Hash)

    hash containing the column size for each column as returned by colsizes_count

Returns:

  • (String)

    the justified element



119
120
121
# File 'lib/pass_station/output.rb', line 119

def justify(row, column, colsizes)
  row[column].to_s.ljust(colsizes[column])
end

.justify_row(row, colsizes) ⇒ String

Left justify all elements of the column

Parameters:

  • row (CSV::Row)

    CSV::Row

  • colsizes (Hash)

    hash containing the column size for each column as returned by colsizes_count

Returns:

  • (String)

    the justified row



127
128
129
130
131
132
133
# File 'lib/pass_station/output.rb', line 127

def justify_row(row, colsizes)
  out = ''
  row.to_h.each_key do |col|
    out += justify(row, col, colsizes)
  end
  out
end