Class: Arrow::TableFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/arrow/table-formatter.rb

Overview

TODO: Almost codes should be implemented in Apache Arrow C++.

Direct Known Subclasses

TableListFormatter, TableTableFormatter

Instance Method Summary collapse

Constructor Details

#initialize(table, options = {}) ⇒ TableFormatter

Returns a new instance of TableFormatter.



21
22
23
24
# File 'lib/arrow/table-formatter.rb', line 21

def initialize(table, options={})
  @table = table
  @options = options
end

Instance Method Details

#formatObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/arrow/table-formatter.rb', line 26

def format
  text = ""
  columns = @table.columns
  format_header(text, columns)

  n_rows = @table.n_rows
  return text if n_rows.zero?

  border = @options[:border] || 10
  n_digits = (Math.log10(n_rows) + 1).truncate
  head_limit = [border, n_rows].min
  head_column_values = columns.collect do |column|
    column.each.take(head_limit)
  end
  format_rows(text,
              columns,
              head_column_values.transpose,
              n_digits,
              0)
  return text if n_rows <= border

  tail_start = [border, n_rows - border].max
  tail_limit = n_rows - tail_start
  tail_column_values = columns.collect do |column|
    column.reverse_each.take(tail_limit).reverse
  end

  if head_limit != tail_start
    format_ellipsis(text)
  end

  format_rows(text,
              columns,
              tail_column_values.transpose,
              n_digits,
              tail_start)

  text
end