Class: Freyia::Shell::TablePrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/freyia/shell/table_printer.rb

Constant Summary collapse

BORDER_SEPARATOR =
:separator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout, indent: 0, colwidth: nil, truncate: false, borders: false) ⇒ TablePrinter

rubocop:disable Metrics/ParameterLists



17
18
19
20
21
22
23
24
25
26
# File 'lib/freyia/shell/table_printer.rb', line 17

def initialize(stdout, indent: 0, colwidth: nil, truncate: false, borders: false) # rubocop:disable Metrics/ParameterLists
  @stdout = stdout
  @indent = indent.to_i
  @formats = []
  @maximas = []
  @colwidth = colwidth
  @truncate = truncate == true ? Terminal.terminal_width : truncate
  @borders = borders
  @padding = 1
end

Instance Attribute Details

#bordersBoolean (readonly)

Returns:

  • (Boolean)


15
16
17
# File 'lib/freyia/shell/table_printer.rb', line 15

def borders
  @borders
end

#stdoutIO (readonly)

Returns:

  • (IO)


12
13
14
# File 'lib/freyia/shell/table_printer.rb', line 12

def stdout
  @stdout
end

Instance Method Details

rubocop:todo Metrics



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/freyia/shell/table_printer.rb', line 28

def print(array) # rubocop:todo Metrics
  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 = +""

    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