module SycLink::Formatter
Methods to print data in a formatted way
Methods to print data in a formatted way
Public Instance Methods
Cuts the string down to the specified size
# File lib/syclink/formatter.rb, line 110 def cut(string, size) string[0..size-1] end
Extracts the columns to display in the table based on the header column names
# File lib/syclink/formatter.rb, line 59 def extract_columns(rows, header) columns = [] header.each do |h| columns << rows.map do |r| r.send(h) end end columns end
Creates a formatter string based on the widths and the column separator
# File lib/syclink/formatter.rb, line 88 def formatter_string(widhts, separator) widhts.map do |width| "%-#{width}.#{width}s" end.join(separator) end
Determines max column widths for each column based on the data and header columns.
# File lib/syclink/formatter.rb, line 71 def max_column_widths(columns, header, opts = {}) row_column_widths = columns.map do |c| c.reduce(0) { |m, v| [m, v.nil? ? 0 : v.length].max } end header_column_widths = header.map { |h| h.length } row_column_widths = header_column_widths if row_column_widths.empty? widths = row_column_widths.zip(header_column_widths).map do |column| column.reduce(0) { |m, v| [m, v].max } end widths.empty? ? [] : scale_widths(widths, opts) end
Prints the table's header
# File lib/syclink/formatter.rb, line 95 def print_header(header, formatter) puts sprintf(formatter, *header) end
Prints a horizontal line below the header
# File lib/syclink/formatter.rb, line 100 def print_horizontal_line(line, separator, widths) puts widths.map { |width| line * width }.join(separator) end
Prints columns in a table format
# File lib/syclink/formatter.rb, line 105 def print_table(columns, formatter) columns.transpose.each { |row| puts sprintf(formatter, *row) } end
Scales the widths in regard to opts and opts. If :expand is true and :width is set the rows are expanded to the :width if the rows are shorter than width. If the rows are larger than :width the rows are scaled to not exceed the :width. If :width is not set the rows are not scaled.
# File lib/syclink/formatter.rb, line 119 def scale_widths(widths, opts = {}) return widths unless opts[:width] row_width = widths.inject(:+) return widths if !opts[:expand] && row_width <= opts[:width] scale = 1.0*opts[:width]/row_width widths.map { |width| (scale * width).round } end
Based on the rows (an array of Links) provided and the header values a table is printed. If the options :expand and or :width are specified the rows are scaled accordingly. If :expand is false the rows will be cut so they fit the :width. Otherwise if the rows are less than :width the rows are expanded to :width.
Example
¶ ↑
table(rows, header, width: 80, expand: true)
Params
¶ ↑
- rows
-
array of Links with row values
- header
-
array of string with header values
- width
-
width of the table
- expand
-
whether to expand the table to width if rows are less than width
# File lib/syclink/formatter.rb, line 23 def table(rows, header, opts = {}) columns = extract_columns(rows, header) widths = max_column_widths(columns, header, opts) formatter = formatter_string(widths, " | ") print_header(header, formatter) print_horizontal_line("-", "-+-", widths) print_table(columns, formatter) end
Based on the rows (an array of values) provided and the header values a table is printed. If the options :expand and or :width are specified the rows are scaled accordingly. If :expand is false the rows will be cut so they fit the :width. Otherwise if the rows are less than :width the rows are expanded to :width.
Example
¶ ↑
table(rows, header, width: 80, expand: true)
Params
¶ ↑
- rows
-
array of row values
- header
-
array of string with header values
- width
-
width of the table
- expand
-
whether to expand the table to width if rows are less than width
# File lib/syclink/formatter.rb, line 48 def table_of_array(rows, header, opts = {}) columns = rows.transpose widths = max_column_widths(columns, header, opts) formatter = formatter_string(widths, " | ") print_header(header, formatter) print_horizontal_line("-", "-+-", widths) print_table(columns, formatter) end