Skip to content
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

Merged
merged 8 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require:
- rubocop-performance
- rubocop-github
AllCops:
TargetRubyVersion: 2.5
TargetRubyVersion: 2.6
DisplayCopNames: false
StyleGuideCopsOnly: false
Layout/AccessModifierIndentation:
Expand Down Expand Up @@ -428,10 +428,15 @@ Metrics/BlockNesting:
Enabled: false
Max: 3
Metrics/ClassLength:
Description: Avoid classes longer than 100 lines of code.
Enabled: false
Description: Avoid classes longer than 150 lines of code.
Enabled: true
CountComments: false
Max: 150
Metrics/ModuleLength:
Description: Avoid modules longer than 150 lines of code.
Enabled: true
CountComments: false
Max: 100
Max: 150
Metrics/CyclomaticComplexity:
Description: A complexity metric that is strongly correlated to the number of test
cases needed to validate a method.
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ String.disable_colorization false # enable colorization
String.disable_colorization = true # disable colorization
String.disable_colorization true # disable colorization

# adding aliases

String.add_color_alias(:niebieski, :blue)
String.add_color_alias(:zielony => :green)
String.add_color_alias(czarny: :black)
String.add_color_alias(czerwony: :red, granatowy: :blue)

puts "This is blue".colorize(:blue)
puts "This is light blue".colorize(:light_blue)
puts "This is also blue".colorize(:color => :blue)
Expand All @@ -59,6 +66,13 @@ ColorizedString.disable_colorization false # enable colorization
ColorizedString.disable_colorization = true # disable colorization
ColorizedString.disable_colorization true # disable colorization

# adding aliases

ColorizedString.add_color_alias(:niebieski, :blue)
ColorizedString.add_color_alias(:zielony => :green)
ColorizedString.add_color_alias(czarny: :black)
ColorizedString.add_color_alias(czerwony: :red, granatowy: :blue)

puts ColorizedString["This is blue"].colorize(:blue)
puts ColorizedString["This is light blue"].colorize(:light_blue)
puts ColorizedString["This is also blue"].colorize(:color => :blue)
Expand Down
2 changes: 1 addition & 1 deletion colorize.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
Expand Down
4 changes: 4 additions & 0 deletions lib/colorize.rb
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__))

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.

require File.expand_path('colorize/class_methods', File.dirname(__FILE__))
require File.expand_path('colorize/instance_methods', File.dirname(__FILE__))
#
Expand All @@ -11,4 +12,7 @@ class String

color_methods
modes_methods

add_color_alias(:grey, :light_black)
add_color_alias(:gray, :light_black)
end
125 changes: 92 additions & 33 deletions lib/colorize/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,63 @@

module Colorize
module ClassMethods
@@color_codes ||= {

Choose a reason for hiding this comment

The 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 ||= {

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The 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

#
Expand Down Expand Up @@ -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_|

Choose a reason for hiding this comment

The 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
Expand All @@ -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

#
Expand All @@ -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)

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

end
end

Expand All @@ -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!")

Choose a reason for hiding this comment

The 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!")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [102/80]

end
end
end
9 changes: 9 additions & 0 deletions lib/colorize/errors.rb
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
5 changes: 2 additions & 3 deletions lib/colorize/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ module InstanceMethods
def colorize(params)
return self if self.class.disable_colorization

require_windows_libs
scan_for_colors.inject(self.class.new) do |str, match|
colors_from_params(match, params)
defaults_colors(match)
Expand Down Expand Up @@ -88,14 +87,14 @@ def color_from_symbol(match, symbol)
# Color for foreground (offset 30)
#
def color(color)
self.class.color_codes[color] + 30 if self.class.color_codes[color]
self.class.color_codes[color]
end

#
# Color for background (offset 40)
#
def background_color(color)
self.class.color_codes[color] + 40 if self.class.color_codes[color]
self.class.color_codes[color] + 10 if self.class.color_codes[color]
end

#
Expand Down
4 changes: 4 additions & 0 deletions lib/colorized_string.rb
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__))

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.

require File.expand_path('colorize/class_methods', File.dirname(__FILE__))
require File.expand_path('colorize/instance_methods', File.dirname(__FILE__))
#
Expand All @@ -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'].
#
Expand Down
62 changes: 62 additions & 0 deletions test/test_colorize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

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.

assert_equal 'example string'.on_blue, 'example string'.on_extra_color_d

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.

end

def test_add_color_alias_with_multi_hash_mixed
String.add_color_alias(extra_color_e: :gray, :extra_color_f => :blue)

Choose a reason for hiding this comment

The 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

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.

assert_equal 'example string'.on_blue, 'example string'.on_extra_color_f

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.

end
end
Loading