Class: IParty::CLI::Formatter::ConsolePretty

Inherits:
IParty::CLI::Formatter show all
Defined in:
lib/iparty/cli/formatter.rb

Constant Summary

Constants inherited from IParty::CLI::Formatter

VOID_OUTPUT

Constants included from Colorize

Colorize::COLORMAP

Instance Method Summary collapse

Methods inherited from IParty::CLI::Formatter

#colorize?, descendants, find_by_id, #initialize

Methods included from Colorize

#colorize, #decolorize, #with_color

Constructor Details

This class inherits a constructor from IParty::CLI::Formatter

Instance Method Details

#format(ip, index: 0, **kw, &to_data) ⇒ Object



79
80
81
82
83
# File 'lib/iparty/cli/formatter.rb', line 79

def format ip, index: 0, **kw, &to_data
  out = to_indented_strings(to_data.call(ip), **kw).join("\n")
  out = VOID_OUTPUT if out.empty?
  out
end

#format_all(ips, base_index: 0, **kw, &to_data) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/iparty/cli/formatter.rb', line 65

def format_all ips, base_index: 0, **kw, &to_data
  ips.map.with_index do |ip, index|
    [].tap do |r|
      r << nil unless (base_index + index).zero?
      next unless out = format(ip, index: base_index + index, **kw, &to_data)

      indent_header = decolorize(out[0...(out.index(":") || 0)]).length - 1 if @opts[:align]
      r << c("#{"".rjust(indent_header || 0, "=")}=> #{ip}", :red)

      r << out
    end
  end
end

#setupObject



61
62
63
# File 'lib/iparty/cli/formatter.rb', line 61

def setup
  @opts[:align] = @app.opts[:summarize]
end

#to_indented_strings(value, key: nil, indent: -1,, maxkeylength: 0, buf: []) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/iparty/cli/formatter.rb', line 85

def to_indented_strings value, key: nil, indent: -1, maxkeylength: 0, buf: []
  indent_spaces = "".rjust(2 * indent, " ")

  if value.is_a?(Hash)
    buf << "#{indent_spaces}#{c(key.is_a?(Numeric) ? "[#{key}]" : "#{key}:")}" if key
    maxkeylength = value.keys.map{ _1.to_s.length }.max if @opts[:align]
    value.each do |k, v|
      to_indented_strings(v, buf: buf, maxkeylength: maxkeylength, key: k, indent: indent + 1)
    end
  elsif value.is_a?(Array)
    buf << "#{indent_spaces}#{c(key.is_a?(Numeric) ? "[#{key}]" : "#{key}:")}" if key
    value.each_with_index do |v, i|
      to_indented_strings(v, buf: buf, maxkeylength: maxkeylength, key: i, indent: indent + 1)
    end
  elsif key
    buf << "#{indent_spaces}#{c(key.to_s.rjust(maxkeylength))}: #{c(value, :blue)}"
  else
    buf << "#{indent_spaces}#{c(value, :blue)}"
  end

  buf
end