-
-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add color aliases #75
Changes from 5 commits
d80e4ec
224a20f
925d29c
93aa2d4
934a510
99f8089
e24d50e
51c3f67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
Gem::Specification.new do |s| | ||
s.name = 'colorize' | ||
s.version = '0.8.1' | ||
s.required_ruby_version = '>= 2.5' | ||
s.required_ruby_version = '>= 2.6' | ||
|
||
s.authors = ['Michał Kalbarczyk'] | ||
s.email = '[email protected]' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,26 +2,63 @@ | |
|
||
module Colorize | ||
module ClassMethods | ||
@@color_codes ||= { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/ClassVars: Replace class var @@color_codes with a class instance var. |
||
black: 30, | ||
red: 31, | ||
green: 32, | ||
yellow: 33, | ||
blue: 34, | ||
purple: 35, | ||
cyan: 36, | ||
white: 37, | ||
default: 39, | ||
light_black: 90, | ||
light_red: 91, | ||
light_green: 92, | ||
light_yellow: 93, | ||
light_blue: 94, | ||
light_purple: 95, | ||
light_cyan: 96, | ||
light_white: 97 | ||
} | ||
|
||
@@mode_codes ||= { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/ClassVars: Replace class var @@mode_codes with a class instance var. |
||
default: 0, # Turn off all attributes | ||
bold: 1, | ||
dim: 2, | ||
italic: 3, | ||
underline: 4, | ||
blink: 5, | ||
blink_slow: 5, | ||
blink_fast: 6, | ||
invert: 7, | ||
hide: 8, | ||
strike: 9, | ||
double_underline: 20, | ||
reveal: 28, | ||
overlined: 53 | ||
} | ||
|
||
# | ||
# Property to disable colorization | ||
# | ||
def disable_colorization(value = nil) | ||
if value.nil? | ||
if defined?(@disable_colorization) | ||
@disable_colorization || false | ||
if defined?(@@disable_colorization) | ||
@@disable_colorization || false | ||
else | ||
false | ||
end | ||
else | ||
@disable_colorization = (value || false) | ||
@@disable_colorization = (value || false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/ClassVars: Replace class var @@disable_colorization with a class instance var. |
||
end | ||
end | ||
|
||
# | ||
# Setter for disable colorization | ||
# | ||
def disable_colorization=(value) | ||
@disable_colorization = (value || false) | ||
@@disable_colorization = (value || false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/ClassVars: Replace class var @@disable_colorization with a class instance var. |
||
end | ||
|
||
# | ||
|
@@ -49,10 +86,16 @@ def color_samples | |
end | ||
|
||
# | ||
# Method removed, raise NoMethodError | ||
# Add color alias | ||
# | ||
def color_matrix(_ = '') | ||
fail NoMethodError, '#color_matrix method was removed, try #color_samples instead' | ||
def add_color_alias(*params) | ||
parse_color_alias_params(params).each do |_alias_, _color_| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lint/UnderscorePrefixedVariableName: Do not use prefix _ for a variable that is used. |
||
check_if_color_available!(_alias_) | ||
check_if_color_exist!(_color_) | ||
|
||
add_color_code(_alias_, color_codes[_color_]) | ||
add_color_method(_alias_) | ||
end | ||
end | ||
|
||
# private | ||
|
@@ -61,32 +104,18 @@ def color_matrix(_ = '') | |
# 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 | ||
} | ||
@@color_codes | ||
end | ||
|
||
def add_color_code(code, color) | ||
@@color_codes[code] = color | ||
end | ||
|
||
# | ||
# Mode codes hash | ||
# | ||
def mode_codes | ||
{ | ||
:default => 0, # Turn off all attributes | ||
:bold => 1, # Set bold mode | ||
:italic => 3, # Set italic 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) | ||
} | ||
@@mode_codes | ||
end | ||
|
||
# | ||
|
@@ -96,13 +125,20 @@ def color_methods | |
colors.each do |key| | ||
next if key == :default | ||
|
||
define_method key do | ||
colorize(:color => key) | ||
end | ||
add_color_method(key) | ||
end | ||
end | ||
|
||
define_method "on_#{key}" do | ||
colorize(:background => key) | ||
end | ||
# | ||
# Generate color and on_color method | ||
# | ||
def add_color_method(key) | ||
define_method key do | ||
colorize(:color => key) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/HashSyntax: Use the new Ruby 1.9 hash syntax. |
||
end | ||
|
||
define_method "on_#{key}" do | ||
colorize(:background => key) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/HashSyntax: Use the new Ruby 1.9 hash syntax. |
||
end | ||
end | ||
|
||
|
@@ -118,5 +154,28 @@ def modes_methods | |
end | ||
end | ||
end | ||
|
||
def parse_color_alias_params(params) | ||
return [params] if params.is_a?(Array) && params.length == 2 | ||
|
||
params.flat_map do |param| | ||
next param if param.is_a?(Array) && param.length == 2 | ||
next param.to_a if param.is_a?(Hash) | ||
end | ||
end | ||
|
||
# | ||
# Check if color exists | ||
# | ||
def check_if_color_available!(color) | ||
color_codes[color] && fail(::Colorize::ColorAlreadyExist, "Colorize: color named :#{color} already exist!") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [113/80] |
||
end | ||
|
||
# | ||
# Check if color is missing | ||
# | ||
def check_if_color_exist!(color) | ||
color_codes[color] || fail(::Colorize::ColorDontExist, "Colorize: color :#{color} don't exist!") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [102/80] |
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Colorize | ||
class ColorAlreadyExist < StandardError | ||
end | ||
|
||
class ColorDontExist < StandardError | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
require File.expand_path('colorize/errors', File.dirname(__FILE__)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
require File.expand_path('colorize/class_methods', File.dirname(__FILE__)) | ||
require File.expand_path('colorize/instance_methods', File.dirname(__FILE__)) | ||
# | ||
|
@@ -12,6 +13,9 @@ class ColorizedString < String | |
color_methods | ||
modes_methods | ||
|
||
add_color_alias(:grey, :light_black) | ||
add_color_alias(:gray, :light_black) | ||
|
||
# | ||
# Shortcut to create ColorizedString with ColorizedString['test']. | ||
# | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,4 +136,66 @@ def test_color_samples_method | |
String.color_samples | ||
end | ||
end | ||
|
||
def test_grey_alias | ||
assert_equal 'This is grey'.grey, 'This is grey'.light_black | ||
end | ||
|
||
def test_gray_alias | ||
assert_equal 'This is gray'.gray, 'This is gray'.light_black | ||
end | ||
|
||
def test_add_color_alias | ||
String.add_color_alias(:extra_blue, :light_blue) | ||
|
||
assert_equal 'blue'.light_blue, 'blue'.extra_blue | ||
assert_equal 'blue'.on_light_blue, 'blue'.on_extra_blue | ||
end | ||
|
||
def test_add_color_alias_errors | ||
String.add_color_alias(:extra_red, :light_red) | ||
|
||
assert_raises ::Colorize::ColorAlreadyExist, 'Colorize: color :extra_red already exist!' do | ||
String.add_color_alias(:extra_red, :light_blue) | ||
end | ||
|
||
assert_raises ::Colorize::ColorDontExist, 'Colorize: color :light_color don\'t exist!' do | ||
String.add_color_alias(:extra_white, :light_color) | ||
end | ||
end | ||
|
||
def test_add_color_alias_with_single_hash | ||
String.add_color_alias(extra_green: :light_green) | ||
|
||
assert_equal 'example string'.light_green, 'example string'.extra_green | ||
assert_equal 'example string'.on_light_green, 'example string'.on_extra_green | ||
end | ||
|
||
def test_add_color_alias_with_single_hash_with_arrow | ||
String.add_color_alias(:extra_color => :gray) | ||
|
||
assert_equal 'example string'.gray, 'example string'.extra_color | ||
assert_equal 'example string'.on_gray, 'example string'.on_extra_color | ||
end | ||
|
||
def test_add_color_alias_with_multi_hash | ||
String.add_color_alias(extra_color_a: :gray, extra_color_b: :blue) | ||
|
||
assert_equal 'example string'.gray, 'example string'.extra_color_a | ||
assert_equal 'example string'.blue, 'example string'.extra_color_b | ||
end | ||
|
||
def test_add_color_alias_with_multi_hash_with_arrow | ||
String.add_color_alias(:extra_color_c => :gray, :extra_color_d => :blue) | ||
|
||
assert_equal 'example string'.gray, 'example string'.extra_color_c | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
assert_equal 'example string'.on_blue, 'example string'.on_extra_color_d | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
end | ||
|
||
def test_add_color_alias_with_multi_hash_mixed | ||
String.add_color_alias(extra_color_e: :gray, :extra_color_f => :blue) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/HashSyntax: Use the new Ruby 1.9 hash syntax. |
||
|
||
assert_equal 'example string'.gray, 'example string'.extra_color_e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
assert_equal 'example string'.on_blue, 'example string'.on_extra_color_f | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.