Sha256: 0de65d0f21440fba884e55945cfb79f7e3fb4f06e7bd714800b67a5b2f6ef03a

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

module Polytrix
  module Color
    ANSI = {
      reset: 0, black: 30, red: 31, green: 32, yellow: 33,
      blue: 34, magenta: 35, cyan: 36, white: 37,
      bright_black: 90, bright_red: 91, bright_green: 92,
      bright_yellow: 93, bright_blue: 94, bright_magenta: 95,
      bright_cyan: 96, bright_white: 97
    }.freeze

    COLORS = %w[
      cyan yellow green magenta blue bright_cyan bright_yellow
      bright_green bright_magenta bright_blue
    ].freeze

    # Returns an ansi escaped string representing a color control sequence.
    #
    # @param name [Symbol] a valid color representation, taken from
    #   Polytrix::Color::ANSI
    # @return [String] an ansi escaped string if the color is valid and an
    #   empty string otherwise
    def self.escape(name)
      return '' if name.nil?
      return '' unless ANSI[name]
      "\e[#{ANSI[name]}m"
    end

    # Returns a colorized ansi escaped string with the given color.
    #
    # @param str [String] a string to colorize
    # @param name [Symbol] a valid color representation, taken from
    #   Polytrix::Color::ANSI
    # @return [String] an ansi escaped string if the color is valid and an
    #   unescaped string otherwise
    def self.colorize(str, name)
      color = escape(name)
      color.empty? ? str : "#{color}#{str}#{escape(:reset)}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
polytrix-0.1.2 lib/polytrix/color.rb
polytrix-0.1.1 lib/polytrix/color.rb
polytrix-0.1.0 lib/polytrix/color.rb