Class: Bundler::Thor::Shell::TablePrinter
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
ColumnPrinter
|
|
Instance Chain:
self,
ColumnPrinter
|
|
Inherits: |
Bundler::Thor::Shell::ColumnPrinter
|
Defined in: | lib/bundler/vendor/thor/lib/thor/shell/table_printer.rb |
Constant Summary
-
BORDER_SEPARATOR =
# File 'lib/bundler/vendor/thor/lib/thor/shell/table_printer.rb', line 7:separator
Class Method Summary
Instance Attribute Summary
ColumnPrinter
- Inherited
Instance Method Summary
- #print(array)
-
#as_unicode
private
See additional method definition at line 120.
- #format_cell(column, row_size, index) private
- #indentation private
- #prepare(array) private
- #print_border_separator private
- #truncate(string) private
ColumnPrinter
- Inherited
Constructor Details
.new(stdout, options = {}) ⇒ TablePrinter
# File 'lib/bundler/vendor/thor/lib/thor/shell/table_printer.rb', line 9
def initialize(stdout, = {}) super @formats = [] @maximas = [] @colwidth = [:colwidth] @truncate = [:truncate] == true ? Terminal.terminal_width : [:truncate] @padding = 1 end
Instance Method Details
#as_unicode (private)
See additional method definition at line 120.
# File 'lib/bundler/vendor/thor/lib/thor/shell/table_printer.rb', line 124
def as_unicode yield end
#format_cell(column, row_size, index) (private)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/shell/table_printer.rb', line 72
def format_cell(column, row_size, index) maxima = @maximas[index] f = if column.is_a?(Numeric) if [:borders] # With borders we handle padding separately "%#{maxima}s" elsif index == row_size - 1 # Don't output 2 trailing spaces when printing the last column "%#{maxima}s" else "%#{maxima}s " end else @formats[index] end cell = "".dup cell << "|" + " " * @padding if [:borders] cell << f % column.to_s cell << " " * @padding if [:borders] cell end
#indentation (private)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/shell/table_printer.rb', line 115
def indentation " " * @indent end
#prepare(array) (private)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/shell/table_printer.rb', line 47
def prepare(array) array = array.reject{|row| row == BORDER_SEPARATOR } @formats << "%-#{@colwidth + 2}s".dup if @colwidth start = @colwidth ? 1 : 0 colcount = array.max { |a, b| a.size <=> b.size }.size start.upto(colcount - 1) do |index| maxima = array.map { |row| row[index] ? row[index].to_s.size : 0 }.max @maximas << maxima @formats << if [:borders] "%-#{maxima}s".dup elsif index == colcount - 1 # Don't output 2 trailing spaces when printing the last column "%-s".dup else "%-#{maxima + 2}s".dup end end @formats << "%s" end
#print(array)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/shell/table_printer.rb', line 18
def print(array) return if array.empty? prepare(array) print_border_separator if [:borders] array.each do |row| if [:borders] && row == BORDER_SEPARATOR print_border_separator next end sentence = "".dup row.each_with_index do |column, index| sentence << format_cell(column, row.size, index) end sentence = truncate(sentence) sentence << "|" if [:borders] stdout.puts indentation + sentence end print_border_separator if [:borders] end
#print_border_separator (private)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/shell/table_printer.rb', line 96
def print_border_separator separator = @maximas.map do |maxima| "+" + "-" * (maxima + 2 * @padding) end stdout.puts indentation + separator.join + "+" end
#truncate(string) (private)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/shell/table_printer.rb', line 103
def truncate(string) return string unless @truncate as_unicode do chars = string.chars.to_a if chars.length <= @truncate chars.join else chars[0, @truncate - 3 - @indent].join + "..." end end end