forked from ddollar/foreman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace term-ansicolor with built-in colorization
- Loading branch information
Showing
5 changed files
with
81 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |