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

Deprecate top-level with_color. Add Colorize.with #8892

Merged
merged 2 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 27 additions & 1 deletion spec/std/colorize_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ private def colorize(obj, *args)
end

private def with_color_wrap(*args)
with_color(*args).toggle(true)
Colorize.with(*args).toggle(true)
end

private class ColorizeToS
Expand Down Expand Up @@ -129,6 +129,32 @@ describe "colorize" do
colorize("hello", :red).inspect.should eq("\e[31m\"hello\"\e[0m")
end

describe "with_color deprecated top-level method" do
it "without args" do
io = IO::Memory.new
with_color.red.surround(io) do
io << "hello"
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 "with args" do
io = IO::Memory.new
with_color(:red).surround(io) do
io << "hello"
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
end

it "colorizes with surround" do
io = IO::Memory.new
with_color_wrap.red.surround(io) do
Expand Down
19 changes: 17 additions & 2 deletions src/colorize.cr
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,29 @@ module Colorize
def self.reset(io = STDOUT)
io << "\e[0m" if enabled?
end

# Helper method to use colorize with `IO`.
#
# ```
# io = IO::Memory.new
# io << "not-green"
# Colorize.with.green.bold.surround(io) do
# io << "green and bold if Colorize.enabled"
# end
# ```
def self.with
"".colorize
end
end

@[Deprecated("Use `Colorize.with`")]
def with_color
"".colorize
Colorize.with
end

@[Deprecated("Use `Colorize.with.fore(color)`")]
def with_color(color : Symbol)
"".colorize(color)
Colorize.with.fore(color)
end

module Colorize::ObjectExtensions
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/exception.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module Crystal
end

def with_color
::with_color.toggle(@color)
Colorize.with.toggle(@color)
end

def replace_leading_tabs_with_spaces(line)
Expand Down Expand Up @@ -126,7 +126,7 @@ module Crystal
size ||= 0
io << '\n'
io << (" " * (offset + column_number - 1))
with_color.green.bold.surround(io) do
Colorize.with.green.bold.surround(io) do
io << '^'
if size > 0
io << ("-" * (size - 1))
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/tools/print_hierarchy.cr
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ module Crystal
end

def with_color
::with_color.toggle(@program.color?)
Colorize.with.toggle(@program.color?)
end
end

Expand Down