Class: Terminal::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal-table/row.rb,
lib/terminal-table/cell.rb,
lib/terminal-table/style.rb,
lib/terminal-table/table.rb,
lib/terminal-table/version.rb,
lib/terminal-table/separator.rb,
lib/terminal-table/table_helper.rb

Defined Under Namespace

Modules: TableHelper Classes: Cell, Row, Separator, Style

Constant Summary collapse

VERSION =
'1.5.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Table

Generates a ASCII table with the given options.



10
11
12
13
14
15
16
17
# File 'lib/terminal-table/table.rb', line 10

def initialize options = {}, &block
  @column_widths = []
  self.style = options.fetch :style, {}
  self.headings = options.fetch :headings, []
  self.rows = options.fetch :rows, []
  self.title = options.fetch :title, nil
  yield_or_eval(&block) if block
end

Instance Attribute Details

#headingsObject

Returns the value of attribute headings.



5
6
7
# File 'lib/terminal-table/table.rb', line 5

def headings
  @headings
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/terminal-table/table.rb', line 4

def title
  @title
end

Instance Method Details

#==(other) ⇒ Object

Check if other is equal to self. other is considered equal if it contains the same headings and rows.



162
163
164
165
166
# File 'lib/terminal-table/table.rb', line 162

def == other
  if other.respond_to? :render and other.respond_to? :rows
    self.headings == other.headings and self.rows == other.rows
  end
end

#add_row(array) ⇒ Object Also known as: <<

Add a row.



33
34
35
36
37
# File 'lib/terminal-table/table.rb', line 33

def add_row array
  row = array == :separator ? Separator.new(self) : Row.new(self, array)
  @rows << row
  recalc_column_widths row
end

#add_separatorObject

Add a separator.



43
44
45
# File 'lib/terminal-table/table.rb', line 43

def add_separator
  self << :separator
end

#align_column(n, alignment) ⇒ Object

Align column n to the given alignment of :center, :left, or :right.



22
23
24
25
26
27
28
# File 'lib/terminal-table/table.rb', line 22

def align_column n, alignment
  r = rows
  column(n).each_with_index do |col, i|
    cell = r[i][n]
    cell.alignment = alignment unless cell.alignment?
  end
end

#cell_paddingObject



51
52
53
# File 'lib/terminal-table/table.rb', line 51

def cell_padding
  style.padding_left + style.padding_right
end

#cell_spacingObject



47
48
49
# File 'lib/terminal-table/table.rb', line 47

def cell_spacing
  cell_padding + style.border_y.length
end

#column(n, method = :value, array = rows) ⇒ Object

Return column n.



58
59
60
61
62
63
# File 'lib/terminal-table/table.rb', line 58

def column n, method = :value, array = rows
  array.map { |row|
    cell = row[n]
    cell && method ? cell.__send__(method) : cell
  }.compact
end

#column_width(n) ⇒ Object Also known as: length_of_column

Return length of column n.



82
83
84
85
# File 'lib/terminal-table/table.rb', line 82

def column_width n
  width = @column_widths[n] || 0
  width + additional_column_widths[n].to_i
end

#column_with_headings(n, method = :value) ⇒ Object

Return n column including headings.



68
69
70
# File 'lib/terminal-table/table.rb', line 68

def column_with_headings n, method = :value
  column n, method, headings_with_rows
end

#columnsObject

Return columns.



75
76
77
# File 'lib/terminal-table/table.rb', line 75

def columns
  (0...number_of_columns).map { |n| column n }
end

#number_of_columnsObject

Return total number of columns available.



91
92
93
# File 'lib/terminal-table/table.rb', line 91

def number_of_columns
  headings_with_rows.map { |r| r.cells.size }.max
end

#renderObject Also known as: to_s

Render the table.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/terminal-table/table.rb', line 110

def render
  separator = Separator.new(self)
  buffer = [separator]
  unless @title.nil?
    buffer << Row.new(self, [title_cell_options])
    buffer << separator
  end
  @headings.each do |row|
    unless row.cells.empty?
      buffer << row
      buffer << separator
    end
  end
  if style.all_separators
    buffer += @rows.product([separator]).flatten
  else
    buffer += @rows
    buffer << separator
  end
  buffer.map { |r| style.margin_left + r.render }.join("\n")
end

#rowsObject

Return rows without separator rows.



136
137
138
# File 'lib/terminal-table/table.rb', line 136

def rows
  @rows.reject { |row| row.is_a? Separator }
end

#rows=(array) ⇒ Object



140
141
142
143
# File 'lib/terminal-table/table.rb', line 140

def rows= array
  @rows = []
  array.each { |arr| self << arr }
end

#styleObject



149
150
151
# File 'lib/terminal-table/table.rb', line 149

def style
  @style ||= Style.new
end

#style=(options) ⇒ Object



145
146
147
# File 'lib/terminal-table/table.rb', line 145

def style=(options)
  style.apply options
end