class TermUtils::Tab::Table
Represents a table.
Attributes
@return [Hash] `:width`, `:align`, `:fixed`, `:ellipsis`, `:format`.
@return [Integer]
@return [Array<Tab::Column>]
@return [Symbol]
@return [Integer]
Public Class Methods
@param opts [Hash] @option opts [Symbol] :id @option opts [Integer] :offset @option opts [Hash] :column_defaults
# File lib/term_utils/tab.rb, line 128 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
Public Instance Methods
Defines a column. @param id [Symbol] @param opts [Hash] @option opts [Integer] :width @option opts [Symbol] :align @option opts [Boolean] :fixed @option opts [String] :ellipsis @option opts [Proc, String, nil] :format @return [Tab::Column]
# File lib/term_utils/tab.rb, line 163 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
Finds a column. @param id [Symbol] @return [Tab::Column, nil]
# File lib/term_utils/tab.rb, line 182 def find_column(id) @columns.find { |c| c.id == id } end
Prints a data row. @param io [#puts] @param values [Array<Object>, Hash<Symbol, Object>] @param opts [Hash] @option opts [Integer] :offset @option opts [Integer] :column_separator_width @return [nil] @raise [TermUtils::Tab::TableError]
# File lib/term_utils/tab.rb, line 237 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
Prints a header row. @param io [#puts] @param values [Array<Object>, Hash<Symbol, Object>] @param opts [Hash] @option opts [Integer] :offset @option opts [Integer] :column_separator_width @return [nil] @raise [TermUtils::Tab::TableError]
# File lib/term_utils/tab.rb, line 206 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
Prints a separator row. @param io [#puts] @param opts [Hash] @option opts [Integer] :offset @option opts [Integer] :column_separator_width @return [nil]
# File lib/term_utils/tab.rb, line 264 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
Creates a new table printer. @param io [#puts] @param opts [Hash] @option opts [Integer] :offset @option opts [Integer] :column_separator_width @return [Tab::Printer]
# File lib/term_utils/tab.rb, line 192 def printer(io, opts = {}, &block) ptr = Printer.new(self, io, props.merge(opts)) block&.call(ptr) ptr end
Returns the properties of this one. @return [Hash]
# File lib/term_utils/tab.rb, line 139 def props { offset: @offset, column_separator_width: @column_separator_width } end
Sets column default properties. @param opts [Hash] @option opts [Integer] :width @option opts [Symbol] :align @option opts [Boolean] :fixed @option opts [String] :ellipsis @option opts [Proc, String, nil] :format
# File lib/term_utils/tab.rb, line 150 def set_column_defaults(opts = {}) TermUtils::Tab.assign_column_props(@column_defaults, opts) end
Returns column titles. @return [Hash<Symbol, String>]
# File lib/term_utils/tab.rb, line 278 def titles h = {} @columns.each do |col| h[col.id] = col.id.to_s end h end