Module: TermUtils::Tab

Defined in:
lib/term_utils/tab.rb

Overview

The tab module provides a way to print formatted tables.

Defined Under Namespace

Classes: Column, Header, Holder, Printer, Table, TableError

Constant Summary collapse

@@default_holder =
Holder.new

Class Method Summary collapse

Class Method Details

.align_cut(src, align, fixed, width, ellipsis) ⇒ String

Aligns and cuts a given string.

Parameters:

  • src (String)
  • align (Symbol)

    ‘:left`, `:right`.

  • fixed (Boolean)

    Whether the column width is fixed.

  • width (Integer)

    The column width.

  • ellipsis (String)

    The ellipsis when not fixed.

Returns:

  • (String)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/term_utils/tab.rb', line 77

def self.align_cut(src, align, fixed, width, ellipsis)
  res = src
  if align == :left
    # Align left
    if fixed && (src.length > width)
      if ellipsis.length >= width
        res = ellipsis[0..(width - 1)]
      else
        res = "%s%s" % [src[0..(width - (ellipsis.length + 1))], ellipsis]
      end
    else
      res = "%-*s" % [width, src]
    end
  elsif align == :right
    # Align right
    if fixed && (src.length > width)
      if ellipsis.length >= width
        res = ellipsis[0..(width - 1)]
      else
        res = "%s%s" % [ellipsis, src[(src.length - width + ellipsis.length)..(src.length - 1)]]
      end
    else
      res = "%*s" % [width, src]
    end
  end
  res
end

.assign_column_props(target, source) ⇒ Hash

Assigns column properties.

Parameters:

  • target (Hash)
  • source (Hash)

    ‘:width`, `:align`, `:fixed`, `:ellipsis`, `:format`.

Returns:

  • (Hash)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/term_utils/tab.rb', line 52

def self.assign_column_props(target, source)
  if (source.has_key? :width) && (source[:width].is_a? Integer) && (source[:width] > 0)
    target[:width] = source[:width]
  end
  if (source.has_key? :align) && %i[left right].index(source[:align])
    target[:align] = source[:align]
  end
  if (source.has_key? :fixed) && (!!source[:fixed] == source[:fixed])
    target[:fixed] = source[:fixed]
  end
  if (source.has_key? :ellipsis) && (source[:ellipsis].is_a? String)
    target[:ellipsis] = source[:ellipsis]
  end
  if (source.has_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) ⇒ Hash

Assigns table properties.

Parameters:

  • target (Hash)
  • source (Hash)

    ‘:offset`, `:column_separator_width`.

Returns:

  • (Hash)


39
40
41
42
43
44
45
46
47
# File 'lib/term_utils/tab.rb', line 39

def self.assign_table_props(target, source)
  if (source.has_key? :offset) && (source[:offset].is_a? Integer) && (source[:offset] >= 0)
    target[:offset] = source[:offset]
  end
  if (source.has_key? :column_separator_width) && (source[:column_separator_width].is_a? Integer) && (source[:column_separator_width] > 0)
    target[:column_separator_width] = source[:column_separator_width]
  end
  target
end

.create_table(opts = {}, &block) ⇒ Tab::Table

Creates a new Table, using default properties, without registering it.

Parameters:

  • opts (Hash) (defaults to: {})

Returns:



510
511
512
# File 'lib/term_utils/tab.rb', line 510

def self.create_table(opts = {}, &block)
  @@default_holder.create_table(opts, &block)
end

.define_table(id, opts = {}, &block) ⇒ Tab::Table

Defines a new Table, using default properties, and registers it.

Parameters:

  • id (Symbol)
  • opts (Hash) (defaults to: {})

Returns:



517
518
519
# File 'lib/term_utils/tab.rb', line 517

def self.define_table(id, opts = {}, &block)
  @@default_holder.define_table(id, opts, &block)
end

.find_table(id) ⇒ Tab::Table?

Finds a registered table.

Parameters:

  • id (Symbol)

Returns:



523
524
525
# File 'lib/term_utils/tab.rb', line 523

def self.find_table(id)
  @@default_holder.find_table(id)
end

.init_column_propsHash

Creates initial column properties.

Returns:

  • (Hash)

    ‘:width`, `:align`, `:fixed`, `:ellipsis`, `:format`.



32
33
34
# File 'lib/term_utils/tab.rb', line 32

def self.init_column_props
  {:width => 8, :align => :left, :fixed => false, :ellipsis => "?", :format => nil}
end

.init_table_propsHash

Creates initial column properties.

Returns:

  • (Hash)

    ‘:offset`, `:column_separator_width`.



27
28
29
# File 'lib/term_utils/tab.rb', line 27

def self.init_table_props
  {:offset => 0, :column_separator_width => 2}
end

.printer(id, io, opts = {}, &block) ⇒ Tab::Printer

Creates a new Printer for a registered Table.

Parameters:

  • id (Symbol)
  • io (IO)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :offset (Integer)
  • :column_separator_width (Integer)

Returns:



533
534
535
# File 'lib/term_utils/tab.rb', line 533

def self.printer(id, io, opts = {}, &block)
  @@default_holder.find_table(id).printer(io, opts, &block)
end

.set_column_defaults(opts = {}) ⇒ Object

Sets column default properties.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :width (Integer)
  • :align (Symbol)
  • :fixed (Boolean)
  • :ellipsis (String)
  • :format (Proc, String, nil)


504
505
506
# File 'lib/term_utils/tab.rb', line 504

def self.set_column_defaults(opts = {})
  @@default_holder.set_column_defaults(opts)
end

.set_table_defaults(opts = {}) ⇒ Object

Sets table default properties.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :offset (Integer)
  • :column_separator_width (Symbol)


494
495
496
# File 'lib/term_utils/tab.rb', line 494

def self.set_table_defaults(opts = {})
  @@default_holder.set_table_defaults(opts)
end