Sha256: 92798699247b4aa41b5b9c0dbed247376dab640b2e7abd1fb9dbb1a3a422dfbf

Contents?: true

Size: 717 Bytes

Versions: 5

Compression:

Stored size: 717 Bytes

Contents

#! /usr/bin/env ruby
# coding: utf-8

class String
  COLOR_CODE = {
    :black   => 0, :red     => 1, :green   => 2, :yellow  => 3,
    :blue    => 4, :magenta => 5, :cyan    => 6, :white   => 7,
    :default => 9,
  }

  #Add escape characters at head and tail of self.
  #Foreground and background can be indicated by argued symbols.
  # 0: black
  # 1: red
  # 2: green
  # 3: yellow
  # 4: blue
  # 5: magenta
  # 6: cyan
  # 7: white
  # 9: default
  # Argument bg indicates background color. If not indicated, default color.
  # Argument fg indicates foreground color. This must be indicated.
  def color( fg, bg = :default )
    "\e[3#{COLOR_CODE[ fg ]};4#{COLOR_CODE[ bg ]}m" + self + "\e[39;49m"
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
builtinextension-0.1.4 lib/string/color.rb
builtinextension-0.1.3 lib/string/color.rb
builtinextension-0.1.2 lib/string/color.rb
builtinextension-0.1.1 lib/string/color.rb
builtinextension-0.1.0 lib/string/color.rb