Skip to content

Commit 103a829

Browse files
refactor: split formatting in banner and rest
1 parent 4a0cf6c commit 103a829

2 files changed

Lines changed: 12 additions & 32 deletions

File tree

lib/elixir/lib/kernel/cli.ex

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,26 @@ defmodule Kernel.CLI do
9898
@doc """
9999
Shared helper for error formatting on CLI tools.
100100
"""
101-
def format_error(kind, reason, stacktrace, ansi? \\ false) do
101+
def format_error(kind, reason, stacktrace) do
102+
{banner, rest} = format_error_parts(kind, reason, stacktrace)
103+
[banner, rest]
104+
end
105+
106+
defp format_error_parts(kind, reason, stacktrace) do
102107
{blamed, stacktrace} = Exception.blame(kind, reason, stacktrace)
103108

104-
iodata =
109+
banner =
105110
case blamed do
106111
%FunctionClauseError{} ->
107112
formatted = Exception.format_banner(kind, reason, stacktrace)
108113
padded_blame = pad(FunctionClauseError.blame(blamed, &inspect/1, &blame_match/1))
109-
[banner_ansi(formatted, ansi?), padded_blame]
114+
[formatted, padded_blame]
110115

111116
_ ->
112-
Exception.format_banner(kind, blamed, stacktrace) |> banner_ansi(ansi?)
117+
Exception.format_banner(kind, blamed, stacktrace)
113118
end
114119

115-
[iodata, ?\n, Exception.format_stacktrace(prune_stacktrace(stacktrace))]
120+
{banner, [?\n, Exception.format_stacktrace(prune_stacktrace(stacktrace))]}
116121
end
117122

118123
@doc """
@@ -179,14 +184,10 @@ defmodule Kernel.CLI do
179184
## Error handling
180185

181186
defp print_error(kind, reason, stacktrace) do
182-
IO.write(:stderr, format_error(kind, reason, stacktrace, IO.ANSI.enabled?()))
187+
{banner, rest} = format_error_parts(kind, reason, stacktrace)
188+
IO.write(:stderr, [IO.ANSI.format([:red, banner]), rest])
183189
end
184190

185-
defp banner_ansi(banner, true),
186-
do: IO.iodata_to_binary(IO.ANSI.format([:red, banner], true))
187-
188-
defp banner_ansi(banner, false), do: banner
189-
190191
defp blame_match(%{match?: true, node: node}), do: blame_ansi(:normal, "+", node)
191192
defp blame_match(%{match?: false, node: node}), do: blame_ansi(:red, "-", node)
192193

lib/elixir/test/elixir/kernel/cli_test.exs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -197,27 +197,6 @@ defmodule Kernel.CLI.ExecutableTest do
197197
assert elixir(fixture_path("at_exit.exs") |> to_charlist()) ==
198198
"goodbye cruel world with status 1\n"
199199
end
200-
201-
describe "format_error/4" do
202-
test "colors the banner in red when ansi? is true" do
203-
formatted =
204-
Kernel.CLI.format_error(:error, %ArgumentError{message: "boom"}, [], true)
205-
|> IO.iodata_to_binary()
206-
207-
assert formatted =~ IO.ANSI.red()
208-
assert formatted =~ IO.ANSI.reset()
209-
assert formatted =~ "** (ArgumentError) boom"
210-
end
211-
212-
test "leaves output plain by default" do
213-
formatted =
214-
Kernel.CLI.format_error(:error, %ArgumentError{message: "boom"}, [])
215-
|> IO.iodata_to_binary()
216-
217-
refute formatted =~ "\e["
218-
assert formatted =~ "** (ArgumentError) boom"
219-
end
220-
end
221200
end
222201

223202
defmodule Kernel.CLI.RPCTest do

0 commit comments

Comments
 (0)