Module: Philiprehberger::CliKit::Colorize
- Defined in:
- lib/philiprehberger/cli_kit/colorize.rb
Overview
ANSI color and style helpers. Output is auto-disabled when stdout is not a TTY or the NO_COLOR environment variable is set (per no-color.org).
Constant Summary collapse
- CODES =
{ red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37, gray: 90 }.freeze
Class Method Summary collapse
- .bold(text) ⇒ String
-
.color(text, name) ⇒ String
Wraps text in an ANSI color escape, or returns it untouched when colors are disabled.
- .dim(text) ⇒ String
-
.enabled? ⇒ Boolean
True when ANSI escape codes should be emitted.
Class Method Details
.bold(text) ⇒ String
37 38 39 40 41 |
# File 'lib/philiprehberger/cli_kit/colorize.rb', line 37 def bold(text) return text unless enabled? "\e[1m#{text}\e[0m" end |
.color(text, name) ⇒ String
Wraps text in an ANSI color escape, or returns it untouched when colors are disabled.
28 29 30 31 32 33 |
# File 'lib/philiprehberger/cli_kit/colorize.rb', line 28 def color(text, name) return text unless enabled? code = CODES.fetch(name) { raise ArgumentError, "unknown color: #{name.inspect}" } "\e[#{code}m#{text}\e[0m" end |
.dim(text) ⇒ String
45 46 47 48 49 |
# File 'lib/philiprehberger/cli_kit/colorize.rb', line 45 def dim(text) return text unless enabled? "\e[2m#{text}\e[0m" end |
.enabled? ⇒ Boolean
Returns true when ANSI escape codes should be emitted.
16 17 18 19 20 |
# File 'lib/philiprehberger/cli_kit/colorize.rb', line 16 def enabled? return false if ENV.key?('NO_COLOR') $stdout.tty? end |