lib/term_utils/tab.rb in term_utils-0.1.1 vs lib/term_utils/tab.rb in term_utils-0.2.0

- old
+ new

@@ -1,5 +1,7 @@ +# Copyright (C) 2019 Thomas Baron +# # This file is part of term_utils. # # term_utils is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 3 of the License. @@ -10,10 +12,11 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with term_utils. If not, see <https://www.gnu.org/licenses/>. module TermUtils + # The tab module provides a way to print formatted tables. module Tab # Represents a table. class Table # @return [Symbol] attr_accessor :id @@ -51,11 +54,10 @@ def find_column(id) @columns.find { |c| c.id == id } end # Creates a new table printer. # @param io [IO] - # @param values [Array<Object>, Hash<Symbol, Object>] # @param opts [Hash] # @option opts [Integer] :offset # @option opts [Integer] :column_separator_width # @return [Tab::Printer] def printer(io, opts = {}, &block) @@ -248,16 +250,29 @@ def initialize(table, io, options) @table = table @io = io @options = options end + # Prints an empty line. def line @io.puts "" end + # Prints a header row. + # @param values [Array<Object>, Hash<Symbol, Object>] + # @param opts [Hash] + # @option opts [Integer] :offset + # @option opts [Integer] :column_separator_width + # @return [nil] def header(values = nil, opts = {}) @table.print_header(@io, values, @options.merge(opts)) end + # Prints a data row. + # @param values [Array<Object>, Hash<Symbol, Object>] + # @param opts [Hash] + # @option opts [Integer] :offset + # @option opts [Integer] :column_separator_width + # @return [nil] def data(values, opts = {}) @table.print_data(@io, values, @options.merge(opts)) end # Prints a separator. # @param opts [Hash] @@ -297,11 +312,10 @@ @tables[id] end # Creates a new table printer. # @param id [Symbol] # @param io [IO] - # @param values [Array<Object>, Hash<Symbol, Object>] # @param opts [Hash] # @option opts [Integer] :offset # @option opts [Integer] :column_separator_width # @return [Tab::Printer] def printer(id, io, opts = {}, &block) @@ -314,13 +328,18 @@ # @param opts [Hash] # @return [Tab::Table] def self.define_table(id, opts = {}, &block) @@default_holder.define_table(id, opts = {}, &block) end + # Finds a table. + # @param id [Symbol] + # @return [Tab::Table, nil] + def self.find_table(id) + @@default_holder.find_table(id) + end # Creates a new table printer. # @param id [Symbol] # @param io [IO] - # @param values [Array<Object>, Hash<Symbol, Object>] # @param opts [Hash] # @option opts [Integer] :offset # @option opts [Integer] :column_separator_width # @return [Tab::Printer] def self.printer(id, io, opts = {}, &block)