diff --git a/README.markdown b/README.markdown index 9cf886d..1b0cf1e 100644 --- a/README.markdown +++ b/README.markdown @@ -14,7 +14,7 @@ Colorize your text for the terminal # etc... ``` -## Supported Colors +## Supported Text Colors - `red` - `black` @@ -24,7 +24,23 @@ Colorize your text for the terminal - `white` - `blue` - `cyan` + +## Supported Background Colors + +- `red` +- `black` +- `green` +- `yellow` +- `magenta` +- `white` +- `blue` +- `cyan` + +## Supported Text Style + - `bold` +- `dim` +- `underlined` ## Other supported Ansi methods diff --git a/lib/colorator.rb b/lib/colorator.rb index 107f6e3..a6ab0dd 100644 --- a/lib/colorator.rb +++ b/lib/colorator.rb @@ -1,4 +1,5 @@ -$:.unshift File.dirname(__FILE__) +# $:.unshift File.dirname(__FILE__) +$LOAD_PATH.unshift File.dirname(__FILE__) module Colorator module_function @@ -7,7 +8,18 @@ module Colorator # -------------------------------------------------------------------------- ANSI_MATCHR = /\x1b.*?[jkmsuABGKH]/ - ANSI_COLORS = { + ANSI_BACKGROUND_COLORS = { + :bg_black => 40, + :bg_red => 41, + :bg_green => 42, + :bg_yellow => 43, + :bg_blue => 44, + :bg_magenta => 45, + :bg_cyan => 46, + :bg_white => 47 + } + + ANSI_TEXT_COLORS = { :black => 30, :red => 31, :green => 32, @@ -15,10 +27,17 @@ module Colorator :blue => 34, :magenta => 35, :cyan => 36, - :white => 37, - :bold => 1 + :white => 37 + } + + ANSI_TEXT_STYLE = { + :bold => 1, + :dim => 2, + :underlined => 4, } + ANSI_HASH = [ANSI_TEXT_COLORS, ANSI_BACKGROUND_COLORS, ANSI_TEXT_STYLE].inject(&:merge) + # -------------------------------------------------------------------------- # Allows you to check if a string currently has ansi. # -------------------------------------------------------------------------- @@ -85,7 +104,7 @@ def colorize(str = "", color) # -------------------------------------------------------------------------- - Colorator::ANSI_COLORS.each do |color, code| + Colorator::ANSI_HASH.each do |color, code| define_singleton_method color do |str| colorize( str, code diff --git a/spec/tests/lib/colorator_spec.rb b/spec/tests/lib/colorator_spec.rb index 9ecf249..9511f54 100644 --- a/spec/tests/lib/colorator_spec.rb +++ b/spec/tests/lib/colorator_spec.rb @@ -1,6 +1,6 @@ require "spec/helper" describe Colorator do - Colorator::ANSI_COLORS.each do |color, code| + Colorator::ANSI_HASH.each do |color, code| it "colors the text #{color} properly" do expect(Colorator.send(color, "string")).to include code.to_s expect(Colorator.send(color, "string")).to eq(