Skip to content
This repository was archived by the owner on Feb 25, 2023. It is now read-only.

Commit d529605

Browse files
committed
lib/cli: Implement NO_COLOR support
When NO_COLOR is set in the environment, KBSecret will avoid using TTY colors in its output.
1 parent 3bf9624 commit d529605

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/kbsecret/cli.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ def guard
159159
# @param msg [String] the message to print
160160
# @return [void]
161161
def info(msg)
162-
self.class.stderr.puts "#{GREEN["Info"]}: #{msg}"
162+
info = ENV["NO_COLOR"] ? "Info" : GREEN["Info"]
163+
self.class.stderr.puts "#{info}: #{msg}"
163164
end
164165

165166
# Print an information message, but only if verbose output has been enabled.
@@ -184,25 +185,26 @@ def bye(msg)
184185
# @return [void]
185186
def warn(msg)
186187
return if @opts.no_warn?
187-
self.class.stderr.puts "#{YELLOW["Warning"]}: #{msg}"
188+
warning = ENV["NO_COLOR"] ? "Warning" : YELLOW["Warning"]
189+
self.class.stderr.puts "#{warning}: #{msg}"
188190
end
189191

190192
# Print an error message and terminate.
191193
# @param msg [String] the message to print
192194
# @return [void]
193195
# @note This method does not return!
194196
def die(msg)
195-
pretty = "#{RED["Fatal"]}: #{msg}"
196-
abort pretty
197+
fatal = ENV["NO_COLOR"] ? "Fatal" : RED["Fatal"]
198+
abort "#{fatal}: #{msg}"
197199
end
198200

199201
# Print an error message and terminate.
200202
# @param msg [String] the message to print
201203
# @return [void]
202204
# @note This method does not return!
203205
def self.die(msg)
204-
pretty = "#{RED["Fatal"]}: #{msg}"
205-
abort pretty
206+
fatal = ENV["NO_COLOR"] ? "Fatal" : RED["Fatal"]
207+
abort "#{fatal}: #{msg}"
206208
end
207209

208210
# Finds a reasonable default field separator by checking the environment first

0 commit comments

Comments
 (0)