Module: SimpleCov::Color
- Defined in:
- lib/simplecov/color.rb
Overview
ANSI colorization for stderr diagnostics. Thresholds mirror the HTML formatter (>= 90 green, >= 75 yellow, otherwise red) so a team's mental model of "what's the cutoff" is the same whether they're reading the terminal output or the HTML report.
Color precedence, highest first:
SimpleCov.color = true/false(programmatic override, wins over everything; default is:autowhich falls through)NO_COLORenv var (any non-empty value) → off (see no-color.org)FORCE_COLORenv var (any non-empty value) → onstream.tty?fallback
NO_COLOR wins over FORCE_COLOR if both env vars are set.
Constant Summary collapse
- GREEN_THRESHOLD =
90- YELLOW_THRESHOLD =
75- ANSI =
{ red: "\e[31m", yellow: "\e[33m", green: "\e[32m", reset: "\e[0m" }.freeze
Class Method Summary collapse
-
.colorize(text, color, enabled: enabled?) ) ⇒ Object
Wrap
textin the ANSI sequence forcolor(a key of ANSI). -
.colorize_percent(percent, text = nil, enabled: enabled?) ) ⇒ Object
Render
percentas a fixed "NN.NN%" string colored by which threshold band it falls into. -
.enabled?(stream = $stderr) ⇒ Boolean
streamis the IO that the colorized text is destined for. - .env_set?(name) ⇒ Boolean
- .for_percent(percent) ⇒ Object
Class Method Details
.colorize(text, color, enabled: enabled?) ) ⇒ Object
Wrap text in the ANSI sequence for color (a key of ANSI).
Returns the bare text if color is disabled. The enabled:
keyword lets callers (e.g., CLI subcommands honoring --no-color)
override the auto-detection without touching env vars.
56 57 58 59 60 |
# File 'lib/simplecov/color.rb', line 56 def colorize(text, color, enabled: enabled?) return text unless enabled "#{ANSI.fetch(color)}#{text}#{ANSI.fetch(:reset)}" end |
.colorize_percent(percent, text = nil, enabled: enabled?) ) ⇒ Object
Render percent as a fixed "NN.NN%" string colored by which
threshold band it falls into. Callers that want a different
rendering of the number can pass the pre-rendered text.
65 66 67 |
# File 'lib/simplecov/color.rb', line 65 def colorize_percent(percent, text = nil, enabled: enabled?) colorize(text || format("%.2f%%", percent), for_percent(percent), enabled: enabled) end |
.enabled?(stream = $stderr) ⇒ Boolean
stream is the IO that the colorized text is destined for. The
formatter writes to stderr, so that's the default. CLI subcommands
that print to stdout should pass $stdout so a redirected pipe
doesn't get ANSI sequences. See the module-level comment for
precedence.
36 37 38 39 40 41 42 43 |
# File 'lib/simplecov/color.rb', line 36 def enabled?(stream = $stderr) config = SimpleCov.color return config if [true, false].include?(config) return false if env_set?("NO_COLOR") return true if env_set?("FORCE_COLOR") stream.tty? end |
.env_set?(name) ⇒ Boolean
69 70 71 72 |
# File 'lib/simplecov/color.rb', line 69 def env_set?(name) value = ENV.fetch(name, nil) value && !value.empty? end |
.for_percent(percent) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/simplecov/color.rb', line 45 def for_percent(percent) return :green if percent >= GREEN_THRESHOLD return :yellow if percent >= YELLOW_THRESHOLD :red end |