Class: CliFormat::Presenter::Info

Inherits:
Base
  • Object
show all
Defined in:
lib/cli_format/presenter/info.rb

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

#column_sizeObject



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

def column_size
  @rows.first.size
end

#skip?(row) ⇒ Boolean

special rule to skip when second column is blank

Returns:

  • (Boolean)


33
34
35
# File 'lib/cli_format/presenter/info.rb', line 33

def skip?(row)
  column_size == 2 && row[1].blank?
end

#textObject

This format probably works best for 2 columns, though it can support an arbitrary number of columns. It produces aligned text output. Example:

Name          demo
Description   demo-dev-ci
Source        GITHUB


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cli_format/presenter/info.rb', line 11

def text
  column_widths = []
  @rows.each do |row|
    next if skip?(row)
    row.each_with_index do |cell, i|
      column_widths[i] = [column_widths[i].to_i, cell.to_s.length].max
    end
  end

  spacing = @options[:spacing] || "   "
  lines = @rows.map do |row|
    items = []
    next if skip?(row)
    row.each_with_index do |cell, i|
      items << cell.to_s.ljust(column_widths[i]) unless cell.nil?
    end
    items.join(spacing)
  end
  lines.compact.join("\n")
end