add colorization
This commit is contained in:
1
Rakefile
1
Rakefile
@@ -53,6 +53,7 @@ begin
|
||||
s.add_development_dependency 'rr', '~> 0.10.11'
|
||||
s.add_development_dependency 'rspec', '~> 2.0.0'
|
||||
|
||||
s.add_dependency 'term-ansicolor', '~> 1.0.5'
|
||||
s.add_dependency 'thor', '~> 0.13.6'
|
||||
end
|
||||
Jeweler::GemcutterTasks.new
|
||||
|
||||
@@ -2,12 +2,17 @@ require "foreman"
|
||||
require "foreman/process"
|
||||
require "pty"
|
||||
require "tempfile"
|
||||
require "term/ansicolor"
|
||||
|
||||
class Foreman::Engine
|
||||
|
||||
attr_reader :procfile
|
||||
attr_reader :directory
|
||||
|
||||
extend Term::ANSIColor
|
||||
|
||||
COLORS = [ cyan, yellow, green, magenta, on_blue ]
|
||||
|
||||
def initialize(procfile)
|
||||
@procfile = read_procfile(procfile)
|
||||
@directory = File.expand_path(File.dirname(procfile))
|
||||
@@ -18,6 +23,7 @@ class Foreman::Engine
|
||||
procfile.split("\n").inject({}) do |hash, line|
|
||||
next if line.strip == ""
|
||||
process = Foreman::Process.new(*line.split(" ", 2))
|
||||
process.color = next_color
|
||||
hash.update(process.name => process)
|
||||
end
|
||||
end
|
||||
@@ -82,14 +88,17 @@ private ######################################################################
|
||||
def kill_and_exit(signal="TERM")
|
||||
info "termination requested"
|
||||
running_processes.each do |pid, process|
|
||||
info "killing pid #{pid}", process
|
||||
info "killing #{process.name} in pid #{pid}"
|
||||
Process.kill(signal, pid)
|
||||
end
|
||||
exit 0
|
||||
end
|
||||
|
||||
def info(message, process=nil)
|
||||
puts "[#{Time.now.strftime("%H:%M:%S")}] [#{process ? process.name : "system"}] #{message}"
|
||||
print process.color if process
|
||||
print "[#{Time.now.strftime("%H:%M:%S")}] [#{process ? process.name : "system"}] #{message.chomp}"
|
||||
print Term::ANSIColor.reset
|
||||
puts
|
||||
end
|
||||
|
||||
def print_info
|
||||
@@ -118,4 +127,10 @@ private ######################################################################
|
||||
@running_processes ||= {}
|
||||
end
|
||||
|
||||
def next_color
|
||||
@current_color ||= -1
|
||||
@current_color += 1
|
||||
@current_color >= COLORS.length ? "" : COLORS[@current_color]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -4,6 +4,7 @@ class Foreman::Process
|
||||
|
||||
attr_reader :name
|
||||
attr_reader :command
|
||||
attr_accessor :color
|
||||
|
||||
def initialize(name, command)
|
||||
@name = name
|
||||
|
||||
Reference in New Issue
Block a user