Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/thor/shell/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ def set_color(string, *colors)
protected

def can_display_colors?
stdout.tty? && !are_colors_disabled?
are_colors_supported? && !are_colors_disabled?
end

def are_colors_supported?
stdout.tty? && ![nil, "dumb"].include?(ENV["TERM"])
end

def are_colors_disabled?
Expand Down
18 changes: 16 additions & 2 deletions spec/shell/color_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def shell

before do
allow($stdout).to receive(:tty?).and_return(true)
allow(ENV).to receive(:[]).and_return(nil)
allow(ENV).to receive(:[]).with("TERM").and_return("ansi")
allow_any_instance_of(StringIO).to receive(:tty?).and_return(true)
end

Expand Down Expand Up @@ -131,13 +133,25 @@ def shell
expect(colorless).to eq("hi!")
end

it "does nothing when the terminal does not support color" do
it "does nothing when stdout is not a tty" do
allow($stdout).to receive(:tty?).and_return(false)
colorless = shell.set_color "hi!", :white
expect(colorless).to eq("hi!")
end

it "does nothing when the terminal has the NO_COLOR environment variable set" do
it "does nothing when the TERM environment variable is set to 'dumb'" do
allow(ENV).to receive(:[]).with("TERM").and_return("dumb")
colorless = shell.set_color "hi!", :white
expect(colorless).to eq("hi!")
end

it "does nothing when the TERM environment variable is not set" do
allow(ENV).to receive(:[]).with("TERM").and_return(nil)
colorless = shell.set_color "hi!", :white
expect(colorless).to eq("hi!")
end

it "does nothing when the NO_COLOR environment variable is set" do
allow(ENV).to receive(:[]).with("NO_COLOR").and_return("")
allow($stdout).to receive(:tty?).and_return(true)
colorless = shell.set_color "hi!", :white
Expand Down