Skip to content

Commit

Permalink
Merge pull request #29 from fazibear/code-climate
Browse files Browse the repository at this point in the history
Code refactor, disable_colorization feature
  • Loading branch information
fazibear committed Dec 16, 2014
2 parents 93752d4 + 394f4d1 commit 7cba4a1
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 197 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
language: ruby
rvm:
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
== 0.7.5 / 2014-12-11
* big code refactoring
* disable_colorization feature added

== 0.7.4 / 2014-12-10
* code cleanups

Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ Some usage samples:

Class methods:

String.colors - return array of all possible colors names
String.modes - return array of all possible modes
String.color_samples - displays color samples in all combinations
String.colors - return array of all possible colors names
String.modes - return array of all possible modes
String.color_samples - displays color samples in all combinations
String.disable_colorization - check if colorization is disabled
String.disable_colorization = false - disable colorization
String.disable_colorization false - disable colorization
String.disable_colorization = true - enable colorization
String.disable_colorization true - enable colorization

requirements
------------
Expand All @@ -47,7 +52,7 @@ install
license
-------

Copyright (C) 2007 Michal Kalbarczyk
Copyright (C) 2007-2015 Michal Kalbarczyk

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
4 changes: 3 additions & 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.4'
s.version = '0.7.5'

s.authors = ['fazibear']
s.email = 'fazibear@gmail.com'
Expand All @@ -21,6 +21,8 @@ Gem::Specification.new do |s|
'Rakefile',
'colorize.gemspec',
'lib/colorize.rb',
'lib/colorize/class_methods.rb',
'lib/colorize/instance_methods.rb'.
'test/test_colorize.rb',
]
s.test_files = [
Expand Down
197 changes: 6 additions & 191 deletions lib/colorize.rb
Original file line number Diff line number Diff line change
@@ -1,197 +1,12 @@
require File.expand_path('colorize/class_methods', File.dirname(__FILE__))
require File.expand_path('colorize/instance_methods', File.dirname(__FILE__))
#
# Colorize String class extension.
#
class String
extend Colorize::ClassMethods
include Colorize::InstanceMethods

#
# Colors Hash
#
COLORS = {
:black => 0,
:red => 1,
:green => 2,
:yellow => 3,
:blue => 4,
:magenta => 5,
:cyan => 6,
:white => 7,
:default => 9,

:light_black => 60,
:light_red => 61,
:light_green => 62,
:light_yellow => 63,
:light_blue => 64,
:light_magenta => 65,
:light_cyan => 66,
:light_white => 67
}

#
# Modes Hash
#
MODES = {
:default => 0, # Turn off all attributes
:bold => 1, # Set bold mode
:underline => 4, # Set underline mode
:blink => 5, # Set blink mode
:swap => 7, # Exchange foreground and background colors
:hide => 8 # Hide text (foreground color would be the same as background)
}

REGEXP_PATTERN = /\033\[([0-9]+);([0-9]+);([0-9]+)m(.+?)\033\[0m|([^\033]+)/m
COLOR_OFFSET = 30
BACKGROUND_OFFSET = 40

public

#
# Change color of string
#
# Examples:
#
# puts "This is blue".colorize(:blue)
# puts "This is light blue".colorize(:light_blue)
# puts "This is also blue".colorize(:color => :blue)
# puts "This is light blue with red background".colorize(:color => :light_blue, :background => :red)
# puts "This is light blue with red background".colorize(:light_blue ).colorize( :background => :red)
# puts "This is blue text on red".blue.on_red
# puts "This is red on blue".colorize(:red).on_blue
# puts "This is red on blue and underline".colorize(:red).on_blue.underline
# puts "This is blue text on red".blue.on_red.blink
# puts "This is uncolorized".blue.on_red.uncolorize
#
def colorize(params)
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

#
# Return uncolorized string
#
def uncolorize
scan(REGEXP_PATTERN).inject('') do |str, match|
str << (match[3] || match[4])
end
end

#
# Return true if string is colorized
#
def colorized?
scan(REGEXP_PATTERN).reject(&:last).any?
end

#
# Make some color and on_color methods
#
COLORS.each_key do |key|
next if key == :default

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

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

#
# Methods for modes
#
MODES.each_key do |key|
next if key == :default

define_method key do
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

#
# Return array of available modes used by colorize method
#
def modes
MODES.keys
end

#
# Return array of available colors used by colorize method
#
def colors
COLORS.keys
end

#
# Display color samples
#
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
end

#
# Method removed, raise NoMethodError
#
def color_matrix(txt = '')
fail NoMethodError, '#color_matrix method was removed, try #color_samples instead'
end
end
color_methods
modes_methods
end
114 changes: 114 additions & 0 deletions lib/colorize/class_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
module Colorize
module ClassMethods

#
# Property to disable colorization
#
def disable_colorization(value = nil)
if value.nil?
@disable_colorization || false
else
@disable_colorization = (value || false)
end
end

#
# Setter for disable colorization
#
def disable_colorization=(value)
@disable_colorization = (value || false)
end

#
# Color codes hash
#
def color_codes
{
:black => 0, :light_black => 60,
:red => 1, :light_red => 61,
:green => 2, :light_green => 62,
:yellow => 3, :light_yellow => 63,
:blue => 4, :light_blue => 64,
:magenta => 5, :light_magenta => 65,
:cyan => 6, :light_cyan => 66,
:white => 7, :light_white => 67,
:default => 9
}
end

#
# Return array of available colors used by colorize
#
def colors
color_codes.keys
end

#
# Mode codes hash
#
def mode_codes
{
:default => 0, # Turn off all attributes
:bold => 1, # Set bold mode
:underline => 4, # Set underline mode
:blink => 5, # Set blink mode
:swap => 7, # Exchange foreground and background colors
:hide => 8 # Hide text (foreground color would be the same as background)
}
end

#
# Return array of available modes used by colorize
#
def modes
mode_codes.keys
end

#
# Color and on_color methods
#
def color_methods
colors.each do |key|
next if key == :default

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

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

#
# Modes methods
#
def modes_methods
modes.each do |key|
next if key == :default

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

#
# Display color samples
#
def color_samples
colors.permutation(2).each do |background, color|
sample_text = "#{color.inspect.rjust(15)} on #{background.inspect.ljust(15)}"
puts "#{sample_text.colorize(:color => color, :background => background)} #{sample_text}"
end
end

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

0 comments on commit 7cba4a1

Please sign in to comment.