Module: HeapScope::CLI::Color

Defined in:
lib/heapscope/cli/color.rb

Overview

Optional ANSI color for TTY output. Respects NO_COLOR and --no-color.

Constant Summary collapse

RESET =
"\e[0m"
BOLD =
"\e[1m"
DIM =
"\e[2m"
RED =
"\e[31m"
GREEN =
"\e[32m"
YELLOW =
"\e[33m"
CYAN =
"\e[36m"
MAGENTA =
"\e[35m"

Class Method Summary collapse

Class Method Details

.accent(text, enabled:) ⇒ Object



58
59
60
# File 'lib/heapscope/cli/color.rb', line 58

def accent(text, enabled:)
  wrap(text, CYAN, enabled: enabled)
end

.bold(text, enabled:) ⇒ Object



62
63
64
# File 'lib/heapscope/cli/color.rb', line 62

def bold(text, enabled:)
  wrap(text, BOLD, enabled: enabled)
end

.enabled?(force: nil) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
# File 'lib/heapscope/cli/color.rb', line 18

def enabled?(force: nil)
  return force unless force.nil?
  return false if ENV["NO_COLOR"] && !ENV["NO_COLOR"].empty?
  return false if ENV["TERM"].to_s == "dumb"

  $stdout.tty?
rescue StandardError
  false
end

.err(text, enabled:) ⇒ Object



54
55
56
# File 'lib/heapscope/cli/color.rb', line 54

def err(text, enabled:)
  wrap(text, RED, enabled: enabled)
end

.ok(text, enabled:) ⇒ Object



46
47
48
# File 'lib/heapscope/cli/color.rb', line 46

def ok(text, enabled:)
  wrap(text, GREEN, enabled: enabled)
end

.severity(sev, enabled:) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/heapscope/cli/color.rb', line 34

def severity(sev, enabled:)
  s = sev.to_s.upcase
  code =
    case sev.to_s.downcase.to_sym
    when :high then RED
    when :medium then YELLOW
    when :low then CYAN
    else DIM
    end
  wrap(s, code, enabled: enabled)
end

.warn_text(text, enabled:) ⇒ Object



50
51
52
# File 'lib/heapscope/cli/color.rb', line 50

def warn_text(text, enabled:)
  wrap(text, YELLOW, enabled: enabled)
end

.wrap(text, code, enabled:) ⇒ Object



28
29
30
31
32
# File 'lib/heapscope/cli/color.rb', line 28

def wrap(text, code, enabled:)
  return text.to_s unless enabled && code

  "#{code}#{text}#{RESET}"
end