Permalink
Browse files

Standard colors with foreground and background

  • Loading branch information...
1 parent cd52f89 commit fc80a92ac4a2f97fe762326e8ed056a3b7206740 @randland committed Aug 14, 2011
Showing with 101 additions and 5 deletions.
  1. +2 −0 .rspec
  2. +1 −0 .rvmrc
  3. +4 −4 colorful.gemspec
  4. +41 −1 lib/colorful.rb
  5. +45 −0 spec/color_spec.rb
  6. +8 −0 spec/spec_helper.rb
View
2 .rspec
@@ -0,0 +1,2 @@
+-f Fuubar
+--color
View
1 .rvmrc
@@ -0,0 +1 @@
+rvm gemset use 1.9.2@colorful --create
View
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
s.authors = ["Nick Karpenske"]
s.email = ["randland@gmail.com"]
s.homepage = ""
- s.summary = %q{TODO: Write a gem summary}
- s.description = %q{TODO: Write a gem description}
+ s.summary = %q{Terminal Color Gem}
+ s.description = %q{Provides string extensions for terminal color output}
s.rubyforge_project = "colorful"
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
- # specify any dependencies here; for example:
- # s.add_development_dependency "rspec"
+ s.add_development_dependency "rspec"
+ s.add_development_dependency "fuubar"
# s.add_runtime_dependency "rest-client"
end
View
@@ -1,5 +1,45 @@
require "colorful/version"
module Colorful
- # Your code goes here...
+ COLORS = { :black => 0,
+ :red => 1,
+ :green => 2,
+ :yellow => 3,
+ :blue => 4,
+ :magenta => 5,
+ :cyan => 6,
+ :white => 7,
+ :default => 9 }
+end
+
+class String
+ Colorful::COLORS.each do |color, code|
+ define_method color do
+ inject_ansi_code 30 + code
+ end
+
+ define_method "#{color}_background" do
+ inject_ansi_code 40 + code
+ end
+
+ Colorful::COLORS.each do |bg_color, bg_code|
+ define_method "#{color}_on_#{bg_color}" do
+ inject_ansi_code 30 + code, 40 + bg_code
+ end
+ end
+ end
+
+ private
+ def inject_ansi_code *codes
+ result = self
+ codes.each do |code|
+ if result =~ /^\e\[[0-9;]*m/
+ terminator = result.index('m')
+ result = result.insert(terminator, ";#{code}")
+ else
+ result = "\e[#{code}m#{result}\e[m"
+ end
+ end
+ result
+ end
end
View
@@ -0,0 +1,45 @@
+require 'spec_helper'
+
+def color_codes
+ { :black => 0,
+ :red => 1,
+ :green => 2,
+ :yellow => 3,
+ :blue => 4,
+ :magenta => 5,
+ :cyan => 6,
+ :white => 7,
+ :default => 9 }
+end
+
+describe String do
+ let(:string) { "This is a test"}
+
+ context 'colors' do
+ color_codes.each do |color, code|
+
+ describe "##{color}" do
+ subject { string.send color }
+ it { should == "\e[3#{code}m#{string}\e[m" }
+ end
+
+ describe "##{color}_background" do
+ subject { string.send "#{color}_background" }
+ it { should == "\e[4#{code}m#{string}\e[m" }
+ end
+
+ color_codes.each do |bg_color, bg_code|
+ describe "##{color}_on_#{bg_color}" do
+ subject { string.send "#{color}_on_#{bg_color}" }
+ it { should == "\e[3#{code};4#{bg_code}m#{string}\e[m" }
+ end
+
+ describe "##{color}.#{bg_color}_background" do
+ subject { string.send("#{color}").send("#{bg_color}_background") }
+ it { should == "\e[3#{code};4#{bg_code}m#{string}\e[m" }
+ end
+ end
+ end
+ end
+
+end
View
@@ -0,0 +1,8 @@
+require 'rubygems'
+require 'bundler/setup'
+
+require 'colorful'
+
+RSpec.configure do |config|
+end
+

0 comments on commit fc80a92

Please sign in to comment.