This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Check for TTY before sending color codes

  • Loading branch information...
jyurek committed Oct 17, 2012
1 parent bce41dc commit f986c810610901a7c7b43bf01a72c7b031061f9f
Showing with 23 additions and 4 deletions.
  1. +9 −1 lib/cocaine/command_line.rb
  2. +8 −2 spec/cocaine/command_line_spec.rb
  3. +6 −1 spec/support/fake_logger.rb
@@ -72,7 +72,7 @@ def run(interpolations = {})
output = ''
begin
full_command = command(interpolations)
- log("\e[32mCommand\e[0m :: #{full_command}")
+ log("#{colored("Command")} :: #{full_command}")
output = execute(full_command)
rescue Errno::ENOENT
raise Cocaine::CommandNotFoundError
@@ -94,6 +94,14 @@ def unix?
private
+ def colored(text, ansi_color = "\e[32m")
+ if @logger && @logger.tty?
+ "#{ansi_color}#{text}\e[0m"
+ else
+ text
+ end
+ end
+
def log(text)
if @logger
@logger.info(text)
@@ -173,16 +173,22 @@
Cocaine::CommandLine.new("convert").should_not be_unix
end
+ it "colorizes the output to a tty" do
+ logger = FakeLogger.new(:tty => true)
+ Cocaine::CommandLine.new("echo", "'Logging!' :foo", :logger => logger).run(:foo => "bar")
+ logger.entries.should include("\e[32mCommand\e[0m :: echo 'Logging!' 'bar'")
+ end
+
it "logs the command to a supplied logger" do
logger = FakeLogger.new
Cocaine::CommandLine.new("echo", "'Logging!' :foo", :logger => logger).run(:foo => "bar")
- logger.entries.should include("\e[32mCommand\e[0m :: echo 'Logging!' 'bar'")
+ logger.entries.should include("Command :: echo 'Logging!' 'bar'")
end
it "logs the command to a default logger" do
Cocaine::CommandLine.logger = FakeLogger.new
Cocaine::CommandLine.new("echo", "'Logging!'").run
- Cocaine::CommandLine.logger.entries.should include("\e[32mCommand\e[0m :: echo 'Logging!'")
+ Cocaine::CommandLine.logger.entries.should include("Command :: echo 'Logging!'")
end
it "is fine if no logger is supplied" do
@@ -1,5 +1,6 @@
class FakeLogger
- def initialize
+ def initialize(options = {})
+ @tty = options[:tty]
@entries = []
end
@@ -10,4 +11,8 @@ def info(text)
def entries
@entries
end
+
+ def tty?
+ @tty
+ end
end

0 comments on commit f986c81

Please sign in to comment.