Class: EY::Serverside::Shell::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/engineyard-serverside/shell/formatter.rb

Constant Summary collapse

FATAL =
'ERROR'.freeze
ERROR =
'ERROR'.freeze
WARN =
'WARN'.freeze
INFO =
'INFO'.freeze
DEBUG =
'DEBUG'.freeze
IMPORTANT =
[WARN, ERROR, FATAL].freeze
SECONDS_FORMAT =
'+    %02ds  '.freeze
MINUTES_FORMAT =
'+%2dm %02ds  '.freeze
STATUS_PREFIX =
'~> '.freeze
SUBSTATUS_PREFIX =
' ~ '.freeze
IMPORTANT_PREFIX =
'!> '.freeze
NL =
"\n".freeze

Instance Method Summary collapse

Constructor Details

#initialize(stdout, stderr, start_time, verbose) ⇒ Formatter

Returns a new instance of Formatter.


21
22
23
24
25
# File 'lib/engineyard-serverside/shell/formatter.rb', line 21

def initialize(stdout, stderr, start_time, verbose)
  @stdout, @stderr = stdout, stderr
  @start = start_time.to_i
  @verbose = verbose
end

Instance Method Details

#build_message(severity, stamp, message) ⇒ Object


33
34
35
36
37
38
39
40
41
# File 'lib/engineyard-serverside/shell/formatter.rb', line 33

def build_message(severity, stamp, message)
  if IMPORTANT.include?(severity)
    prepend("#{stamp}#{IMPORTANT_PREFIX}", message)
  elsif INFO == severity
    prepend(stamp, message)
  else
    prepend(stamp, message)
  end
end

#call(severity, time, _, message) ⇒ Object


27
28
29
30
31
# File 'lib/engineyard-serverside/shell/formatter.rb', line 27

def call(severity, time, _, message)
  msg = build_message(severity, timestamp(time), message)
  put_to_io(severity, msg)
  msg
end

#prepend(pre, str) ⇒ Object


43
44
45
# File 'lib/engineyard-serverside/shell/formatter.rb', line 43

def prepend(pre, str)
  str.gsub(/^/, pre).sub(/\n?\z/m,NL)
end

#put_to_io(severity, msg) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/engineyard-serverside/shell/formatter.rb', line 47

def put_to_io(severity, msg)
  case severity
  when DEBUG
    if @verbose
      @stdout << msg
      @stdout.flush
    end
  when INFO
    # Need to differentiate info messages more when we're running in verbose mode
    if @verbose && msg.index(STATUS_PREFIX)
      @stdout.puts
      @stdout << thor_shell.set_color(msg, :white, true)
    else
      @stdout << msg
    end
    @stdout.flush
  when WARN
    @stderr.puts
    @stderr << thor_shell.set_color(msg, :yellow, true)
    @stderr.flush
  when ERROR
    @stderr.puts
    @stderr << thor_shell.set_color(msg, :red, true)
    @stderr.flush
  else
    @stderr << msg
    @stderr.flush
  end
end

#thor_shellObject


88
89
90
# File 'lib/engineyard-serverside/shell/formatter.rb', line 88

def thor_shell
  thor_shell ||= Thor::Shell::Color.new
end

#timestamp(datetime) ⇒ Object


77
78
79
80
81
82
83
84
85
86
# File 'lib/engineyard-serverside/shell/formatter.rb', line 77

def timestamp(datetime)
  diff = datetime.to_i - @start
  diff = 0 if diff < 0
  div, mod = diff.divmod(60)
  if div.zero?
    SECONDS_FORMAT % mod
  else
    MINUTES_FORMAT % [div,mod]
  end
end