diff --git a/Gemfile.lock b/Gemfile.lock index 9370d19..5f54f95 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,7 +2,6 @@ PATH remote: . specs: foreman (0.40.0) - term-ansicolor (~> 1.0.7) thor (>= 0.13.6) GEM @@ -47,7 +46,6 @@ GEM multi_json (~> 1.0.3) simplecov-html (~> 0.5.3) simplecov-html (0.5.3) - term-ansicolor (1.0.7) thor (0.14.6) win32console (1.3.0-x86-mingw32) xml-simple (1.0.15) diff --git a/foreman.gemspec b/foreman.gemspec index ea85bec..e2921c7 100644 --- a/foreman.gemspec +++ b/foreman.gemspec @@ -16,8 +16,7 @@ Gem::Specification.new do |gem| gem.files = Dir["**/*"].select { |d| d =~ %r{^(README|bin/|data/|ext/|lib/|spec/|test/)} } gem.files << "man/foreman.1" - gem.add_dependency 'term-ansicolor', '~> 1.0.7' - gem.add_dependency 'thor', '>= 0.13.6' + gem.add_dependency 'thor', '>= 0.13.6' if ENV["PLATFORM"] == "java" gem.add_dependency "posix-spawn", "~> 0.3.6" diff --git a/lib/foreman/color.rb b/lib/foreman/color.rb new file mode 100644 index 0000000..ff0157b --- /dev/null +++ b/lib/foreman/color.rb @@ -0,0 +1,40 @@ +require "foreman" + +module Foreman::Color + + ANSI = { + :reset => 0, + :black => 30, + :red => 31, + :green => 32, + :yellow => 33, + :blue => 34, + :magenta => 35, + :cyan => 36, + :white => 37, + :bright_black => 30, + :bright_red => 31, + :bright_green => 32, + :bright_yellow => 33, + :bright_blue => 34, + :bright_magenta => 35, + :bright_cyan => 36, + :bright_white => 37, + } + + def self.enable(io) + io.extend(self) + end + + def color? + return false unless self.respond_to?(:isatty) + self.isatty && ENV["TERM"] + end + + def color(name) + return "" unless color? + return "" unless ansi = ANSI[name.to_sym] + "\e[#{ansi}m" + end + +end diff --git a/lib/foreman/engine.rb b/lib/foreman/engine.rb index f6e28ed..71953fb 100644 --- a/lib/foreman/engine.rb +++ b/lib/foreman/engine.rb @@ -1,10 +1,10 @@ require "foreman" +require "foreman/color" require "foreman/process" require "foreman/procfile" require "foreman/utils" require "tempfile" require "timeout" -require "term/ansicolor" require "fileutils" require "thread" @@ -15,11 +15,10 @@ class Foreman::Engine attr_reader :directory attr_reader :options - extend Term::ANSIColor + COLORS = %w( cyan yellow green magenta red blue intense_cyan intense_yellow + intense_green intense_magenta intense_red, intense_blue ) - COLORS = [ cyan, yellow, green, magenta, red, blue, - intense_cyan, intense_yellow, intense_green, intense_magenta, - intense_red, intense_blue ] + Foreman::Color.enable($stdout) def initialize(procfile, options={}) @procfile = Foreman::Procfile.new(procfile) @@ -128,11 +127,11 @@ private ###################################################################### rescue Errno::ECHILD end - def info(message, name="system", color=Term::ANSIColor.white) + def info(message, name="system", color=:white) output = "" - output += color + output += $stdout.color(color) output += "#{Time.now.strftime("%H:%M:%S")} #{pad_process_name(name)} | " - output += Term::ANSIColor.reset + output += $stdout.color(:reset) output += message.chomp puts output end @@ -182,8 +181,8 @@ private ###################################################################### end def assign_colors - procfile.entries.each do |entry| - colors[entry.name] = next_color + procfile.entries.each_with_index do |entry, idx| + colors[entry.name] = COLORS[idx % COLORS.length] end end @@ -191,13 +190,6 @@ private ###################################################################### readers.invert[reader] end - def next_color - @current_color ||= -1 - @current_color += 1 - @current_color = 0 if COLORS.length < @current_color - COLORS[@current_color] - end - def read_environment_files(filenames) environment = {} diff --git a/spec/foreman/color_spec.rb b/spec/foreman/color_spec.rb new file mode 100644 index 0000000..011224a --- /dev/null +++ b/spec/foreman/color_spec.rb @@ -0,0 +1,31 @@ +require "spec_helper" +require "foreman/color" + +describe Foreman::Color do + + let(:io) { Object.new } + + it "should extend an object with colorization" do + Foreman::Color.enable(io) + io.should respond_to(:color) + end + + it "should not colorize if the object does not respond to isatty" do + mock(io).respond_to?(:isatty) { false } + Foreman::Color.enable(io) + io.color(:white).should == "" + end + + it "should not colorize if the object is not a tty" do + mock(io).isatty { false } + Foreman::Color.enable(io) + io.color(:white).should == "" + end + + it "should colorize if the object is a tty" do + mock(io).isatty { true } + Foreman::Color.enable(io) + io.color(:white).should == "\e[37m" + end + +end