Skip to content

Commit 4e7ba9d

Browse files
author
José Valim
committed
Deprecate IO.ANSI.escape/2 and IO.ANSI.escape_fragment/2
1 parent 60b66ac commit 4e7ba9d

File tree

5 files changed

+11
-104
lines changed

5 files changed

+11
-104
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
* Deprecations
2424
* [Inspect] `Inspect.Algebra.pretty/2` is deprecated in favor of `Inspect.Algebra.format/2` that instead returns iodata. This function was used only by documentation examples and it is unlikely to affect actual code
25+
* [IO] `IO.ANSI.escape/2` and `IO.ANSI.escape_fragment/2` is deprecated in favor of `IO.ANSI.format/2` and `IO.ANSI.format_fragment/2`
2526
* [Kernel] Leading `0` for octals is deprecated in favor of `0o`
2627
* [Kernel] `0X` for hexadecimals is deprecated in favor of `0x`
2728
* [Kernel] `0B` for binaries is deprecated in favor of `0b`

lib/elixir/lib/io/ansi.ex

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ defmodule IO.ANSI do
152152
153153
iex> IO.ANSI.format(["Hello, ", :red, :bright, "world!"], true)
154154
[[[[[[], "Hello, "], "\e[31m"], "\e[1m"], "world!"] | "\e[0m"]
155+
155156
"""
156-
def format(chardata, emit \\ terminal?) do
157+
def format(chardata, emit \\ terminal?) when is_boolean(emit) do
157158
do_format(chardata, [], [], emit, :maybe)
158159
end
159160

@@ -172,8 +173,9 @@ defmodule IO.ANSI do
172173
173174
iex> IO.ANSI.format_fragment([:bright, 'Word'], true)
174175
[[[[[[], "\e[1m"], 87], 111], 114], 100]
176+
175177
"""
176-
def format_fragment(chardata, emit \\ terminal?) do
178+
def format_fragment(chardata, emit \\ terminal?) when is_boolean(emit) do
177179
do_format(chardata, [], [], emit, false)
178180
end
179181

@@ -210,27 +212,7 @@ defmodule IO.ANSI do
210212
acc
211213
end
212214

213-
@doc ~S"""
214-
Escapes a string by converting named ANSI sequences into actual ANSI codes.
215-
216-
The format for referring to sequences is `%{red}` and `%{red,bright}` (for
217-
multiple sequences).
218-
219-
It will also append a `%{reset}` to the string. If you don't want this
220-
behaviour, use `escape_fragment/2`.
221-
222-
An optional boolean parameter can be passed to enable or disable
223-
emitting actual ANSI codes. When `false`, no ANSI codes will emitted.
224-
By default, standard output will be checked if it is a terminal capable
225-
of handling these sequences (using `terminal?/1` function)
226-
227-
## Examples
228-
229-
iex> IO.ANSI.escape("Hello %{red,bright,green}yes", true)
230-
"Hello \e[31m\e[1m\e[32myes\e[0m"
231-
232-
"""
233-
@spec escape(String.t, emit :: boolean) :: String.t
215+
@doc false
234216
def escape(string, emit \\ terminal?) when is_binary(string) and is_boolean(emit) do
235217
{rendered, emitted} = do_escape(string, emit, false, nil, [])
236218
if emitted do
@@ -240,27 +222,7 @@ defmodule IO.ANSI do
240222
end
241223
end
242224

243-
@doc ~S"""
244-
Escapes a string by converting named ANSI sequences into actual ANSI codes.
245-
246-
The format for referring to sequences is `%{red}` and `%{red,bright}` (for
247-
multiple sequences).
248-
249-
An optional boolean parameter can be passed to enable or disable
250-
emitting actual ANSI codes. When `false`, no ANSI codes will emitted.
251-
By default, standard output will be checked if it is a terminal capable
252-
of handling these sequences (using `terminal?/1` function)
253-
254-
## Examples
255-
256-
iex> IO.ANSI.escape_fragment("Hello %{red,bright,green}yes", true)
257-
"Hello \e[31m\e[1m\e[32myes"
258-
259-
iex> IO.ANSI.escape_fragment("%{reset}bye", true)
260-
"\e[0mbye"
261-
262-
"""
263-
@spec escape_fragment(String.t, emit :: boolean) :: String.t
225+
@doc false
264226
def escape_fragment(string, emit \\ terminal?) when is_binary(string) and is_boolean(emit) do
265227
{escaped, _emitted} = do_escape(string, emit, false, nil, [])
266228
escaped

