Class: PassStation::Output::PrettyTable

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

Overview

Pretty table with ASCII borders formatter

Class Method Summary collapse

Methods inherited from Table

colsize_count, colsizes_count, correct_min_colsizes

Class Method Details

.dividers(colsizes) ⇒ String

Generate dividers

Parameters:

  • colsizes (Hash)

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

Returns:

  • (String)

    divider line



188
189
190
# File 'lib/pass_station/output.rb', line 188

def dividers(colsizes)
  "+#{colsizes.map { |_, cs| '-' * (cs + 1) }.join('+')}+"
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



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/pass_station/output.rb', line 152

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

.headers(colsizes) ⇒ String

Generate justified headers

Parameters:

  • colsizes (Hash)

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

Returns:

  • (String)

    the justified headers



195
196
197
# File 'lib/pass_station/output.rb', line 195

def headers(colsizes)
  "| #{colsizes.map { |k, v| k.to_s.ljust(v - 1) }.join(' | ')} |"
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 Table.colsizes_count

Returns:

  • (String)

    the justified element



169
170
171
# File 'lib/pass_station/output.rb', line 169

def justify(row, column, colsizes)
  row[column].to_s.ljust(colsizes[column] - 1)
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 Table.colsizes_count

Returns:

  • (String)

    the justified row



177
178
179
180
181
182
183
# File 'lib/pass_station/output.rb', line 177

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