Skip to content

Commit

Permalink
replace term-ansicolor with built-in colorization
Browse files Browse the repository at this point in the history
  • Loading branch information
ddollar committed Mar 16, 2012
1 parent bb33774 commit 2f982ff
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 21 deletions.
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ PATH
remote: .
specs:
foreman (0.40.0)
term-ansicolor (~> 1.0.7)
thor (>= 0.13.6)

GEM
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions foreman.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 40 additions & 0 deletions lib/foreman/color.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 9 additions & 17 deletions lib/foreman/engine.rb
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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)
Expand Down Expand Up @@ -128,11 +127,11 @@ def watch_for_termination
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
Expand Down Expand Up @@ -182,22 +181,15 @@ def colors
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

def process_by_reader(reader)
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 = {}

Expand Down
31 changes: 31 additions & 0 deletions spec/foreman/color_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2f982ff

Please sign in to comment.