Module: Dry::CLI::Help::Colors

Defined in:
lib/dry/cli/help/colors.rb,
sig/dry/cli/help.rbs

Overview

Every pastel style as a method. Include it, or extend a module with it:

include Dry::CLI::Help::Colors

red("text")          # => decorated text
red.bold("text")     # => chained styles

Whether the methods decorate is one process-wide switch, Colors.enabled=. Help screens do not read it: they follow the color setting instead.

Constant Summary collapse

HUES =
%i[black red green yellow blue magenta cyan white].freeze
FOREGROUNDS =
(HUES + HUES.map { :"bright_#{it}" }).freeze
BACKGROUNDS =
FOREGROUNDS.map { :"on_#{it}" }.freeze
MODIFIERS =
%i[clear bold dim italic underline inverse hidden strikethrough].freeze
STYLES =

Returns:

  • (Array[Symbol])
(FOREGROUNDS + BACKGROUNDS + MODIFIERS).freeze
SETTINGS =

Values the color switch and the color setting accept.

Returns:

  • (Array[bool | Symbol])
[true, false, :auto].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.enabled ⇒ Boolean, Symbol

Returns true, false or :auto.

Returns:

  • (Boolean, Symbol) —

    true, false or :auto



29
30
31
# File 'lib/dry/cli/help/colors.rb', line 29

def enabled
  @enabled
end

Class Method Details

.enabled_for?(setting, io) ⇒ Boolean

:auto colors a terminal unless NO_COLOR is set to anything but the empty string. https://no-color.org

Parameters:

  • setting (Boolean, Symbol) —

    true, false or :auto

  • io (IO, #tty?) —

    where the text is going

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/dry/cli/help/colors.rb', line 51

def enabled_for?(setting, io)
  return setting unless setting == :auto

  io.respond_to?(:tty?) && io.tty? && ENV.fetch("NO_COLOR", "").empty?
end

.pastel ⇒ Pastel

Returns shared by every includer, rebuilt when the switch changes.

Returns:

  • (Pastel) —

    shared by every includer, rebuilt when the switch changes



41
42
43
# File 'lib/dry/cli/help/colors.rb', line 41

def pastel
  @pastel ||= ::Pastel.new(enabled: enabled_for?(enabled, $stdout))
end

Instance Method Details

#pastel ⇒ Pastel

Returns:

  • (Pastel)


59
60
61
# File 'lib/dry/cli/help/colors.rb', line 59

def pastel
  Colors.pastel
end