Skip to content

Commit

Permalink
Merge pull request #710 from tpope/dumb-terminal
Browse files Browse the repository at this point in the history
Don't use ANSI colors when terminal is dumb
  • Loading branch information
rafaelfranca authored Jan 22, 2020
2 parents fb625b2 + 21e1da2 commit 6610df3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
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

0 comments on commit 6610df3

Please sign in to comment.