lib/elixir/src/elixir_dispatch.erl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,5 +523,9 @@ deprecation('Elixir.System', 'cmd', 1) ->
523523
"use System.cmd/3 instead";
524524
deprecation('Elixir.Inspect.Algebra', 'pretty', 2) ->
525525
"use Inspect.Algebra.format/2 instead";
526+
deprecation('Elixir.IO.ANSI', 'escape', _) ->
527+
"use the new API in IO.ANSI.format/2 instead";
528+
deprecation('Elixir.IO.ANSI', 'escape_fragment', _) ->
529+
"use the new API in IO.ANSI.format_fragment/2 instead";
526530
deprecation(_, _, _) ->
527531
false.

lib/elixir/test/elixir/io/ansi_test.exs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -91,53 +91,4 @@ defmodule IO.ANSITest do
9191
IO.ANSI.format([:brigh, "Hello!"], true)
9292
end
9393
end
94-
95-
test :escape_single do
96-
assert IO.ANSI.escape("Hello, %{red}world!", true) ==
97-
"Hello, #{IO.ANSI.red}world!#{IO.ANSI.reset}"
98-
assert IO.ANSI.escape("Hello, %{red}world!", true) ==
99-
"Hello, #{IO.ANSI.red}world!#{IO.ANSI.reset}"
100-
end
101-
102-
test :escape_non_attribute do
103-
assert IO.ANSI.escape("Hello %{clear}world!", true) ==
104-
"Hello #{IO.ANSI.clear}world!#{IO.ANSI.reset}"
105-
assert IO.ANSI.escape("Hello %{home}world!", true) ==
106-
"Hello #{IO.ANSI.home}world!#{IO.ANSI.reset}"
107-
end
108-
109-
test :escape_multiple do
110-
assert IO.ANSI.escape("Hello, %{red,bright}world!", true) ==
111-
"Hello, #{IO.ANSI.red}#{IO.ANSI.bright}world!#{IO.ANSI.reset}"
112-
assert IO.ANSI.escape("Hello, %{red, bright}world!", true) ==
113-
"Hello, #{IO.ANSI.red}#{IO.ANSI.bright}world!#{IO.ANSI.reset}"
114-
assert IO.ANSI.escape("Hello, %{red , bright}world!", true) ==
115-
"Hello, #{IO.ANSI.red}#{IO.ANSI.bright}world!#{IO.ANSI.reset}"
116-
end
117-
118-
test :escape_no_emit do
119-
assert IO.ANSI.escape("Hello, %{}world!", false) ==
120-
"Hello, world!"
121-
122-
assert IO.ANSI.escape("Hello, %{red,bright}world!", false) ==
123-
"Hello, world!"
124-
end
125-
126-
test :escape_fragment do
127-
assert IO.ANSI.escape("%{red}", true) == "#{IO.ANSI.red}#{IO.ANSI.reset}"
128-
assert IO.ANSI.escape_fragment("", true) == ""
129-
end
130-
131-
test :escape_noop do
132-
assert IO.ANSI.escape("") == ""
133-
end
134-
135-
test :escape_invalid do
136-
assert_raise ArgumentError, "invalid ANSI sequence specification: brigh", fn ->
137-
IO.ANSI.escape("%{brigh}, yes")
138-
end
139-
assert_raise ArgumentError, "invalid ANSI sequence specification: brigh", fn ->
140-
IO.ANSI.escape("%{brigh,red}, yes")
141-
end
142-
end
14394
end

lib/iex/test/iex/interaction_test.exs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,6 @@ defmodule IEx.InteractionTest do
104104
== "\e[34mhello\e[0m\n\e[31m:ok\e[0m"
105105
assert capture_iex("IO.puts IO.ANSI.escape(\"%{blue}hello\", true)", [colors: [enabled: false]])
106106
== "\e[34mhello\e[0m\n:ok"
107-
108-
# Test that ANSI escapes in the docs are left alone
109-
opts = [colors: [enabled: true]]
110-
assert capture_iex("h IO.ANSI.escape_fragment", opts)
111-
=~ ~r"%\{red\}"
112-
113-
# Test that ANSI escapes in iex output are left alone
114-
opts = [colors: [enabled: true, eval_result: "red", eval_info: "red"]]
115-
assert capture_iex("\"%{red} %{blue}\"", opts) == "\e[31m\"%{red} %{blue}\"\e[0m"
116-
assert capture_iex("IO.puts IEx.color(:eval_info, \"%{red} %{blue}\")", opts)
117-
== "\e[31m%{red} %{blue}\e[0m\n\e[31m:ok\e[0m"
118107
end
119108
end
120109

0 commit comments

Comments
 (0)