Class: Bow::ResponseFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/bow/response_formatter.rb

Constant Summary collapse

ERROR =
"\033[31m%s\033[0m"
INFO =
"\033[33m%s\033[0m"
SUCCESS =
"\033[32m%s\033[0m"
HEADER =
"\033[1;35m%s\033[0m"

Class Method Summary collapse

Class Method Details

.colorize(msg, color_pattern) ⇒ Object



40
41
42
# File 'lib/bow/response_formatter.rb', line 40

def colorize(msg, color_pattern)
  color_pattern % msg
end

.colorize_result(result) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/bow/response_formatter.rb', line 29

def colorize_result(result)
  out, err = result.map { |m| m.to_s.strip }
  err = err.empty? ? nil : colorize(err, ERROR)
  out = if !out.empty?
          colorize(out, SUCCESS)
        elsif err.nil?
          colorize('DONE', INFO)
        end
  [out, err]
end

.multi_print(host, results) ⇒ Object



15
16
17
18
19
# File 'lib/bow/response_formatter.rb', line 15

def multi_print(host, results)
  results.each do |r|
    puts "#{wrap(host, r)}\n"
  end
end

.pretty_print(*args) ⇒ Object



11
12
13
# File 'lib/bow/response_formatter.rb', line 11

def pretty_print(*args)
  puts "#{wrap(*args)}\n"
end

.wrap(host, result) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/bow/response_formatter.rb', line 21

def wrap(host, result)
  host_group = colorize("[#{host.group}]", HEADER)
  host_addr = colorize(host.host, HEADER)
  host_header = "\n#{host_group} #{host_addr}:\n\n"
  response = colorize_result(result).compact.first
  "#{host_header}#{response}"
end