Class: TogoWS::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/togows/color.rb

Constant Summary collapse

STYLES =
{
  bold: 1
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enabled) ⇒ Color

Returns a new instance of Color.



9
10
11
# File 'lib/togows/color.rb', line 9

def initialize(enabled)
  @enabled = enabled
end

Class Method Details

.enabled_for?(io) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/togows/color.rb', line 13

def self.enabled_for?(io)
  io.respond_to?(:tty?) && io.tty? && !ENV.key?("NO_COLOR")
end

Instance Method Details

#apply(text, *styles) ⇒ Object



17
18
19
20
21
22
# File 'lib/togows/color.rb', line 17

def apply(text, *styles)
  return text unless @enabled

  codes = styles.map { |style| STYLES.fetch(style) }
  "\e[#{codes.join(';')}m#{text}\e[0m"
end

#bold(text) ⇒ Object



24
25
26
# File 'lib/togows/color.rb', line 24

def bold(text)
  apply(text, :bold)
end