Module: Booker::Colors

Defined in:
lib/booker/output.rb

Overview

"Success: ".grn at every call site, but as a refinement rather than an open class: a gem claiming String#red for the whole process is taking a name it does not own. files that want these say using Booker::Colors at the top

Constant Summary collapse

CODES =
{red: 31, grn: 32, yel: 33, blu: 34, cyan: 36}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enabled=(value) ⇒ Object (writeonly)

nil means decide per call from the environment; true or false forces it, which is what the specs do since their $stdout is never a terminal



43
44
45
# File 'lib/booker/output.rb', line 43

def enabled=(value)
  @enabled = value
end

Class Method Details

.enabled?Boolean

honor NO_COLOR (no-color.org), and skip the escapes when stdout is not a terminal - otherwise booker > out.txt and booker | less fill up with raw \033[ sequences

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/booker/output.rb', line 48

def enabled?
  return @enabled unless @enabled.nil?

  ENV["NO_COLOR"].to_s.empty? && $stdout.tty?
end

.paint(str, name) ⇒ Object

color by name, reachable without the refinement. a caller holding the color as data - the browser table's :yel, say - cannot go through the refined methods, because refinements are invisible to send



57
58
59
60
61
# File 'lib/booker/output.rb', line 57

def paint(str, name)
  return str unless enabled?

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