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 =

rubocop:disable Style/ClassVars

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)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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) ⇒ Hash

Assigns column properties.

Parameters:

  • target (Hash)
  • source (Hash)

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

Returns:

  • (Hash)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Hash

Assigns table properties.

Parameters:

  • target (Hash)
  • source (Hash)

    ‘:offset`, `:column_separator_width`.

Returns:

  • (Hash)


45
46
47
48
49
50
51
52
53
# 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) ⇒ Tab::Table

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

Parameters:

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

Returns:



551
552
553
# 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) ⇒ Tab::Table

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

Parameters:

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

Returns:



559
560
561
# 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) ⇒ Tab::Table?

Finds a registered table.

Parameters:

  • id (Symbol)

Returns:



566
567
568
# File 'lib/term_utils/tab.rb', line 566

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`.



37
38
39
# 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_propsHash

Creates initial column properties.

Returns:

  • (Hash)

    ‘:offset`, `:column_separator_width`.



31
32
33
# 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) ⇒ 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:



577
578
579
# 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 = {}) ⇒ 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)


544
545
546
# 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 = {}) ⇒ Object

Sets table default properties.

Parameters:

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

Options Hash (opts):

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


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

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