Skip to content

Commit

Permalink
Merge pull request #27 from fazibear/code-climate
Browse files Browse the repository at this point in the history
Code climate cleanups
  • Loading branch information
fazibear committed Dec 10, 2014
2 parents 84ea4a3 + 7ec7294 commit 93752d4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 44 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
== 0.7.23 / 2014-05-19
== 0.7.4 / 2014-12-10
* code cleanups

== 0.7.3 / 2014-05-19
* fix new line maching

== 0.7.2 / 2014-04-08
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
colorize [![Gem Version](https://badge.fury.io/rb/colorize.svg)](http://badge.fury.io/rb/colorize) [![Build Status](https://travis-ci.org/fazibear/colorize.svg?branch=master)](https://travis-ci.org/fazibear/colorize)
colorize [![Gem Version](https://badge.fury.io/rb/colorize.svg)](http://badge.fury.io/rb/colorize) [![Build Status](https://travis-ci.org/fazibear/colorize.svg?branch=master)](https://travis-ci.org/fazibear/colorize) [![Code Climate](https://codeclimate.com/github/fazibear/colorize/badges/gpa.svg)](https://codeclimate.com/github/fazibear/colorize)
========

Ruby String class extension. Adds methods to set text color, background color and, text effects on ruby console and command line output, using ANSI escape sequences.
Expand Down Expand Up @@ -30,8 +30,7 @@ Class methods:

String.colors - return array of all possible colors names
String.modes - return array of all possible modes
String.color_matrix - displays color matrix with color names
String.color_matrix("FOO") - display color matrix with "FOO" text
String.color_samples - displays color samples in all combinations

requirements
------------
Expand Down
2 changes: 1 addition & 1 deletion colorize.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'colorize'
s.version = '0.7.3'
s.version = '0.7.4'

s.authors = ['fazibear']
s.email = 'fazibear@gmail.com'
Expand Down
108 changes: 69 additions & 39 deletions lib/colorize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class String
REGEXP_PATTERN = /\033\[([0-9]+);([0-9]+);([0-9]+)m(.+?)\033\[0m|([^\033]+)/m
COLOR_OFFSET = 30
BACKGROUND_OFFSET = 40

public

#
Expand All @@ -62,26 +62,11 @@ class String
# puts "This is uncolorized".blue.on_red.uncolorize
#
def colorize(params)
begin
require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
rescue LoadError
raise 'You must gem install win32console to use colorize on Windows'
end

self.scan(REGEXP_PATTERN).inject("") do |str, match|
match[0] ||= MODES[:default]
match[1] ||= COLORS[:default] + COLOR_OFFSET
match[2] ||= COLORS[:default] + BACKGROUND_OFFSET
match[3] ||= match[4]

if (params.instance_of?(Hash))
match[0] = MODES[params[:mode]] if params[:mode] && MODES[params[:mode]]
match[1] = COLORS[params[:color]] + COLOR_OFFSET if params[:color] && COLORS[params[:color]]
match[2] = COLORS[params[:background]] + BACKGROUND_OFFSET if params[:background] && COLORS[params[:background]]
elsif (params.instance_of?(Symbol))
match[1] = COLORS[params] + COLOR_OFFSET if params && COLORS[params]
end
windows_requires

scan(REGEXP_PATTERN).inject('') do |str, match|
set_defaults(match)
set_from_params(match, params)
str << "\033[#{match[0]};#{match[1]};#{match[2]}m#{match[3]}\033[0m"
end
end
Expand All @@ -90,7 +75,7 @@ def colorize(params)
# Return uncolorized string
#
def uncolorize
self.scan(REGEXP_PATTERN).inject("") do |str, match|
scan(REGEXP_PATTERN).inject('') do |str, match|
str << (match[3] || match[4])
end
end
Expand All @@ -99,9 +84,7 @@ def uncolorize
# Return true if string is colorized
#
def colorized?
self.scan(REGEXP_PATTERN).reject do |match|
match.last
end.any?
scan(REGEXP_PATTERN).reject(&:last).any?
end

#
Expand All @@ -111,11 +94,11 @@ def colorized?
next if key == :default

define_method key do
self.colorize(:color => key)
colorize(:color => key)
end

define_method "on_#{key}" do
self.colorize(:background => key)
colorize(:background => key)
end
end

Expand All @@ -126,10 +109,59 @@ def colorized?
next if key == :default

define_method key do
self.colorize(:mode => key)
colorize(:mode => key)
end
end

private

#
# Require windows libs
#
def windows_requires
begin
require 'Win32/Console/ANSI' if RUBY_VERSION < "2.0.0" && RUBY_PLATFORM =~ /win32/
rescue LoadError
raise 'You must gem install win32console to use colorize on Windows'
end
end

#
# Set default colors
#
def set_defaults(match)
match[0] ||= MODES[:default]
match[1] ||= COLORS[:default] + COLOR_OFFSET
match[2] ||= COLORS[:default] + BACKGROUND_OFFSET
match[3] ||= match[4]
end

#
# Set color from params
#
def set_from_params(match, params)
case params
when Hash then set_from_hash(match, params)
when Symbol then set_from_symbol(match, params)
end
end

#
# Set colors from params hash
#
def set_from_hash(match, hash)
match[0] = MODES[hash[:mode]] if hash[:mode] && MODES[hash[:mode]]
match[1] = COLORS[hash[:color]] + COLOR_OFFSET if hash[:color] && COLORS[hash[:color]]
match[2] = COLORS[hash[:background]] + BACKGROUND_OFFSET if hash[:background] && COLORS[hash[:background]]
end

#
# Set color from params symbol
#
def set_from_symbol(match, symbol)
match[1] = COLORS[symbol] + COLOR_OFFSET if symbol && COLORS[symbol]
end

class << self

#
Expand All @@ -147,21 +179,19 @@ def colors
end

#
# Display color matrix with color names
# Display color samples
#
def color_matrix(txt = '[X]')
size = String.colors.length
String.colors.each do |color|
String.colors.each do |back|
print txt.colorize(:color => color, :background => back)
end
puts " < #{color}"
def color_samples
String.colors.permutation(2).each do |background, color|
puts "#{color.inspect.rjust(15)} on #{background.inspect.ljust(15)}".colorize(:color => color, :background => background) + "#{color.inspect.rjust(15)} on #{background.inspect.ljust(15)}"
end
String.colors.reverse.each_with_index do |back, index|
puts "#{"|".rjust(txt.length)*(size-index)} < #{back}"
end
''
end

#
# Method removed, raise NoMethodError
#
def color_matrix(txt = '')
fail NoMethodError, '#color_matrix method was removed, try #color_samples instead'
end
end
end

0 comments on commit 93752d4

Please sign in to comment.