| VERSION | = | '1.4.5' |
| VERSION | = | '1.4.5' |
| headings | [R] | |
| headings | [R] | |
| title | [R] | |
| title | [R] |
Generates a ASCII table with the given options.
# File lib/terminal-table/table.rb, line 11
11: def initialize options = {}, &block
12: @column_widths = []
13: self.style = options.fetch :style, {}
14: self.headings = options.fetch :headings, []
15: self.rows = options.fetch :rows, []
16: self.title = options.fetch :title, nil
17: yield_or_eval(&block) if block
18: end
Generates a ASCII table with the given options.
# File lib/terminal-table/table.rb, line 11
11: def initialize options = {}, &block
12: @column_widths = []
13: self.style = options.fetch :style, {}
14: self.headings = options.fetch :headings, []
15: self.rows = options.fetch :rows, []
16: self.title = options.fetch :title, nil
17: yield_or_eval(&block) if block
18: end
Check if other is equal to self. other is considered equal if it contains the same headings and rows.
# File lib/terminal-table/table.rb, line 153
153: def == other
154: if other.respond_to? :render and other.respond_to? :rows
155: self.headings == other.headings and self.rows == other.rows
156: end
157: end
Check if other is equal to self. other is considered equal if it contains the same headings and rows.
# File lib/terminal-table/table.rb, line 153
153: def == other
154: if other.respond_to? :render and other.respond_to? :rows
155: self.headings == other.headings and self.rows == other.rows
156: end
157: end
Add a row.
# 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.
# 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.
# File lib/terminal-table/table.rb, line 44
44: def add_separator
45: self << :separator
46: end
Add a separator.
# File lib/terminal-table/table.rb, line 44
44: def add_separator
45: self << :separator
46: end
# File lib/terminal-table/table.rb, line 52
52: def cell_padding
53: style.padding_left + style.padding_right
54: end
# File lib/terminal-table/table.rb, line 52
52: def cell_padding
53: style.padding_left + style.padding_right
54: end
# File lib/terminal-table/table.rb, line 48
48: def cell_spacing
49: cell_padding + style.border_y.length
50: end
# File lib/terminal-table/table.rb, line 48
48: def cell_spacing
49: cell_padding + style.border_y.length
50: end
Set the headings
# 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
# 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
Render the table.
# File lib/terminal-table/table.rb, line 107
107: def render
108: separator = Separator.new(self)
109: buffer = [separator]
110: unless @title.nil?
111: buffer << Row.new(self, [title_cell_options])
112: buffer << separator
113: end
114: unless @headings.cells.empty?
115: buffer << @headings
116: buffer << separator
117: end
118: buffer += @rows
119: buffer << separator
120: buffer.map { |r| r.render }.join("\n")
121: end
Render the table.
# File lib/terminal-table/table.rb, line 107
107: def render
108: separator = Separator.new(self)
109: buffer = [separator]
110: unless @title.nil?
111: buffer << Row.new(self, [title_cell_options])
112: buffer << separator
113: end
114: unless @headings.cells.empty?
115: buffer << @headings
116: buffer << separator
117: end
118: buffer += @rows
119: buffer << separator
120: buffer.map { |r| r.render }.join("\n")
121: end
# File lib/terminal-table/table.rb, line 131
131: def rows= array
132: @rows = []
133: array.each { |arr| self << arr }
134: end
# File lib/terminal-table/table.rb, line 131
131: def rows= array
132: @rows = []
133: array.each { |arr| self << arr }
134: end
# File lib/terminal-table/table.rb, line 136
136: def style=(options)
137: style.apply options
138: end
# File lib/terminal-table/table.rb, line 136
136: def style=(options)
137: style.apply options
138: end
# File lib/terminal-table/table.rb, line 144
144: def title=(title)
145: @title = title
146: recalc_column_widths Row.new(self, [title_cell_options])
147: end