Sha256: 014e5c0091f04614f51bb96f419bbbbe666ee009e3c950fad4b15b95efb2ecfa

Contents?: true

Size: 1.64 KB

Versions: 7

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module Hanami
  module Utils
    # Shell helper for colorizing STDOUT
    #
    # It doesn't check if you're writing to a file or anything, so you have to
    # check that yourself before using this module.
    #
    # @since 1.2.0
    module ShellColor
      # Unknown color code error
      #
      # @since 1.2.0
      class UnknownColorCodeError < ::StandardError
        def initialize(code)
          super("unknown color code: `#{code.inspect}'")
        end
      end

      # Escapes codes for terminals to output strings in colors
      #
      # @since 1.2.0
      # @api private
      COLORS = ::Hash[
        black: 30,
        red: 31,
        green: 32,
        yellow: 33,
        blue: 34,
        magenta: 35,
        cyan: 36,
        gray: 37,
      ].freeze

      # Colorizes output
      # 8 colors available: black, red, green, yellow, blue, magenta, cyan, and gray
      #
      # @param input [#to_s] the string to colorize
      # @param color [Symbol] the color
      #
      # @raise [Hanami::Utils::ShellColor::UnknownColorError] if the color code is
      #   unknown
      #
      # @return [String] the colorized string
      #
      # @since 1.2.0
      def self.call(input, color:)
        "\e[#{color_code(color)}m#{input}\e[0m"
      end

      # Helper method to translate between color names and terminal escape codes
      #
      # @api private
      # @since 1.2.0
      #
      # @raise [Hanami::Utils::ShellColor::UnknownColorError] if the color code is
      #   unknown
      def self.color_code(code)
        COLORS.fetch(code) { raise UnknownColorCodeError.new(code) }
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hanami-utils-2.0.0.rc1 lib/hanami/utils/shell_color.rb
hanami-utils-2.0.0.beta1 lib/hanami/utils/shell_color.rb
hanami-utils-2.0.0.alpha6 lib/hanami/utils/shell_color.rb
hanami-utils-2.0.0.alpha3 lib/hanami/utils/shell_color.rb
hanami-utils-2.0.0.alpha2 lib/hanami/utils/shell_color.rb
hanami-utils-1.3.8 lib/hanami/utils/shell_color.rb
hanami-utils-1.3.7 lib/hanami/utils/shell_color.rb