Module: CommandKit::Colors
Overview
Defines ANSI color codes.
Examples
include CommandKit::Colors
def run
puts colors.green("hello world")
end
Printing color error messages
stderr.puts colors(stderr).red("error!")
Environment Variables
TERM
- Specifies the type of terminal. When set toDUMB
, it will disable color output.
Alternatives
Defined Under Namespace
Instance Attribute Summary
Attributes included from Env
Instance Method Summary collapse
-
#ansi?(stream = stdout) ⇒ Boolean
Checks if the stream supports ANSI output.
-
#colors(stream = stdout) {|color| ... } ⇒ ANSI, PlainText
Returns the colors available for the given stream.
Methods included from Env
Methods included from Stdio
#abort, #gets, #initialize, #print, #printf, #putc, #puts, #readline, #readlines, #stderr, #stdin, #stdout
Instance Method Details
#ansi?(stream = stdout) ⇒ Boolean
Note:
When the env variable TERM
is set to dumb
, it will disable color
output. Color output will also be disabled if the given stream is not
a TTY.
Checks if the stream supports ANSI output.
358 359 360 |
# File 'lib/command_kit/colors.rb', line 358 def ansi?(stream=stdout) env['TERM'] != 'dumb' && stream.tty? end |
#colors(stream = stdout) {|color| ... } ⇒ ANSI, PlainText
Returns the colors available for the given stream.
378 379 380 381 382 383 384 385 |
# File 'lib/command_kit/colors.rb', line 378 def colors(stream=stdout) color = if ansi?(stream) then ANSI else PlainText end yield color if block_given? return color end |