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.
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.
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.
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.
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.
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.
523 524 525 |
# File 'lib/term_utils/tab.rb', line 523 def self.find_table(id) @@default_holder.find_table(id) end |
.init_column_props ⇒ Hash
Creates initial column properties.
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_props ⇒ Hash
Creates initial column properties.
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.
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.
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.
494 495 496 |
# File 'lib/term_utils/tab.rb', line 494 def self.set_table_defaults(opts = {}) @@default_holder.set_table_defaults(opts) end |