Module: ObjectTable::Printing
- Included in:
- TableMethods
- Defined in:
- lib/object_table/printing.rb
Class Method Summary collapse
- .calc_column_widths(rows, padding) ⇒ Object
- .format_column(column) ⇒ Object
- .format_rows(rows, widths) ⇒ Object
- .format_section(columns, row_slice) ⇒ Object
- .split_column_lines(name, column) ⇒ Object
Instance Method Summary collapse
Class Method Details
.calc_column_widths(rows, padding) ⇒ Object
40 41 42 43 |
# File 'lib/object_table/printing.rb', line 40 def self.calc_column_widths(rows, padding) columns = rows.transpose columns.map{|col| col.flatten.map(&:length).max + padding} end |
.format_column(column) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/object_table/printing.rb', line 28 def self.format_column(column) return column.to_a.map(&:inspect) if column.rank < 2 column.shape[-1].times.map do |i| row = column[false, i] row.inspect.partition("\n")[-1].strip end end |
.format_rows(rows, widths) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/object_table/printing.rb', line 45 def self.format_rows(rows, widths) rows = rows.flat_map do |row| height = row.map(&:length).max row.map{|cell| cell.fill('', cell.length...height)}.transpose end format = widths.to_a.map{|w| "%#{w}s"}.join rows.map{|row| format % row } end |
.format_section(columns, row_slice) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/object_table/printing.rb', line 55 def self.format_section(columns, row_slice) row_slice = row_slice.to_a numbers = split_column_lines('', row_slice.map{|i| "#{i}: "}) section = columns.map do |name, c| c = (row_slice.empty? ? NArray[] : c.slice(false, row_slice)) c = format_column(c) c = split_column_lines(name.to_s, c) end [numbers] + section end |
.split_column_lines(name, column) ⇒ Object
36 37 38 |
# File 'lib/object_table/printing.rb', line 36 def self.split_column_lines(name, column) [name, *column, name].map{|i| i.split("\n")} end |
Instance Method Details
#inspect(max_section = 5, col_padding = 2) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/object_table/printing.rb', line 4 def inspect(max_section=5, col_padding=2) header = "#{self.class}(#{nrows}, #{ncols})\n" return "#{header}(empty table)" if ncols == 0 return "#{header}(empty table with columns: #{colnames.join(", ")})" if nrows == 0 separated = (nrows > max_section * 2) max_section = (nrows / 2.0) unless separated head = Printing.format_section(columns, 0...max_section.to_i).transpose[0...-1] tail = Printing.format_section(columns, (nrows - max_section).to_i...nrows).transpose[1..-1] widths = Printing.calc_column_widths(head + tail, col_padding) rows = Printing.format_rows(head, widths) rows.push('-' * widths.reduce(:+)) if separated rows.concat Printing.format_rows(tail, widths) header + rows.join("\n") rescue NoMethodError => e raise Exception.new(e) end |