Skip to content

Commit 87ee689

Browse files
committed
update ansi coloring for warnings
1 parent b400e43 commit 87ee689

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

README.md

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,32 @@ iex(6)> inspect(sql)
6464
## Compile time errors
6565
run `mix sql.get` to generate your `sql.lock` file which is used for error reporting.
6666

67-
```elixir
68-
==> myapp
69-
Compiling 1 file (.ex)
70-
warning:
71-
the relation OPS does not exist
72-
the relation email is mentioned 2 times but does not exist
73-
the relation users does not exist
74-
~SQL"""
75-
select
76-
email,
77-
1 + "OPS"
78-
from
79-
users
80-
where
81-
82-
"""
83-
lib/myapp.ex:18: Myapp.list_users/0
84-
(sql 0.4.0) lib/sql.ex:225: SQL.__inspect__/3
85-
(sql 0.4.0) lib/sql.ex:115: SQL.build/4
86-
(elixir 1.20.0-dev) src/elixir_dispatch.erl:263: :elixir_dispatch.expand_macro_fun/7
87-
(elixir 1.20.0-dev) src/elixir_dispatch.erl:122: :elixir_dispatch.dispatch_import/6
88-
(elixir 1.20.0-dev) src/elixir_clauses.erl:192: :elixir_clauses.def/3
89-
(elixir 1.20.0-dev) src/elixir_def.erl:218: :elixir_def."-store_definition/10-lc$^0/1-0-"/3
90-
(elixir 1.20.0-dev) src/elixir_def.erl:219: :elixir_def.store_definition/10
91-
```
67+
<pre>
68+
==> myapp
69+
Compiling 1 file (.ex)
70+
<span style="color: #00AA00;">warning:</span>
71+
the relation <span style="color: #AA0000;">OPS</span> does not exist
72+
the relation <span style="color: #AA0000;">email</span> is mentioned 2 times but does not exist
73+
the relation <span style="color: #AA0000;">users</span> does not exist
74+
~SQL&quot;&quot;&quot;
75+
<span style="color: #FF55FF">select</span>
76+
<span style="color: #AA0000;">email</span>,
77+
<span style="color: #AA5500;">1</span> <span style="color: #FF55FF">+</span> &quot;<span style="color: #AA0000;">OPS</span>&quot;
78+
<span style="color: #FF55FF">from</span>
79+
<span style="color: #AA0000;">users</span>
80+
<span style="color: #FF55FF">where</span>
81+
<span style="color: #AA0000;">email</span> <span style="color: #FF55FF">=</span> '<span style="color: #00AA00;">[email protected]</span>'
82+
&quot;&quot;&quot;
83+
lib/myapp.ex:18: Myapp.list_users/0
84+
(sql 0.4.0) lib/sql.ex:225: SQL.__inspect__/3
85+
(sql 0.4.0) lib/sql.ex:115: SQL.build/4
86+
(elixir 1.20.0-dev) src/elixir_dispatch.erl:263: :elixir_dispatch.expand_macro_fun/7
87+
(elixir 1.20.0-dev) src/elixir_dispatch.erl:122: :elixir_dispatch.dispatch_import/6
88+
(elixir 1.20.0-dev) src/elixir_clauses.erl:192: :elixir_clauses.def/3
89+
(elixir 1.20.0-dev) src/elixir_def.erl:218: :elixir_def."-store_definition/10-lc$^0/1-0-"/3
90+
(elixir 1.20.0-dev) src/elixir_def.erl:219: :elixir_def.store_definition/10
91+
</pre>
92+
9293

9394
## Benchmark
9495
You can find benchmark results [here](https://github.com/elixir-dbvisor/sql/benchmarks) or run `mix sql.bench`

lib/sql.ex

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,24 +219,27 @@ defmodule SQL do
219219
end
220220
end
221221

222+
@error IO.ANSI.red()
223+
@reset IO.ANSI.reset()
224+
222225
@doc false
223226
def __inspect__(tokens, context, stack) do
224-
inspect = IO.iodata_to_binary(["\e[0m", "~SQL\"\"\""|[SQL.Format.to_iodata(tokens, context)|~c"\n\"\"\""]])
227+
inspect = IO.iodata_to_binary([@reset, "~SQL\"\"\""|[SQL.Format.to_iodata(tokens, context)|~c"\n\"\"\""]])
225228
case context.errors do
226229
[] -> inspect
227230
errors ->
228231
{:current_stacktrace, [_|t]} = Process.info(self(), :current_stacktrace)
229-
IO.warn([?\n,format_error(errors), IO.iodata_to_binary(["\e[0m", " ~SQL\"\"\""|[SQL.Format.to_iodata(tokens, context, 1)|~c"\n \"\"\""]])], [stack|t])
232+
IO.warn([?\n,format_error(errors), IO.iodata_to_binary([@reset, " ~SQL\"\"\""|[SQL.Format.to_iodata(tokens, context, 1)|~c"\n \"\"\""]])], [stack|t])
230233
inspect
231234
end
232235
end
233236

234237
@doc false
235238
def format_error(errors), do: Enum.group_by(errors, &elem(&1, 2)) |> Enum.reduce([], fn
236-
{k, [{:special, _, _}]}, acc -> [acc|[" the operator \e[31m",k,"\e[0m is invalid, did you mean any of #{__suggest__(k)}\n"]]
237-
{k, [{:special, _, _}|_]=v}, acc -> [acc|[" the operator \e[31m",k,"\e[0m is mentioned #{length(v)} times but is invalid, did you mean any of #{__suggest__(k)}\n"]]
238-
{k, [_]}, acc -> [acc|[" the relation \e[31m",k,"\e[0m does not exist\n"]]
239-
{k, v}, acc -> [acc|[" the relation \e[31m",k,"\e[0m is mentioned #{length(v)} times but does not exist\n"]]
239+
{k, [{:special, _, _}]}, acc -> [acc|[" the operator", @error,k,@reset, " is invalid, did you mean any of #{__suggest__(k)}\n"]]
240+
{k, [{:special, _, _}|_]=v}, acc -> [acc|[" the operator ",@error,k,@reset," is mentioned #{length(v)} times but is invalid, did you mean any of #{__suggest__(k)}\n"]]
241+
{k, [_]}, acc -> [acc|[" the relation ",@error,k,@reset," does not exist\n"]]
242+
{k, v}, acc -> [acc|[" the relation ",@error,k,@reset," is mentioned #{length(v)} times but does not exist\n"]]
240243
end)
241244

242245
@doc false

0 commit comments

Comments
 (0)