Module: SycLink::Formatter

Included in:
Website
Defined in:
lib/syclink/formatter.rb

Overview

Methods to print data in a formatted way

Instance Method Summary collapse

Instance Method Details

#cut(string, size) ⇒ Object

Cuts the string down to the specified size



110
111
112
# File 'lib/syclink/formatter.rb', line 110

def cut(string, size)
  string[0..size-1]
end

#extract_columns(rows, header) ⇒ Object

Extracts the columns to display in the table based on the header column names



59
60
61
62
63
64
65
66
67
# 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

#formatter_string(widhts, separator) ⇒ Object

Creates a formatter string based on the widths and the column separator



88
89
90
91
92
# File 'lib/syclink/formatter.rb', line 88

def formatter_string(widhts, separator)
  widhts.map do |width|
    "%-#{width}.#{width}s"
  end.join(separator)
end

#max_column_widths(columns, header, opts = {}) ⇒ Object

Determines max column widths for each column based on the data and header columns.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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



95
96
97
# File 'lib/syclink/formatter.rb', line 95

def print_header(header, formatter)
  puts sprintf(formatter, *header)
end

Prints a horizontal line below the header



100
101
102
# 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



105
106
107
# File 'lib/syclink/formatter.rb', line 105

def print_table(columns, formatter)
  columns.transpose.each { |row| puts sprintf(formatter, *row) }
end

#scale_widths(widths, opts = {}) ⇒ Object

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.



119
120
121
122
123
124
125
126
127
128
# 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

#table(rows, header, opts = {}) ⇒ Object

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



23
24
25
26
27
28
29
30
# 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

#table_of_array(rows, header, opts = {}) ⇒ Object

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



48
49
50
51
52
53
54
55
# 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