Class: Synco::CompactFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/synco/compact_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(verbose: true) ⇒ CompactFormatter

Returns a new instance of CompactFormatter.



26
27
28
29
# File 'lib/synco/compact_formatter.rb', line 26

def initialize(verbose: true)
  @start = Time.now
  @verbose = verbose
end

Instance Method Details

#call(severity, datetime, progname, message) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/synco/compact_formatter.rb', line 89

def call(severity, datetime, progname, message)
  buffer = []
  prefix = ""
  
  if @verbose
    prefix = time_offset_prefix
    buffer << Rainbow(prefix).cyan + ": "
    prefix = " " * (prefix.size) + "| " 
  end
  
  if progname == 'shell' and message.kind_of? Array
    format_command(message, buffer)
  elsif message.kind_of? Exception
    format_exception(message, buffer)
  else
    buffer << format_line(message, severity) << "\n"
  end
  
  result = buffer.join
  
  # This fancy regex indents lines correctly depending on the prefix:
  result.gsub!(/\n(?!$)/, "\n#{prefix}") unless prefix.empty?
  
  return result
end

#chdir_string(options) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/synco/compact_formatter.rb', line 43

def chdir_string(options)
  if options[:chdir]
    " in #{options[:chdir]}"
  else
    ""
  end
end

#format_command(arguments, buffer) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/synco/compact_formatter.rb', line 51

def format_command(arguments, buffer)
  arguments = arguments.dup
  
  # environment = arguments.first.is_a?(Hash) ? arguments.shift : nil
  options = arguments.last.is_a?(Hash) ? arguments.pop : nil
  
  arguments = arguments.flatten.collect(&:to_s)
  
  buffer << Rainbow(arguments.shelljoin).bright.blue
  
  if options
    buffer << chdir_string(options)
  end
  
  buffer << "\n"
  
  # if environment
  #  environment.each do |key,value|
  #    buffer << "\texport #{key}=#{value.dump}\n"
  #  end
  # end
end

#format_exception(exception, buffer) ⇒ Object



74
75
76
77
78
79
# File 'lib/synco/compact_formatter.rb', line 74

def format_exception(exception, buffer)
  buffer << Rainbow("#{exception.class}: #{exception}").bright.red << "\n"
  exception.backtrace.each do |line|
    buffer << "\t" << Rainbow(line).red << "\n"
  end
end

#format_line(message, severity) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/synco/compact_formatter.rb', line 81

def format_line(message, severity)
  if severity == 'ERROR'
    Rainbow(message).red
  else
    message
  end
end

#time_offset_prefixObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/synco/compact_formatter.rb', line 31

def time_offset_prefix
  offset = Time.now - @start
  minutes = (offset/60).floor
  seconds = (offset - (minutes*60))
  
  if minutes > 0
    "#{minutes}m#{seconds.floor}s"
  else
    "#{seconds.round(2)}s"
  end.rjust(6)
end