Class: PassStation::Output::PrettyTable
- Defined in:
- lib/pass_station/output.rb
Overview
Pretty table with ASCII borders formatter
Class Method Summary collapse
-
.dividers(colsizes) ⇒ String
Generate dividers.
-
.format(table) ⇒ Array<String>
Format the
Array<CSV::Row>
into a simple table with justified columns. -
.headers(colsizes) ⇒ String
Generate justified headers.
-
.justify(row, column, colsizes) ⇒ String
Left justify an element of the column.
-
.justify_row(row, colsizes) ⇒ String
Left justify all elements of the column.
Methods inherited from Table
colsize_count, colsizes_count, correct_min_colsizes
Class Method Details
.dividers(colsizes) ⇒ String
Generate dividers
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
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
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
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
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 |