Skip to content

Commit 401d091

Browse files
committed
respect NO_COLOR environment variable
When `NO_COLOR` is set to any non-nil value, output is not colorized. See https://no-color.org/
1 parent e0e3a4d commit 401d091

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/irb/init.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def IRB.init_config(ap_path)
4444
@CONF[:IRB_RC] = nil
4545

4646
@CONF[:USE_SINGLELINE] = false unless defined?(ReadlineInputMethod)
47-
@CONF[:USE_COLORIZE] = true
47+
@CONF[:USE_COLORIZE] = !ENV['NO_COLOR']
4848
@CONF[:INSPECT_MODE] = true
4949
@CONF[:USE_TRACER] = false
5050
@CONF[:USE_LOADER] = false
@@ -224,11 +224,11 @@ def IRB.parse_opts(argv: ::ARGV)
224224
break
225225
end
226226
end
227+
227228
load_path.collect! do |path|
228229
/\A\.\// =~ path ? path : File.expand_path(path)
229230
end
230231
$LOAD_PATH.unshift(*load_path)
231-
232232
end
233233

234234
# running config

test/irb/test_init.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ def test_rc_file_in_subdir
6060
ENV["IRBRC"] = backup_irbrc
6161
end
6262

63+
def test_no_color_environment_variable
64+
orig = ENV['NO_COLOR']
65+
66+
assert IRB.conf[:USE_COLORIZE]
67+
68+
ENV['NO_COLOR'] = 'true'
69+
IRB.setup(eval("__FILE__"))
70+
refute IRB.conf[:USE_COLORIZE]
71+
72+
ENV['NO_COLOR'] = nil
73+
IRB.setup(eval("__FILE__"))
74+
assert IRB.conf[:USE_COLORIZE]
75+
ensure
76+
ENV['NO_COLOR'] = orig
77+
end
78+
6379
private
6480

6581
def with_argv(argv)

0 commit comments

Comments
 (0)