Class: CliFormat::Presenter::Space

Inherits:
Base
  • Object
show all
Extended by:
Memoist
Defined in:
lib/cli_format/presenter/space.rb

Direct Known Subclasses

Markdown

Instance Attribute Summary

Attributes inherited from Base

#empty_message, #header, #rows

Instance Method Summary collapse

Methods inherited from Base

#initialize, #show

Constructor Details

This class inherits a constructor from CliFormat::Presenter::Base

Instance Method Details

#format_row(row, max_widths) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/cli_format/presenter/space.rb', line 28

def format_row(row, max_widths)
  formatted_row = row.each_with_index.map do |col, idx|
    idx == row.size - 1 ? col.to_s : col.to_s.ljust(max_widths[idx])
  end

  formatted_row.join(formatted_row_separator)
end

#formatted_row_separatorObject

interface method



37
38
39
# File 'lib/cli_format/presenter/space.rb', line 37

def formatted_row_separator
  " "
end

#max_widthsObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cli_format/presenter/space.rb', line 14

def max_widths
  all_rows = [@header, *@rows].compact
  max_widths = Array.new(all_rows[0].size, 0)

  all_rows.each do |row|
    row.each_with_index do |col, idx|
      max_widths[idx] = [max_widths[idx], col.to_s.length].max
    end
  end

  max_widths
end

#textObject



5
6
7
8
9
10
11
12
# File 'lib/cli_format/presenter/space.rb', line 5

def text
  @buffer << format_row(@header, max_widths) if @header
  @rows.each do |row|
    @buffer << format_row(row, max_widths)
  end

  @buffer.join("\n")
end