Sha256: 83d644a970b31c90a910e646ede1eeb2710d91e66c49a8aabf4c3176d02a7678

Contents?: true

Size: 951 Bytes

Versions: 2

Compression:

Stored size: 951 Bytes

Contents

module PrettyConsoleOutput
  class Color
    COLOR_CODES = {
      black:         30,
      red:           31,
      green:         32,
      yellow:        33,
      blue:          34,
      magenta:       35,
      cyan:          36,
      white:         37,
      light_black:   90,
      light_red:     91,
      light_green:   92,
      light_yellow:  93,
      light_blue:    94,
      light_magenta: 95,
      light_cyan:    96,
      light_white:   97
    }.freeze

    def initialize(output)
      @output = output
    end

    def colorize(obj, color, mode={})
      string = obj.to_s

      return string unless colorize?
      return string unless COLOR_CODES[color]

      result = mode[:bold] ? "\e[1;" : "\e[0;"
      result << "4;" unless mode[:underscore].nil?
      result << COLOR_CODES[color].to_s
      result << ";49m#{string}\e[0m"
    end

    def colorize?
      (@output.respond_to?(:tty?) && @output.tty?)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pretty_console_output-1.0.0 lib/pretty_console_output/color.rb
pretty_console_output-0.9.0 lib/pretty_console_output/color.rb