From fff958f7b6b045e41ca8bfc458405e7c6c931424 Mon Sep 17 00:00:00 2001 From: David Dollar Date: Tue, 22 Jun 2010 16:28:08 -0400 Subject: [PATCH] add colorization --- Rakefile | 1 + lib/foreman/engine.rb | 19 +++++++++++++++++-- lib/foreman/process.rb | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Rakefile b/Rakefile index a9706ba..ec6d92e 100644 --- a/Rakefile +++ b/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 diff --git a/lib/foreman/engine.rb b/lib/foreman/engine.rb index 59dc5ef..151e135 100644 --- a/lib/foreman/engine.rb +++ b/lib/foreman/engine.rb @@ -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 diff --git a/lib/foreman/process.rb b/lib/foreman/process.rb index 3014c68..939924f 100644 --- a/lib/foreman/process.rb +++ b/lib/foreman/process.rb @@ -4,6 +4,7 @@ class Foreman::Process attr_reader :name attr_reader :command + attr_accessor :color def initialize(name, command) @name = name