class Bundler::Thor::Shell::TablePrinter

Constants

BORDER_SEPARATOR

Public Class Methods

new(stdout, options = {}) click to toggle source
# File bundler/vendor/thor/lib/thor/shell/table_printer.rb, line 9
def initialize(stdout, options = {})
  super
  @formats = []
  @maximas = []
  @colwidth = options[:colwidth]
  @truncate = options[:truncate] == true ? Terminal.terminal_width : options[:truncate]
  @padding = 1
end

Public Instance Methods

print(array) click to toggle source

Private Instance Methods

as_unicode() { || ... } click to toggle source
# File bundler/vendor/thor/lib/thor/shell/table_printer.rb, line 120
def as_unicode
  yield
end
format_cell(column, row_size, index) click to toggle source
# File 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 options[: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 options[:borders]
  cell << f % column.to_s
  cell << " " * @padding if options[:borders]
  cell
end
indentation() click to toggle source
# File bundler/vendor/thor/lib/thor/shell/table_printer.rb, line 115
def indentation
  " " * @indent
end
prepare(array) click to toggle source
# File 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 options[: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_border_separator() click to toggle source
truncate(string) click to toggle source
# File 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