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
Class Method Summary collapse
-
.align_cut(src, align, fixed, width, ellipsis) ⇒ String
Aligns and cuts a given string.
-
.assign_column_props(target, source) ⇒ Hash
Assigns column properties.
-
.assign_table_props(target, source) ⇒ Hash
Assigns table properties.
-
.create_table(opts = {}, &block) ⇒ Tab::Table
Creates a new Table, using default properties, without registering it.
-
.define_table(id, opts = {}, &block) ⇒ Tab::Table
Defines a new Table, using default properties, and registers it.
-
.find_table(id) ⇒ Tab::Table?
Finds a registered table.
-
.init_column_props ⇒ Hash
Creates initial column properties.
-
.init_table_props ⇒ Hash
Creates initial column properties.
-
.printer(id, io, opts = {}, &block) ⇒ Tab::Printer
Creates a new Printer for a registered Table.
-
.set_column_defaults(opts = {}) ⇒ Object
Sets column default properties.
-
.set_table_defaults(opts = {}) ⇒ Object
Sets table default properties.
Class Method Details
.align_cut(src, align, fixed, width, ellipsis) ⇒ String
Aligns and cuts a given 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.
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.
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.
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.
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.
566 567 568 |
# File 'lib/term_utils/tab.rb', line 566 def self.find_table(id) @@default_holder.find_table(id) end |
.init_column_props ⇒ Hash
Creates initial column properties.
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_props ⇒ Hash
Creates initial column properties.
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.
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.
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.
533 534 535 |
# File 'lib/term_utils/tab.rb', line 533 def self.set_table_defaults(opts = {}) @@default_holder.set_table_defaults(opts) end |