Module: Portless::Colors

Extended by:
Colors
Included in:
Colors
Defined in:
lib/portless/colors.rb

Overview

ANSI colours for CLI output. A no-op when the target stream isn't a TTY (so piped/redirected output stays clean) or when NO_COLOR is set. Mirrors portless's colors helper.

Constant Summary collapse

CODES =
{ bold: 1, dim: 90, gray: 90, red: 31, green: 32, yellow: 33, blue: 34, cyan: 36 }.freeze

Instance Method Summary collapse

Instance Method Details

#enabled?(io) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/portless/colors.rb', line 22

def enabled?(io)
  ENV["NO_COLOR"].to_s.empty? && io.respond_to?(:tty?) && io.tty?
end

#paint(name, str, io: $stdout) ⇒ Object



16
17
18
19
20
# File 'lib/portless/colors.rb', line 16

def paint(name, str, io: $stdout)
  return str.to_s unless enabled?(io)

  "\e[#{CODES.fetch(name)}m#{str}\e[0m"
end