module TermUtils::Tab

The tab module provides a way to print formatted tables.

Public Class Methods

align_cut(src, align, fixed, width, ellipsis) click to toggle source

Aligns and cuts a given string. @param src [String] @param align [Symbol] `:left`, `:right`. @param fixed [Boolean] Whether the column width is fixed. @param width [Integer] The column width. @param ellipsis [String] The ellipsis when not fixed. @return [String]

# File lib/term_utils/tab.rb, line 85
def self.align_cut(src, align, fixed, width, ellipsis)
  if align == :left
    # Align left
    if fixed && (src.length > width)
      if ellipsis.length >= width
        ellipsis[0..(width - 1)]
      else
        format '%<value>s%<ellipsis>s', value: src[0..(width - (ellipsis.length + 1))], ellipsis: ellipsis
      end
    else
      src.ljust(width)
    end
  elsif align == :right
    # Align right
    if fixed && (src.length > width)
      if ellipsis.length >= width
        ellipsis[0..(width - 1)]
      else
        format '%<ellipsis>s%<value>s', ellipsis: ellipsis, value: src[(src.length - width + ellipsis.length)..(src.length - 1)]
      end
    else
      src.rjust(width)
    end
  end
end
assign_column_props(target, source) click to toggle source

Assigns column properties. @param target [Hash] @param source [Hash] `:width`, `:align`, `:fixed`, `:ellipsis`, `:format`. @return [Hash]

# File lib/term_utils/tab.rb, line 59
def self.assign_column_props(target, source)
  if (source.key? :width) && (source[:width].is_a? Integer) && source[:width].positive?
    target[:width] = source[:width]
  end
  if (source.key? :align) && %i[left right].index(source[:align])
    target[:align] = source[:align]
  end
  if (source.key? :fixed) && (!!source[:fixed] == source[:fixed])
    target[:fixed] = source[:fixed]
  end
  if (source.key? :ellipsis) && (source[:ellipsis].is_a? String)
    target[:ellipsis] = source[:ellipsis]
  end
  if (source.key? :format) && (source[:ellipsis].nil? || (source[:ellipsis].is_a? Proc) || (source[:ellipsis].is_a? String))
    target[:format] = source[:format]
  end
  target
end
assign_table_props(target, source) click to toggle source

Assigns table properties. @param target [Hash] @param source [Hash] `:offset`, `:column_separator_width`. @return [Hash]

# File lib/term_utils/tab.rb, line 45
def self.assign_table_props(target, source)
  if (source.key? :offset) && (source[:offset].is_a? Integer) && (source[:offset] >= 0)
    target[:offset] = source[:offset]
  end
  if (source.key? :column_separator_width) && (source[:column_separator_width].is_a? Integer) && source[:column_separator_width].positive?
    target[:column_separator_width] = source[:column_separator_width]
  end
  target
end
create_table(opts = {}, &block) click to toggle source

Creates a new Table, using default properties, without registering it. @param opts [Hash] @return [Tab::Table]

# File lib/term_utils/tab.rb, line 551
def self.create_table(opts = {}, &block)
  @@default_holder.create_table(opts, &block)
end
define_table(id, opts = {}, &block) click to toggle source

Defines a new Table, using default properties, and registers it. @param id [Symbol] @param opts [Hash] @return [Tab::Table]

# File lib/term_utils/tab.rb, line 559
def self.define_table(id, opts = {}, &block)
  @@default_holder.define_table(id, opts, &block)
end
find_table(id) click to toggle source

Finds a registered table. @param id [Symbol] @return [Tab::Table, nil]

# File lib/term_utils/tab.rb, line 566
def self.find_table(id)
  @@default_holder.find_table(id)
end
init_column_props() click to toggle source

Creates initial column properties. @return [Hash] `:width`, `:align`, `:fixed`, `:ellipsis`, `:format`.

# File lib/term_utils/tab.rb, line 37
def self.init_column_props
  { width: 8, align: :left, fixed: false, ellipsis: '?', format: nil }
end
init_table_props() click to toggle source

Creates initial column properties. @return [Hash] `:offset`, `:column_separator_width`.

# File lib/term_utils/tab.rb, line 31
def self.init_table_props
  { offset: 0, column_separator_width: 2 }
end
printer(id, io, opts = {}, &block) click to toggle source

Creates a new Printer for a registered Table. @param id [Symbol] @param io [IO] @param opts [Hash] @option opts [Integer] :offset @option opts [Integer] :column_separator_width @return [Tab::Printer]

# File lib/term_utils/tab.rb, line 577
def self.printer(id, io, opts = {}, &block)
  @@default_holder.find_table(id).printer(io, opts, &block)
end
set_column_defaults(opts = {}) click to toggle source

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 544
def self.set_column_defaults(opts = {})
  @@default_holder.set_column_defaults(opts)
end
set_table_defaults(opts = {}) click to toggle source

Sets table default properties. @param opts [Hash] @option opts [Integer] :offset @option opts [Symbol] :column_separator_width

# File lib/term_utils/tab.rb, line 533
def self.set_table_defaults(opts = {})
  @@default_holder.set_table_defaults(opts)
end