Skip to content

Commit

Permalink
Refactor and enhance Colorize
Browse files Browse the repository at this point in the history
- Refactor all codes.
  * Prefer to use enums instead of constants.
  * Separate `Object` and `Style`. `Style` represents a style on
  terminal purely, however `Object` has object content.
- Support 256 color and 32bit true color.
- Add TTY detection. If not, doesn't output escape sequence.
  • Loading branch information
makenowjust committed Jan 21, 2017
1 parent ccf46c0 commit c38413e
Show file tree
Hide file tree
Showing 9 changed files with 476 additions and 242 deletions.
78 changes: 72 additions & 6 deletions spec/std/colorize_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ require "spec"
require "colorize"

describe "colorize" do
Colorize.force = true

it "colorizes without change" do
"hello".colorize.to_s.should eq("hello")
end
Expand Down Expand Up @@ -60,6 +62,7 @@ describe "colorize" do

it "colorizes foreground with background" do
"hello".colorize.blue.on_green.to_s.should eq("\e[34;42mhello\e[0m")
"hello".colorize(fore: :blue, back: :green).to_s.should eq("\e[34;42mhello\e[0m")
end

it "colorizes foreground with background with mode" do
Expand All @@ -71,6 +74,41 @@ describe "colorize" do
"hello".colorize.fore(:red).to_s.should eq("\e[31mhello\e[0m")
end

it "colorizes foreground with nil" do
"hello".colorize(nil).to_s.should eq("hello")
"hello".colorize.fore(nil).to_s.should eq("hello")
end

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

it "colorizes foreground with RGB" do
"hello".colorize(Colorize::ColorRGB.new 11, 22, 33).to_s.should eq("\e[38;2;11;22;33mhello\e[0m")
"hello".colorize.fore(Colorize::ColorRGB.new 11, 22, 33).to_s.should eq("\e[38;2;11;22;33mhello\e[0m")
end

it "colorizes background with symbol" do
"hello".colorize(back: :red).to_s.should eq("\e[41mhello\e[0m")
"hello".colorize.back(:red).to_s.should eq("\e[41mhello\e[0m")
end

it "colorizes background with nil" do
"hello".colorize(back: nil).to_s.should eq("hello")
"hello".colorize.back(nil).to_s.should eq("hello")
end

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

it "colorizes background with RGB" do
"hello".colorize(back: Colorize::ColorRGB.new 11, 22, 33).to_s.should eq("\e[48;2;11;22;33mhello\e[0m")
"hello".colorize.back(Colorize::ColorRGB.new 11, 22, 33).to_s.should eq("\e[48;2;11;22;33mhello\e[0m")
end

it "colorizes mode with symbol" do
"hello".colorize.mode(:bold).to_s.should eq("\e[1mhello\e[0m")
end
Expand Down Expand Up @@ -113,23 +151,23 @@ describe "colorize" do
io.to_s.should eq("\e[31mhello\e[0m")
end

it "colorizes with push and pop" do
it "colorizes with surround stack" do
io = IO::Memory.new
with_color.red.push(io) do
with_color.red.surround(io) do
io << "hello"
with_color.green.push(io) do
with_color.green.surround(io) do
io << "world"
end
io << "bye"
end
io.to_s.should eq("\e[31mhello\e[0;32mworld\e[0;31mbye\e[0m")
end

it "colorizes with push and pop resets" do
it "colorizes with surround stack resets" do
io = IO::Memory.new
with_color.red.push(io) do
with_color.red.surround(io) do
io << "hello"
with_color.green.bold.push(io) do
with_color.green.bold.surround(io) do
io << "world"
end
io << "bye"
Expand All @@ -145,4 +183,32 @@ describe "colorize" do
it "toggles off and on" do
"hello".colorize.toggle(false).black.toggle(true).to_s.should eq("\e[30mhello\e[0m")
end

Colorize.force = false

it "doesn't colorize no tty IO" do
"hello".colorize(fore: :red, back: :green, mode: :bold).to_s.should eq("hello")
"hello".colorize(fore: :red, back: :green, mode: :bold).inspect.should eq("\"hello\"")
io = IO::Memory.new
with_color(fore: :red, back: :green, mode: :bold).surround(io) do
io << "hello"
end
io.to_s.should eq("hello")
end

it "colorize no tty IO with force" do
io = IO::Memory.new
"hello".colorize(fore: :red, back: :green, mode: :bold).to_s(io, force: true)
io.to_s.should eq("\e[31;42;1mhello\e[0m")
io.clear

"hello".colorize(fore: :red, back: :green, mode: :bold).inspect(io, force: true)
io.to_s.should eq("\e[31;42;1m\"hello\"\e[0m")
io.clear

with_color(fore: :red, back: :green, mode: :bold).surround(io, force: true) do
io << "hello"
end
io.to_s.should eq("\e[31;42;1mhello\e[0m")
end
end
Loading

0 comments on commit c38413e

Please sign in to comment.