mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Ported AwesomePrint.force_colors! and its specs
This commit is contained in:
parent
785109a66e
commit
8044c29b50
5 changed files with 115 additions and 86 deletions
|
@ -7,6 +7,13 @@ module AwesomePrint
|
||||||
|
|
||||||
class << self # Class accessors for custom defaults.
|
class << self # Class accessors for custom defaults.
|
||||||
attr_accessor :defaults, :force_colors
|
attr_accessor :defaults, :force_colors
|
||||||
|
|
||||||
|
# Class accessor to force colorized output (ex. forked subprocess where TERM
|
||||||
|
# might be dumb).
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
def force_colors!(value = true)
|
||||||
|
@force_colors = value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Inspector
|
class Inspector
|
||||||
|
@ -71,8 +78,8 @@ module AwesomePrint
|
||||||
# Return true if we are to colorize the output.
|
# Return true if we are to colorize the output.
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
def colorize?
|
def colorize?
|
||||||
@@force_colors ||= false
|
AwesomePrint.force_colors ||= false
|
||||||
@@force_colors || (STDOUT.tty? && ((ENV['TERM'] && ENV['TERM'] != 'dumb') || ENV['ANSICON']))
|
AwesomePrint.force_colors || (STDOUT.tty? && ((ENV['TERM'] && ENV['TERM'] != 'dumb') || ENV['ANSICON']))
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -1,84 +0,0 @@
|
||||||
# require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
||||||
#
|
|
||||||
# describe "AwesomePrint" do
|
|
||||||
# before(:each) do
|
|
||||||
# stub_dotfile!
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# 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
|
|
106
spec/colors_spec.rb
Normal file
106
spec/colors_spec.rb
Normal file
|
@ -0,0 +1,106 @@
|
||||||
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||||
|
|
||||||
|
describe "AwesomePrint" do
|
||||||
|
def stub_tty!(output = true, stream = STDOUT)
|
||||||
|
if output
|
||||||
|
stream.instance_eval { def tty?; true; end }
|
||||||
|
else
|
||||||
|
stream.instance_eval { def tty?; false; end }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
stub_dotfile!
|
||||||
|
end
|
||||||
|
|
||||||
|
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 do
|
||||||
|
ENV['TERM'] = "xterm-colors"
|
||||||
|
ENV.delete('ANSICON')
|
||||||
|
@arr = [ 1, :two, "three", [ nil, [ true, false] ] ]
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "default settings (no forced colors)" do
|
||||||
|
before do
|
||||||
|
AwesomePrint.force_colors! false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "colorizes tty processes by default" do
|
||||||
|
stub_tty!
|
||||||
|
@arr.ai(:multiline => false).should == COLORIZED
|
||||||
|
end
|
||||||
|
|
||||||
|
it "colorizes processes with ENV['ANSICON'] by default" do
|
||||||
|
begin
|
||||||
|
stub_tty!
|
||||||
|
term, ENV['ANSICON'] = ENV['ANSICON'], "1"
|
||||||
|
@arr.ai(:multiline => false).should == COLORIZED
|
||||||
|
ensure
|
||||||
|
ENV['ANSICON'] = term
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does not colorize tty processes running in dumb terminals by default" do
|
||||||
|
begin
|
||||||
|
stub_tty!
|
||||||
|
term, ENV['TERM'] = ENV['TERM'], "dumb"
|
||||||
|
@arr.ai(:multiline => false).should == PLAIN
|
||||||
|
ensure
|
||||||
|
ENV['TERM'] = term
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does not colorize subprocesses by default" do
|
||||||
|
begin
|
||||||
|
stub_tty! false
|
||||||
|
@arr.ai(:multiline => false).should == PLAIN
|
||||||
|
ensure
|
||||||
|
stub_tty!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "forced colors override" do
|
||||||
|
before do
|
||||||
|
AwesomePrint.force_colors!
|
||||||
|
end
|
||||||
|
|
||||||
|
it "still colorizes tty processes" do
|
||||||
|
stub_tty!
|
||||||
|
@arr.ai(:multiline => false).should == COLORIZED
|
||||||
|
end
|
||||||
|
|
||||||
|
it "colorizes processes with ENV['ANSICON'] set to 0" do
|
||||||
|
begin
|
||||||
|
stub_tty!
|
||||||
|
term, ENV['ANSICON'] = ENV['ANSICON'], "1"
|
||||||
|
@arr.ai(:multiline => false).should == COLORIZED
|
||||||
|
ensure
|
||||||
|
ENV['ANSICON'] = term
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "colorizes dumb terminals" do
|
||||||
|
begin
|
||||||
|
stub_tty!
|
||||||
|
term, ENV['TERM'] = ENV['TERM'], "dumb"
|
||||||
|
@arr.ai(:multiline => false).should == COLORIZED
|
||||||
|
ensure
|
||||||
|
ENV['TERM'] = term
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "colorizes subprocess" do
|
||||||
|
begin
|
||||||
|
stub_tty! false
|
||||||
|
@arr.ai(:multiline => false).should == COLORIZED
|
||||||
|
ensure
|
||||||
|
stub_tty!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
0
spec/objects_spec.rb
Normal file
0
spec/objects_spec.rb
Normal file
Loading…
Add table
Add a link
Reference in a new issue