Sha256: 68016dc445ac010903f518fe98c083693ad329fcd394b8bf30533107a412f197

Contents?: true

Size: 1.91 KB

Versions: 14

Compression:

Stored size: 1.91 KB

Contents

require 'cli/ui'

module CLI
  module UI
    class Color
      attr_reader :sgr, :name, :code

      # Creates a new color mapping
      # Signatures can be found here:
      # https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
      #
      # ==== Attributes
      #
      # * +sgr+ - The color signature
      # * +name+ - The name of the color
      #
      def initialize(sgr, name)
        @sgr  = sgr
        @code = CLI::UI::ANSI.sgr(sgr)
        @name = name
      end

      RED     = new('31', :red)
      GREEN   = new('32', :green)
      YELLOW  = new('33', :yellow)
      # default blue is low-contrast against black in some default terminal color scheme
      BLUE    = new('94', :blue) # 9x = high-intensity fg color x
      MAGENTA = new('35', :magenta)
      CYAN    = new('36', :cyan)
      RESET   = new('0',  :reset)
      BOLD    = new('1',  :bold)
      WHITE   = new('97', :white)

      MAP = {
        red:     RED,
        green:   GREEN,
        yellow:  YELLOW,
        blue:    BLUE,
        magenta: MAGENTA,
        cyan:    CYAN,
        reset:   RESET,
        bold:    BOLD,
      }.freeze

      class InvalidColorName < ArgumentError
        def initialize(name)
          @name = name
        end

        def message
          keys = Color.available.map(&:inspect).join(',')
          "invalid color: #{@name.inspect} " \
            "-- must be one of CLI::UI::Color.available (#{keys})"
        end
      end

      # Looks up a color code by name
      #
      # ==== Raises
      # Raises a InvalidColorName if the color is not available
      # You likely need to add it to the +MAP+ or you made a typo
      #
      # ==== Returns
      # Returns a color code
      #
      def self.lookup(name)
        MAP.fetch(name)
      rescue KeyError
        raise InvalidColorName, name
      end

      # All available colors by name
      #
      def self.available
        MAP.keys
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
shopify-cli-0.9.2 vendor/deps/cli-ui/lib/cli/ui/color.rb
shopify-cli-0.9.1 vendor/deps/cli-ui/lib/cli/ui/color.rb
shopify-cli-0.9.0 vendor/deps/cli-ui/lib/cli/ui/color.rb
cli-ui-1.2.3 lib/cli/ui/color.rb
cli-ui-1.2.2 lib/cli/ui/color.rb
cli-ui-1.2.0 lib/cli/ui/color.rb
cli-ui-1.2.1 lib/cli/ui/color.rb
cli-ui-1.1.4 lib/cli/ui/color.rb
cli-ui-1.1.3 lib/cli/ui/color.rb
cli-ui-1.1.2 lib/cli/ui/color.rb
cli-ui-1.1.1 lib/cli/ui/color.rb
cli-ui-1.1.0 lib/cli/ui/color.rb
cli-ui-1.0.0 lib/cli/ui/color.rb
cli-ui-0.1.2 lib/cli/ui/color.rb