Class Terminal::Table
In: lib/terminal-table/cell.rb
lib/terminal-table/separator.rb
lib/terminal-table/table_helper.rb
lib/terminal-table/version.rb
lib/terminal-table/table.rb
lib/terminal-table/style.rb
lib/terminal-table/row.rb
lib/terminal-table/cell.rb
lib/terminal-table/table.rb
lib/terminal-table/table_helper.rb
lib/terminal-table/version.rb
lib/terminal-table/row.rb
lib/terminal-table/separator.rb
lib/terminal-table/style.rb
Parent: Object

Methods

Classes and Modules

Module Terminal::Table::TableHelper
Class Terminal::Table::Cell
Class Terminal::Table::Row
Class Terminal::Table::Separator
Class Terminal::Table::Style

Constants

VERSION = '1.4.4'
VERSION = '1.4.4'

Attributes

headings  [R] 
headings  [R] 
title  [R] 
title  [R] 

Public Class methods

Generates a ASCII table with the given options.

[Source]

    # File lib/terminal-table/table.rb, line 11
11:     def initialize options = {}, &block
12:       @column_widths = []
13:       self.style = options.fetch :style, {}
14:       self.title = options.fetch :title, nil
15:       self.headings = options.fetch :headings, []
16:       self.rows = options.fetch :rows, []
17:       yield_or_eval(&block) if block
18:     end

Generates a ASCII table with the given options.

[Source]

    # File lib/terminal-table/table.rb, line 11
11:     def initialize options = {}, &block
12:       @column_widths = []
13:       self.style = options.fetch :style, {}
14:       self.title = options.fetch :title, nil
15:       self.headings = options.fetch :headings, []
16:       self.rows = options.fetch :rows, []
17:       yield_or_eval(&block) if block
18:     end

Public Instance methods

<<(array)

Alias for add_row

<<(array)

Alias for add_row

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

[Source]

     # File lib/terminal-table/table.rb, line 154
154:     def == other
155:       if other.respond_to? :render and other.respond_to? :rows
156:         self.headings == other.headings and self.rows == other.rows
157:       end
158:     end

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

[Source]

     # File lib/terminal-table/table.rb, line 154
154:     def == other
155:       if other.respond_to? :render and other.respond_to? :rows
156:         self.headings == other.headings and self.rows == other.rows
157:       end
158:     end

Add a row.

[Source]

    # File lib/terminal-table/table.rb, line 34
34:     def add_row array
35:       row = array == :separator ? Separator.new(self) : Row.new(self, array)
36:       @rows << row
37:       recalc_column_widths row
38:     end

Add a row.

[Source]

    # File lib/terminal-table/table.rb, line 34
34:     def add_row array
35:       row = array == :separator ? Separator.new(self) : Row.new(self, array)
36:       @rows << row
37:       recalc_column_widths row
38:     end

Add a separator.

[Source]

    # File lib/terminal-table/table.rb, line 44
44:     def add_separator
45:       self << :separator
46:     end

Add a separator.

[Source]

    # File lib/terminal-table/table.rb, line 44
44:     def add_separator
45:       self << :separator
46:     end

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

[Source]

    # File lib/terminal-table/table.rb, line 23
23:     def align_column n, alignment
24:       r = rows
25:       column(n).each_with_index do |col, i|
26:         cell = r[i][n]
27:         cell.alignment = alignment unless cell.alignment?
28:       end
29:     end

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

[Source]

    # File lib/terminal-table/table.rb, line 23
23:     def align_column n, alignment
24:       r = rows
25:       column(n).each_with_index do |col, i|
26:         cell = r[i][n]
27:         cell.alignment = alignment unless cell.alignment?
28:       end
29:     end

[Source]

    # File lib/terminal-table/table.rb, line 52
52:     def cell_padding
53:       style.padding_left + style.padding_right
54:     end

[Source]

    # File lib/terminal-table/table.rb, line 52
52:     def cell_padding
53:       style.padding_left + style.padding_right
54:     end

[Source]

    # File lib/terminal-table/table.rb, line 48
48:     def cell_spacing
49:       cell_padding + style.border_y.length
50:     end

[Source]

    # File lib/terminal-table/table.rb, line 48
48:     def cell_spacing
49:       cell_padding + style.border_y.length
50:     end

Return column n.

[Source]

    # File lib/terminal-table/table.rb, line 59
59:     def column n, method = :value, array = rows
60:       array.map { |row| 
61:         cell = row[n]
62:         cell && method ? cell.__send__(method) : cell
63:       }.compact 
64:     end

Return column n.

[Source]

    # File lib/terminal-table/table.rb, line 59
59:     def column n, method = :value, array = rows
60:       array.map { |row| 
61:         cell = row[n]
62:         cell && method ? cell.__send__(method) : cell
63:       }.compact 
64:     end

