mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Added AwesomePrint.force_colors! to allow outputting colors when not run in a process attached to a TTY.
This commit is contained in:
parent
638b04206d
commit
7907a5871d
4 changed files with 108 additions and 7 deletions
|
@ -43,7 +43,16 @@ class AwesomePrint
|
||||||
@indentation = @options[:indent].abs
|
@indentation = @options[:indent].abs
|
||||||
Thread.current[AP] ||= []
|
Thread.current[AP] ||= []
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.allow_colors?
|
||||||
|
(STDOUT.tty? && ((ENV['TERM'] && ENV['TERM'] != 'dumb') || ENV['ANSICON'])) || @@force_colors
|
||||||
|
end
|
||||||
|
|
||||||
|
@@force_colors = false
|
||||||
|
def self.force_colors!(value = true)
|
||||||
|
@@force_colors = value
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Format an array.
|
# Format an array.
|
||||||
|
|
|
@ -12,12 +12,20 @@ class String
|
||||||
# 0 => normal
|
# 0 => normal
|
||||||
|
|
||||||
[ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i|
|
[ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i|
|
||||||
if STDOUT.tty? && ((ENV['TERM'] && ENV['TERM'] != 'dumb') || ENV['ANSICON'])
|
define_method color do
|
||||||
define_method color do "\033[1;#{30+i}m#{self}\033[0m" end
|
if AwesomePrint.allow_colors?
|
||||||
define_method :"#{color}ish" do "\033[0;#{30+i}m#{self}\033[0m" end
|
"\033[1;#{30+i}m#{self}\033[0m"
|
||||||
else
|
else
|
||||||
define_method color do self end
|
self
|
||||||
alias_method :"#{color}ish", color
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
define_method :"#{color}ish" do
|
||||||
|
if AwesomePrint.allow_colors?
|
||||||
|
"\033[0;#{30+i}m#{self}\033[0m"
|
||||||
|
else
|
||||||
|
self
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
80
spec/colorization_spec.rb
Normal file
80
spec/colorization_spec.rb
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||||
|
|
||||||
|
describe "AwesomePrint" do
|
||||||
|
describe "colorization" do
|
||||||
|
PLAIN = '[ 1, :two, "three", [ nil, [ true, false ] ] ]'
|
||||||
|
COLORIZED = "[ \e[1;34m1\e[0m, \e[0;36m:two\e[0m, \e[0;33m\"three\"\e[0m, [ \e[1;31mnil\e[0m, [ \e[1;32mtrue\e[0m, \e[1;31mfalse\e[0m ] ] ]"
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
AwesomePrint.force_colors!(false)
|
||||||
|
ENV['TERM'] = "xterm-colors"
|
||||||
|
ENV.delete('ANSICON')
|
||||||
|
@arr = [ 1, :two, "three", [ nil, [ true, false] ] ]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "colorizes tty processes by default" do
|
||||||
|
stub_tty!(STDOUT, true)
|
||||||
|
|
||||||
|
@arr.ai(:multiline => false).should == COLORIZED
|
||||||
|
end
|
||||||
|
|
||||||
|
it "colorizes tty processes by default" do
|
||||||
|
stub_tty!(STDOUT, true)
|
||||||
|
|
||||||
|
@arr.ai(:multiline => false).should == COLORIZED
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
it "colorizes processes with ENV['ANSICON'] by default" do
|
||||||
|
stub_tty!(STDOUT, true)
|
||||||
|
ENV['ANSICON'] = "1"
|
||||||
|
|
||||||
|
@arr.ai(:multiline => false).should == COLORIZED
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does not colorize tty processes running in dumb terminals by default" do
|
||||||
|
stub_tty!(STDOUT, true)
|
||||||
|
ENV['TERM'] = "dumb"
|
||||||
|
|
||||||
|
@arr.ai(:multiline => false).should == PLAIN
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does not colorize subprocesses by default" do
|
||||||
|
stub_tty!(STDOUT, false)
|
||||||
|
|
||||||
|
@arr.ai(:multiline => false).should == PLAIN
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "forced" do
|
||||||
|
before(:each) do
|
||||||
|
AwesomePrint.force_colors!
|
||||||
|
end
|
||||||
|
|
||||||
|
it "still colorizes tty processes" do
|
||||||
|
stub_tty!(STDOUT, true)
|
||||||
|
|
||||||
|
@arr.ai(:multiline => false).should == COLORIZED
|
||||||
|
end
|
||||||
|
|
||||||
|
it "colorizes dumb terminals" do
|
||||||
|
stub_tty!(STDOUT, true)
|
||||||
|
ENV["TERM"] = "dumb"
|
||||||
|
|
||||||
|
@arr.ai(:multiline => false).should == COLORIZED
|
||||||
|
end
|
||||||
|
|
||||||
|
it "colorizes subprocess" do
|
||||||
|
stub_tty!(STDOUT, true)
|
||||||
|
@arr.ai(:multiline => false).should == COLORIZED
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def stub_tty!(stream, value)
|
||||||
|
eval(%{class << stream
|
||||||
|
def tty?
|
||||||
|
#{value}
|
||||||
|
end
|
||||||
|
end})
|
||||||
|
end
|
||||||
|
end
|
|
@ -26,3 +26,7 @@ def stub_dotfile!
|
||||||
dotfile = File.join(ENV["HOME"], ".aprc")
|
dotfile = File.join(ENV["HOME"], ".aprc")
|
||||||
File.should_receive(:readable?).at_least(:once).with(dotfile).and_return(false)
|
File.should_receive(:readable?).at_least(:once).with(dotfile).and_return(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Infinity Test runs tests as subprocesses, which sets STDOUT.tty? to false and
|
||||||
|
# would otherwise prematurely disallow colors. We'll test the defaults later.
|
||||||
|
AwesomePrint.force_colors!
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue