Skip to content

Remove symbol overloads of Colorize color methods for symbol auto-casting #11775

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
36 changes: 19 additions & 17 deletions samples/2048.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
require "colorize"

module Screen
alias Color = Colorize::ColorANSI

TILES = {
0 => {:white, nil},
2 => {:black, :white},
4 => {:blue, :white},
8 => {:black, :yellow},
16 => {:white, :red},
32 => {:black, :red},
64 => {:white, :magenta},
128 => {:red, :yellow},
256 => {:magenta, :yellow},
512 => {:white, :yellow},
1024 => {:white, :yellow},
2048 => {:white, :yellow},
4096 => {:white, :black},
8192 => {:white, :black},
16384 => {:white, :black},
32768 => {:white, :black},
65536 => {:white, :black},
0 => {Color::White, nil},
2 => {Color::Black, Color::White},
4 => {Color::Blue, Color::White},
8 => {Color::Black, Color::Yellow},
16 => {Color::White, Color::Red},
32 => {Color::Black, Color::Red},
64 => {Color::White, Color::Magenta},
128 => {Color::Red, Color::Yellow},
256 => {Color::Magenta, Color::Yellow},
512 => {Color::White, Color::Yellow},
1024 => {Color::White, Color::Yellow},
2048 => {Color::White, Color::Yellow},
4096 => {Color::White, Color::Black},
8192 => {Color::White, Color::Black},
16384 => {Color::White, Color::Black},
32768 => {Color::White, Color::Black},
65536 => {Color::White, Color::Black},
}

def self.colorize_for(tile)
Expand Down
20 changes: 4 additions & 16 deletions spec/std/colorize_spec.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "spec"
require "colorize"

private def colorize(obj, *args)
obj.colorize(*args).toggle(true)
private def colorize(obj)
obj.colorize.toggle(true)
end

private def with_color_wrap(*args)
Expand Down Expand Up @@ -50,6 +50,7 @@ describe "colorize" do
end

it "colorizes background" do
colorize("hello").on(:black).to_s.should eq("\e[40mhello\e[0m")
colorize("hello").on_black.to_s.should eq("\e[40mhello\e[0m")
colorize("hello").on_red.to_s.should eq("\e[41mhello\e[0m")
colorize("hello").on_green.to_s.should eq("\e[42mhello\e[0m")
Expand Down Expand Up @@ -99,28 +100,15 @@ describe "colorize" do
end

it "colorizes foreground with symbol" do
colorize("hello", :red).to_s.should eq("\e[31mhello\e[0m")
colorize("hello").fore(:red).to_s.should eq("\e[31mhello\e[0m")
end

it "colorizes mode with symbol" do
colorize("hello").mode(:bold).to_s.should eq("\e[1mhello\e[0m")
end

it "raises on unknown foreground color" do
expect_raises ArgumentError, "Unknown color: brown" do
colorize("hello", :brown)
end
end

it "raises on unknown background color" do
expect_raises ArgumentError, "Unknown color: brown" do
colorize("hello").back(:brown)
end
end

it "inspects" do
colorize("hello", :red).inspect.should eq("\e[31m\"hello\"\e[0m")
colorize("hello").fore(:red).inspect.should eq("\e[31m\"hello\"\e[0m")
end

it "colorizes with surround" do
Expand Down
59 changes: 8 additions & 51 deletions src/colorize.cr
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,7 @@
# "foo".colorize(Random::DEFAULT.next_bool ? :green : :default)
# ```
#
# Available colors are:
# ```
# :default
# :black
# :red
# :green
# :yellow
# :blue
# :magenta
# :cyan
# :light_gray
# :dark_gray
# :light_red
# :light_green
# :light_yellow
# :light_blue
# :light_magenta
# :light_cyan
# :white
# ```
# See `Colorize::ColorANSI` for all available colors.
#
# See `Colorize::Mode` for available text decorations.
module Colorize
Expand Down Expand Up @@ -170,7 +151,7 @@ module Colorize::ObjectExtensions
end

# Turns `self` into a `Colorize::Object` and colors it with a color.
def colorize(fore)
def colorize(fore : Color)
Colorize::Object.new(self).fore(fore)
end
end
Expand Down Expand Up @@ -283,8 +264,6 @@ end

# A colorized object. Colors and text decorations can be modified.
struct Colorize::Object(T)
private COLORS = %w(default black red green yellow blue magenta cyan light_gray dark_gray light_red light_green light_yellow light_blue light_magenta light_cyan white)

@fore : Color
@back : Color

Expand All @@ -295,14 +274,14 @@ struct Colorize::Object(T)
@enabled = Colorize.enabled?
end

{% for name in COLORS %}
def {{name.id}}
@fore = ColorANSI::{{name.camelcase.id}}
{% for color in ColorANSI.constants.reject { |constant| constant == "All" || constant == "None" } %}
def {{color.underscore.id}}
@fore = ColorANSI::{{color.id}}
self
end

def on_{{name.id}}
@back = ColorANSI::{{name.camelcase.id}}
def on_{{color.underscore.id}}
@back = ColorANSI::{{color.id}}
self
end
{% end %}
Expand All @@ -314,32 +293,10 @@ struct Colorize::Object(T)
end
{% end %}

def fore(color : Symbol) : self
{% for name in COLORS %}
if color == :{{name.id}}
@fore = ColorANSI::{{name.camelcase.id}}
return self
end
{% end %}

raise ArgumentError.new "Unknown color: #{color}"
end

def fore(@fore : Color) : self
self
end

def back(color : Symbol) : self
{% for name in COLORS %}
if color == :{{name.id}}
@back = ColorANSI::{{name.camelcase.id}}
return self
end
{% end %}

raise ArgumentError.new "Unknown color: #{color}"
end

def back(@back : Color) : self
self
end
Expand All @@ -350,7 +307,7 @@ struct Colorize::Object(T)
self
end

def on(color : Symbol)
Copy link
Contributor

Choose a reason for hiding this comment

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

should there be a deprecation warning for this?

Copy link
Contributor

Choose a reason for hiding this comment

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

As mentioned in the OP, the mere existence of a Symbol-accepting overload will trigger deprecations even for arguments that autocast to Color due to overload ordering.

def on(color : Color)
back color
end

Expand Down
14 changes: 7 additions & 7 deletions src/spec/dsl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ module Spec
end

private STATUS_COLORS = {
Status::Success => :green,
Status::Fail => :red,
Status::Error => :red,
Status::Pending => :yellow,
Status::Success => Colorize::ColorANSI::Green,
Status::Fail => Colorize::ColorANSI::Red,
Status::Error => Colorize::ColorANSI::Red,
Status::Pending => Colorize::ColorANSI::Yellow,
}

private INFO_COLORS = {
InfoKind::Comment => :cyan,
InfoKind::Focus => :cyan,
InfoKind::Order => :cyan,
InfoKind::Comment => Colorize::ColorANSI::Cyan,
InfoKind::Focus => Colorize::ColorANSI::Cyan,
InfoKind::Order => Colorize::ColorANSI::Cyan,
}

private LETTERS = {
Expand Down