lib/term_utils/tab.rb in term_utils-0.4.0 vs lib/term_utils/tab.rb in term_utils-0.5.0
- old
+ new
@@ -1,8 +1,8 @@
-# frozen-string-literal: true
+# frozen_string_literal: true
-# Copyright (C) 2020 Thomas Baron
+# Copyright (C) 2023 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
@@ -14,18 +14,17 @@
# 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/>.
+require 'stringio'
+
module TermUtils
# The tab module provides a way to print formatted tables.
module Tab
# Represents a table error.
class TableError < StandardError
- def initialize(msg)
- super
- end
end
# Creates initial column properties.
# @return [Hash] `:offset`, `:column_separator_width`.
def self.init_table_props
@@ -216,13 +215,13 @@
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?
+ sb << (' ' * offset) if offset.positive?
@columns.each do |col|
- sb << ' ' * column_separator_width if col.index.positive?
+ sb << (' ' * column_separator_width) if col.index.positive?
sb << col.render_header(vals[col.index])
end
io.puts sb.string
end
@@ -245,13 +244,13 @@
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?
+ sb << (' ' * offset) if offset.positive?
@columns.each do |col|
- sb << ' ' * column_separator_width if col.index.positive?
+ sb << (' ' * column_separator_width) if col.index.positive?
sb << col.render_data(vals[col.index])
end
io.puts sb.string
end
@@ -263,14 +262,14 @@
# @return [nil]
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?
+ sb << (' ' * offset) if offset.positive?
@columns.each do |col|
- sb << ' ' * column_separator_width if col.index.positive?
- sb << '-' * col.width
+ sb << (' ' * column_separator_width) if col.index.positive?
+ sb << ('-' * col.width)
end
io.puts sb.string
end
# Returns column titles.
@@ -356,11 +355,11 @@
src = @format.call(val)
elsif @format.is_a? String
src = @format % val
end
end
- src = src.is_a?(String) ? src : src.to_s
+ src = src.to_s unless src.is_a?(String)
TermUtils::Tab.align_cut(src, @align, @fixed, @width, @ellipsis)
end
end
# Represents a column header.
@@ -385,10 +384,11 @@
def validate
raise TermUtils::Tab::TableError, 'missing header title (nil)' if @title.nil?
raise TermUtils::Tab::TableError, 'wrong header align (not :left or :right)' unless %i[left right].index(@align)
end
end
+
# Represents a table printer.
class Printer
# @return [Tab::Table]
attr_accessor :table
# @return [IO]
@@ -437,9 +437,10 @@
# @return [nil]
def separator(opts = nil)
@table.print_separator(@io, opts ? @options.merge(opts) : @options)
end
end
+
# Represents a Holder of Table(s).
class Holder
# @return [Hash] `:offset`, `:column_separator_width`.
attr_accessor :table_defaults
# @return [Hash] `:width`, `:align`, `:fixed`, `:ellipsis`, `:format`.