-
Notifications
You must be signed in to change notification settings - Fork 35
/
console_formatter.rb
133 lines (109 loc) · 3.6 KB
/
console_formatter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
76
77
78
79
80
81
82
83
84
85
86
87
88
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require "airbrussh/colors"
require "airbrussh/command_formatter"
require "airbrussh/console"
require "airbrussh/rake/context"
require "sshkit"
module Airbrussh
class ConsoleFormatter < SSHKit::Formatter::Abstract
include Airbrussh::Colors
extend Forwardable
attr_reader :config, :context
def_delegators :context, :current_task_name, :register_new_command
def initialize(io, config=Airbrussh.configuration)
super(io)
@config = config
@context = Airbrussh::Rake::Context.new(config)
@console = Airbrussh::Console.new(original_output, config)
write_banner
end
def write_banner
print_line(config.banner_message) if config.banner_message
end
def log_command_start(command)
return if debug?(command)
first_execution = register_new_command(command)
command = decorate(command)
print_task_if_changed
print_indented_line(command.start_message) if first_execution
end
def log_command_data(command, stream_type, string)
return if debug?(command)
return unless config.show_command_output?(stream_type)
command = decorate(command)
string.each_line do |line|
print_indented_line(command.format_output(line))
end
end
def log_command_exit(command)
return if debug?(command)
command = decorate(command)
print_indented_line(command.exit_message, -2)
end
def write(obj)
case obj
when SSHKit::Command
log_command_start(obj)
log_and_clear_command_output(obj, :stderr)
log_and_clear_command_output(obj, :stdout)
log_command_exit(obj) if obj.finished?
when SSHKit::LogMessage
write_log_message(obj)
end
end
alias << write
private
attr_accessor :last_printed_task
def write_log_message(log_message)
return if debug?(log_message)
print_task_if_changed
print_indented_line(format_log_message(log_message))
end
def format_log_message(log_message)
case log_message.verbosity
when SSHKit::Logger::WARN
"#{yellow('WARN')} #{log_message}"
when SSHKit::Logger::ERROR
"#{red('ERROR')} #{log_message}"
when SSHKit::Logger::FATAL
"#{red('FATAL')} #{log_message}"
else
log_message.to_s
end
end
# For SSHKit versions up to and including 1.7.1, the stdout and stderr
# output was available as attributes on the Command. Print the data for
# the specified command and stream if enabled and clear the stream.
# (see Airbrussh::Configuration#command_output).
def log_and_clear_command_output(command, stream)
output = command.public_send(stream)
log_command_data(command, stream, output)
command.public_send("#{stream}=", "")
end
def print_task_if_changed
return if current_task_name.nil?
return if current_task_name == last_printed_task
self.last_printed_task = current_task_name
print_line("#{config.task_prefix}#{clock} #{blue(current_task_name)}")
end
def clock
@start_at ||= Time.now
duration = Time.now - @start_at
minutes = (duration / 60).to_i
seconds = (duration - minutes * 60).to_i
format("%02d:%02d", minutes, seconds)
end
def debug?(obj)
obj.verbosity <= SSHKit::Logger::DEBUG
end
def decorate(command)
Airbrussh::CommandFormatter.new(command, @context.position(command))
end
def print_line(string)
@console.print_line(string)
end
def print_indented_line(string, offset=0)
indent = " " * (6 + offset)
print_line([indent, string].join)
end
end
end