module SimpleCov::Color
def colorize(text, color, enabled: enabled?)
keyword lets callers (e.g., CLI subcommands honoring `--no-color`)
Returns the bare text if color is disabled. The `enabled:`
Wrap `text` in the ANSI sequence for `color` (a key of ANSI).
def colorize(text, color, enabled: enabled?) return text unless enabled "#{ANSI.fetch(color)}#{text}#{ANSI.fetch(:reset)}" end
def colorize_percent(percent, text = nil, enabled: enabled?)
threshold band it falls into. Callers that want a different
Render `percent` as a fixed "NN.NN%" string colored by which
def colorize_percent(percent, text = nil, enabled: enabled?) colorize(text || format("%.2f%%", percent), for_percent(percent), enabled: enabled) end
def enabled?(stream = $stderr)
doesn't get ANSI sequences. See the module-level comment for
that print to stdout should pass `$stdout` so a redirected pipe
formatter writes to stderr, so that's the default. CLI subcommands
`stream` is the IO that the colorized text is destined for. The
def enabled?(stream = $stderr) # `SimpleCov.color` only exists once the full library is loaded. # The standalone CLI (`exe/simplecov`) loads `simplecov/color` # without `simplecov` itself to stay lightweight, so treat a # missing config the same as its `:auto` default: fall through to # the env vars and tty check below. config = SimpleCov.color if SimpleCov.respond_to?(: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
def env_set?(name)
def env_set?(name) value = ENV.fetch(name, nil) value && !value.empty? end
def for_percent(percent)
def for_percent(percent) return :green if percent >= GREEN_THRESHOLD return :yellow if percent >= YELLOW_THRESHOLD :red end