Class: TermUtils::Tab::Table
- Inherits:
-
Object
- Object
- TermUtils::Tab::Table
- Defined in:
- lib/term_utils/tab.rb
Overview
Represents a table.
Instance Attribute Summary collapse
-
#column_defaults ⇒ Hash
‘:width`, `:align`, `:fixed`, `:ellipsis`, `:format`.
- #column_separator_width ⇒ Integer
- #columns ⇒ Array<Tab::Column>
- #id ⇒ Symbol
- #offset ⇒ Integer
Instance Method Summary collapse
-
#define_column(id, opts = {}, &block) ⇒ Tab::Column
Defines a column.
-
#find_column(id) ⇒ Tab::Column?
Finds a column.
-
#initialize(opts = {}) ⇒ Table
constructor
A new instance of Table.
-
#print_data(io, values, opts = {}) ⇒ nil
Prints a data row.
-
#print_header(io, values = nil, opts = {}) ⇒ nil
Prints a header row.
-
#print_separator(io, opts = {}) ⇒ nil
Prints a separator row.
-
#printer(io, opts = {}, &block) ⇒ Tab::Printer
Creates a new table printer.
-
#props ⇒ Hash
Returns the properties of this one.
-
#set_column_defaults(opts = {}) ⇒ Object
Sets column default properties.
-
#titles ⇒ Hash<Symbol, String>
Returns column titles.
Constructor Details
#initialize(opts = {}) ⇒ Table
Returns a new instance of Table.
127 128 129 130 131 132 133 134 |
# File 'lib/term_utils/tab.rb', line 127 def initialize(opts = {}) opts = TermUtils::Tab.init_table_props.merge(opts) @id = opts.fetch(:id, nil) @offset = opts.fetch(:offset) @column_separator_width = opts.fetch(:column_separator_width) @column_defaults = opts.key?(:column_defaults) ? opts[:column_defaults].dup : TermUtils::Tab.default_column_props @columns = [] end |
Instance Attribute Details
#column_defaults ⇒ Hash
Returns ‘:width`, `:align`, `:fixed`, `:ellipsis`, `:format`.
119 120 121 |
# File 'lib/term_utils/tab.rb', line 119 def column_defaults @column_defaults end |
#column_separator_width ⇒ Integer
117 118 119 |
# File 'lib/term_utils/tab.rb', line 117 def column_separator_width @column_separator_width end |
#columns ⇒ Array<Tab::Column>
121 122 123 |
# File 'lib/term_utils/tab.rb', line 121 def columns @columns end |
#id ⇒ Symbol
113 114 115 |
# File 'lib/term_utils/tab.rb', line 113 def id @id end |
#offset ⇒ Integer
115 116 117 |
# File 'lib/term_utils/tab.rb', line 115 def offset @offset end |
Instance Method Details
#define_column(id, opts = {}, &block) ⇒ Tab::Column
Defines a column.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/term_utils/tab.rb', line 162 def define_column(id, opts = {}, &block) col = @columns.find { |c| c.id == id } if col block&.call(col) col.validate else opts[:id] = id opts[:index] = @columns.length col = Column.new(@column_defaults.merge(opts)) block&.call(col) col.validate @columns << col end col end |
#find_column(id) ⇒ Tab::Column?
Finds a column.
181 182 183 |
# File 'lib/term_utils/tab.rb', line 181 def find_column(id) @columns.find { |c| c.id == id } end |
#print_data(io, values, opts = {}) ⇒ nil
Prints a data row.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/term_utils/tab.rb', line 236 def print_data(io, values, opts = {}) vals = values if values.is_a? Hash vals = [] @columns.each do |col| vals << values[col.id] end end raise TermUtils::Tab::TableError, 'wrong values (not array)' unless vals.is_a? Array offset = opts.fetch(:offset) column_separator_width = opts.fetch(:column_separator_width) sb = StringIO.new sb << (' ' * offset) if offset.positive? @columns.each do |col| sb << (' ' * column_separator_width) if col.index.positive? sb << col.render_data(vals[col.index]) end io.puts sb.string end |
#print_header(io, values = nil, opts = {}) ⇒ nil
Prints a header row.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/term_utils/tab.rb', line 205 def print_header(io, values = nil, opts = {}) vals = values if values.nil? vals = @columns.map { |col| col.header.title } elsif values.is_a? Hash vals = [] @columns.each do |col| vals << values[col.id] end end raise TermUtils::Tab::TableError, 'wrong values (not array)' unless vals.is_a? Array offset = opts.fetch(:offset) column_separator_width = opts.fetch(:column_separator_width) sb = StringIO.new sb << (' ' * offset) if offset.positive? @columns.each do |col| sb << (' ' * column_separator_width) if col.index.positive? sb << col.render_header(vals[col.index]) end io.puts sb.string end |
#print_separator(io, opts = {}) ⇒ nil
Prints a separator row.
263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/term_utils/tab.rb', line 263 def print_separator(io, opts = {}) offset = opts.fetch(:offset) column_separator_width = opts.fetch(:column_separator_width) sb = StringIO.new sb << (' ' * offset) if offset.positive? @columns.each do |col| sb << (' ' * column_separator_width) if col.index.positive? sb << ('-' * col.width) end io.puts sb.string end |
#printer(io, opts = {}, &block) ⇒ Tab::Printer
Creates a new table printer.
191 192 193 194 195 |
# File 'lib/term_utils/tab.rb', line 191 def printer(io, opts = {}, &block) ptr = Printer.new(self, io, props.merge(opts)) block&.call(ptr) ptr end |
#props ⇒ Hash
Returns the properties of this one.
138 139 140 |
# File 'lib/term_utils/tab.rb', line 138 def props { offset: @offset, column_separator_width: @column_separator_width } end |
#set_column_defaults(opts = {}) ⇒ Object
Sets column default properties.
149 150 151 |
# File 'lib/term_utils/tab.rb', line 149 def set_column_defaults(opts = {}) TermUtils::Tab.assign_column_props(@column_defaults, opts) end |
#titles ⇒ Hash<Symbol, String>
Returns column titles.
277 278 279 280 281 282 283 |
# File 'lib/term_utils/tab.rb', line 277 def titles h = {} @columns.each do |col| h[col.id] = col.id.to_s end h end |