Sha256: bbf987bda846e5cbc6cc1010443dda4d6d7571bf10ffdc9bf404097b36c3748d

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

require 'win32console' if RUBY_PLATFORM =~ /(win|m)32/      # ruby color support for win

##
# Add colors
#
class String
  # colorize(:red)
  def colorize(args)
    case args
    when Symbol
      Colorizer.send(args, self)
    when Hash
      Colorizer.send(args[:color], self, args[:mode])
    end
  end

  # Used to colorize strings for the shell
  class Colorizer
    # Returns colors integer mapping
    def self.colors
      @_colors ||= {
        :default => 9,
        :black   => 30,
        :red     => 31,
        :green   => 32,
        :yellow  => 33,
        :blue    => 34,
        :magenta => 35,
        :cyan    => 36,
        :white   => 37
      }
    end

    # Returns modes integer mapping
    def self.modes
      @_modes ||= {
        :default => 0,
        :bold    => 1
      }
    end

    # Defines class level color methods
    # i.e  Colorizer.red("hello")
    class << self
      Colorizer.colors.each do |color, value|
        define_method(color) do |target, mode_name = :default|
          mode = modes[mode_name] || modes[:default]
          "\e[#{mode};#{value}m" << target << "\e[0m"
         end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
padrino-support-0.12.3 lib/padrino-support/core_ext/string/colorize.rb
padrino-support-0.12.2 lib/padrino-support/core_ext/string/colorize.rb