Skip to content

Commit 21e1da2

Browse files
committed
Don't use ANSI colors when terminal is dumb
1 parent fb625b2 commit 21e1da2

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/thor/shell/color.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ def set_color(string, *colors)
9797
protected
9898

9999
def can_display_colors?
100-
stdout.tty? && !are_colors_disabled?
100+
are_colors_supported? && !are_colors_disabled?
101+
end
102+
103+
def are_colors_supported?
104+
stdout.tty? && ![nil, "dumb"].include?(ENV["TERM"])
101105
end
102106

103107
def are_colors_disabled?

spec/shell/color_spec.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ def shell
77

88
before do
99
allow($stdout).to receive(:tty?).and_return(true)
10+
allow(ENV).to receive(:[]).and_return(nil)
11+
allow(ENV).to receive(:[]).with("TERM").and_return("ansi")
1012
allow_any_instance_of(StringIO).to receive(:tty?).and_return(true)
1113
end
1214

@@ -131,13 +133,25 @@ def shell
131133
expect(colorless).to eq("hi!")
132134
end
133135

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

140-
it "does nothing when the terminal has the NO_COLOR environment variable set" do
142+
it "does nothing when the TERM environment variable is set to 'dumb'" do
143+
allow(ENV).to receive(:[]).with("TERM").and_return("dumb")
144+
colorless = shell.set_color "hi!", :white
145+
expect(colorless).to eq("hi!")
146+
end
147+
148+
it "does nothing when the TERM environment variable is not set" do
149+
allow(ENV).to receive(:[]).with("TERM").and_return(nil)
150+
colorless = shell.set_color "hi!", :white
151+
expect(colorless).to eq("hi!")
152+
end
153+
154+
it "does nothing when the NO_COLOR environment variable is set" do
141155
allow(ENV).to receive(:[]).with("NO_COLOR").and_return("")
142156
allow($stdout).to receive(:tty?).and_return(true)
143157
colorless = shell.set_color "hi!", :white

0 commit comments

Comments
 (0)