Class: ColorfulTables::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/colorful_tables/table.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :border_color => :light_blue
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ Table

Returns a new instance of Table.



15
16
17
18
19
20
21
22
23
24
# File 'lib/colorful_tables/table.rb', line 15

def initialize(data, options = {})
  #raise RuntimeError, "data must be an array of columns" unless is_an_array_of_columns?(data)
  @data            = data
  @options         = options.reverse_merge!(DEFAULT_OPTIONS)
  @border_chars    ||= calculate_border_chars
  @columns_width   ||= calculate_columns_width
  @columns_options ||= calculate_columns_options
  @terminal_height ||= ColorfulTables.detect_terminal_size[1]
  @transposed_data ||= self.data.map(&:data).transpose
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



13
14
15
# File 'lib/colorful_tables/table.rb', line 13

def data
  @data
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/colorful_tables/table.rb', line 13

def options
  @options
end

Instance Method Details

#page(n) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/colorful_tables/table.rb', line 26

def page(n)
  return nil if n >= total_pages
  page_output = @transposed_data[page_ranges[n]].inject("\r" + header) do |output, row|
    output += format(row)
  end
  page_output += separator
  page_output
end

#paginateObject



35
36
37
38
39
40
41
# File 'lib/colorful_tables/table.rb', line 35

def paginate
  total_pages.times do |n|
    puts page(n)
    print "-- Press ENTER --"
    STDIN.getc
  end
end