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

Improve Colorize #7690

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b49ed16
Improve Colorize
wooster0 Apr 18, 2019
2e6cdf4
Improve mode documentation
wooster0 Apr 18, 2019
69d64e8
Make Colorize::Object#surround public again
wooster0 Apr 18, 2019
702c6f2
Change Spec::COLORS to use the enum members
wooster0 Apr 18, 2019
64cf180
Rename some variables and add documentation to surround
wooster0 Apr 18, 2019
c22e57c
Fix specs
wooster0 Apr 18, 2019
9432a4c
Fix specs
wooster0 Apr 18, 2019
38ef634
Remove (*args)
wooster0 Apr 18, 2019
f0cac30
Make RGB colorize's arguments more explicit
wooster0 Apr 18, 2019
80180f0
Use color enum members in 2048.cr
wooster0 Apr 18, 2019
e3b84b6
Add back Colorize::Object(T)#on and remove some documentation
wooster0 Apr 18, 2019
ef19001
Remove unneeded parentheses and avoid capitalize in macro
wooster0 Apr 18, 2019
743a6a9
Improve documentation
wooster0 Apr 19, 2019
6425ad9
Fix Mode enum
wooster0 Apr 19, 2019
c0f4405
Remove COLORS and MODES and use ColorANSI and Mode
wooster0 Apr 24, 2019
8042ed4
More documentation
wooster0 May 13, 2019
0f84ac8
Fix specs
wooster0 May 13, 2019
ba56508
Remove the private attribute from two methods
wooster0 May 13, 2019
d8ce0ce
Minor documentation improvements
wooster0 May 13, 2019
d8d271b
Merge branch 'master' into patch-29
wooster0 Sep 28, 2019
a9fda36
Add a missing comma
wooster0 Sep 28, 2019
95569e2
Remove bright
wooster0 Sep 28, 2019
c81322c
Don't generate all and none methods
wooster0 Sep 28, 2019
94fc12e
Add bright back but deprecate it
wooster0 Sep 28, 2019
fedd9d3
Commit Sija's suggestion
wooster0 Sep 29, 2019
073e2d7
Remove useless `u8`s, fix the warning message and improve docs
wooster0 Sep 29, 2019
830d994
Improve an example
wooster0 Dec 22, 2019
0f2470b
Merge branch 'master' into patch-29
wooster0 Dec 22, 2019
3c453bf
Remove on_tty_only!
wooster0 Dec 22, 2019
16aa6fb
Merge branch 'master' into patch-29
wooster0 Apr 22, 2021
9f19d98
Re-add on_tty_only!
wooster0 Apr 23, 2021
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},
wooster0 marked this conversation as resolved.
Show resolved Hide resolved
}

def self.colorize_for(tile)
Expand Down
35 changes: 8 additions & 27 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 @@ -42,11 +42,11 @@ describe "colorize" do
end

it "colorizes foreground with 8-bit color" do
colorize("hello").fore(Colorize::Color256.new(123u8)).to_s.should eq("\e[38;5;123mhello\e[0m")
colorize("hello").fore(Colorize::Color256.new(123)).to_s.should eq("\e[38;5;123mhello\e[0m")
end

it "colorizes foreground with true color" do
colorize("hello").fore(Colorize::ColorRGB.new(12u8, 34u8, 56u8)).to_s.should eq("\e[38;2;12;34;56mhello\e[0m")
colorize("hello").fore(Colorize::ColorRGB.new(12, 34, 56)).to_s.should eq("\e[38;2;12;34;56mhello\e[0m")
end

it "colorizes background" do
Expand All @@ -69,16 +69,15 @@ describe "colorize" do
end

it "colorizes background with 8-bit color" do
colorize("hello").back(Colorize::Color256.new(123u8)).to_s.should eq("\e[48;5;123mhello\e[0m")
colorize("hello").back(Colorize::Color256.new(123)).to_s.should eq("\e[48;5;123mhello\e[0m")
end

it "colorizes background with true color" do
colorize("hello").back(Colorize::ColorRGB.new(12u8, 34u8, 56u8)).to_s.should eq("\e[48;2;12;34;56mhello\e[0m")
colorize("hello").back(Colorize::ColorRGB.new(12, 34, 56)).to_s.should eq("\e[48;2;12;34;56mhello\e[0m")
end

it "colorizes mode" do
colorize("hello").bold.to_s.should eq("\e[1mhello\e[0m")
colorize("hello").bright.to_s.should eq("\e[1mhello\e[0m")
colorize("hello").dim.to_s.should eq("\e[2mhello\e[0m")
colorize("hello").underline.to_s.should eq("\e[4mhello\e[0m")
colorize("hello").blink.to_s.should eq("\e[5mhello\e[0m")
Expand All @@ -99,34 +98,16 @@ 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")
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 "raises on unknown mode" do
expect_raises ArgumentError, "Unknown mode: bad" do
colorize("hello").mode(:bad)
end
end

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

it "colorizes with surround" do
Expand Down
Loading