Return length of column n.

[Source]

    # File lib/terminal-table/table.rb, line 83
83:     def column_width n
84:       width = @column_widths[n] || 0
85:       width + additional_column_widths[n].to_i
86:     end

Return length of column n.

[Source]

    # File lib/terminal-table/table.rb, line 83
83:     def column_width n
84:       width = @column_widths[n] || 0
85:       width + additional_column_widths[n].to_i
86:     end

Return n column including headings.

[Source]

    # File lib/terminal-table/table.rb, line 69
69:     def column_with_headings n, method = :value
70:       column n, method, headings_with_rows
71:     end

Return n column including headings.

[Source]

    # File lib/terminal-table/table.rb, line 69
69:     def column_with_headings n, method = :value
70:       column n, method, headings_with_rows
71:     end

Return columns.

[Source]

    # File lib/terminal-table/table.rb, line 76
76:     def columns
77:       (0...number_of_columns).map { |n| column n } 
78:     end

Return columns.

[Source]

    # File lib/terminal-table/table.rb, line 76
76:     def columns
77:       (0...number_of_columns).map { |n| column n } 
78:     end

Set the headings

[Source]

     # File lib/terminal-table/table.rb, line 99
 99:     def headings= array
100:       @headings = Row.new(self, array)
101:       recalc_column_widths @headings
102:     end

Set the headings

[Source]

     # File lib/terminal-table/table.rb, line 99
 99:     def headings= array
100:       @headings = Row.new(self, array)
101:       recalc_column_widths @headings
102:     end
length_of_column(n)

Alias for column_width

length_of_column(n)

Alias for column_width

Return total number of columns available.

[Source]

    # File lib/terminal-table/table.rb, line 92
92:     def number_of_columns
93:       headings_with_rows.map { |r| r.cells.size }.max
94:     end

Return total number of columns available.

[Source]

    # File lib/terminal-table/table.rb, line 92
92:     def number_of_columns
93:       headings_with_rows.map { |r| r.cells.size }.max
94:     end

Render the table.

[Source]

     # File lib/terminal-table/table.rb, line 107
107:     def render
108:       separator = Separator.new(self)
109:       buffer = [separator]
110:       unless @title.nil?
111:         opts = {:value => @title, :alignment => :center, :colspan => number_of_columns}
112:         buffer << Row.new(self, [opts])
113:         buffer << separator
114:       end
115:       unless @headings.cells.empty?
116:         buffer << @headings
117:         buffer << separator
118:       end
119:       buffer += @rows
120:       buffer << separator
121:       buffer.map { |r| r.render }.join("\n")
122:     end

Render the table.

[Source]

     # File lib/terminal-table/table.rb, line 107
107:     def render
108:       separator = Separator.new(self)
109:       buffer = [separator]
110:       unless @title.nil?
111:         opts = {:value => @title, :alignment => :center, :colspan => number_of_columns}
112:         buffer << Row.new(self, [opts])
113:         buffer << separator
114:       end
115:       unless @headings.cells.empty?
116:         buffer << @headings
117:         buffer << separator
118:       end
119:       buffer += @rows
120:       buffer << separator
121:       buffer.map { |r| r.render }.join("\n")
122:     end

Return rows without separator rows.

[Source]

     # File lib/terminal-table/table.rb, line 128
128:     def rows
129:       @rows.reject { |row| row.is_a? Separator }
130:     end

Return rows without separator rows.

[Source]

     # File lib/terminal-table/table.rb, line 128
128:     def rows
129:       @rows.reject { |row| row.is_a? Separator }
130:     end

[Source]

     # File lib/terminal-table/table.rb, line 132
132:     def rows= array
133:       @rows = []
134:       array.each { |arr| self << arr }
135:     end

[Source]

     # File lib/terminal-table/table.rb, line 132
132:     def rows= array
133:       @rows = []
134:       array.each { |arr| self << arr }
135:     end

[Source]

     # File lib/terminal-table/table.rb, line 141
141:     def style
142:       @style ||= Style.new
143:     end

[Source]

     # File lib/terminal-table/table.rb, line 141
141:     def style
142:       @style ||= Style.new
143:     end

[Source]

     # File lib/terminal-table/table.rb, line 137
137:     def style=(options)
138:       style.apply options
139:     end

[Source]

     # File lib/terminal-table/table.rb, line 137
137:     def style=(options)
138:       style.apply options
139:     end

[Source]

     # File lib/terminal-table/table.rb, line 145
145:     def title=(title)
146:       @title = title
147:       recalc_column_widths Row.new(self, [title])
148:     end

[Source]

     # File lib/terminal-table/table.rb, line 145
145:     def title=(title)
146:       @title = title
147:       recalc_column_widths Row.new(self, [title])
148:     end
to_s()

Alias for render

to_s()

Alias for render

[Validate]