Skip to content

Instantly share code, notes, and snippets.

@garin
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garin/65157997ddd402ed5ead to your computer and use it in GitHub Desktop.
Save garin/65157997ddd402ed5ead to your computer and use it in GitHub Desktop.
# capistrano3 sshkit(v1.7.1) 用のフォーマッターファイル
# 背景の暗いコンソールで見やすいように色を調整しています
# 元ファイルは: https://github.com/capistrano/sshkit/blob/master/lib/sshkit/formatters/pretty.rb
#
module SSHKit
module Formatter
class MyFormat < Abstract
def write(obj)
return if obj.verbosity < SSHKit.config.output_verbosity
case obj
when SSHKit::Command then write_command(obj)
when SSHKit::LogMessage then write_log_message(obj)
else
original_output << c.black(c.on_yellow("Output formatter doesn't know how to handle #{obj.class}\n"))
end
end
alias :<< :write
private
def write_command(command)
unless command.started?
original_output << "%6s %s\n" % [level(command.verbosity),
uuid(command) + "Running #{c.yellow(c.bold(String(command)))} #{command.host.user ? "as #{c.light_blue(command.host.user)}@" : "on "}#{c.light_magenta(command.host.to_s)}"]
if SSHKit.config.output_verbosity == Logger::DEBUG
original_output << "%6s %s\n" % [level(Logger::DEBUG),
uuid(command) + "Command: #{c.light_blue(command.to_command)}"]
end
end
if SSHKit.config.output_verbosity == Logger::DEBUG
unless command.stdout.empty?
command.stdout.lines.each do |line|
original_output << "%6s %s" % [level(Logger::DEBUG),
uuid(command) + c.green("\t" + line)]
original_output << "\n" unless line[-1] == "\n"
end
command.stdout = ''
end
unless command.stderr.empty?
command.stderr.lines.each do |line|
original_output << "%6s %s" % [level(Logger::DEBUG),
uuid(command) + c.red("\t" + line)]
original_output << "\n" unless line[-1] == "\n"
end
command.stderr = ''
end
end
if command.finished?
original_output << "%6s %s\n" % [level(command.verbosity),
uuid(command) + "Finished in #{sprintf('%5.3f seconds', command.runtime)} with exit status #{command.exit_status} (#{c.bold { command.failure? ? c.red('failed') : c.green('successful') }})."]
end
end
def write_log_message(log_message)
original_output << "%6s %s\n" % [level(log_message.verbosity), log_message.to_s]
end
def c
@c ||= Color
end
def uuid(obj)
"[#{c.green(obj.uuid)}] "
end
def level(verbosity)
c.send(level_formatting(verbosity), level_names(verbosity))
end
def level_formatting(level_num)
%w{ light_black cyan yellow red red }[level_num]
end
def level_names(level_num)
%w{ DEBUG INFO WARN ERROR FATAL }[level_num]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment