Class: Thor::Shell::TablePrinter

Inherits:
ColumnPrinter show all
Defined in:
lib/thor/shell/table_printer.rb

Constant Summary collapse

BORDER_SEPARATOR =
:separator

Instance Attribute Summary

Attributes inherited from ColumnPrinter

#options, #stdout

Instance Method Summary collapse

Constructor Details

#initialize(stdout, options = {}) ⇒ TablePrinter

Returns a new instance of TablePrinter.



9
10
11
12
13
14
15
16
# File '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

Instance Method Details



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/thor/shell/table_printer.rb', line 18

def print(array)
  return if array.empty?

  prepare(array)

  print_border_separator if options[:borders]

  array.each do |row|
    if options[: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 options[:borders]
    stdout.puts indentation + sentence

  end
  print_border_separator if options[:borders]
end