-
Notifications
You must be signed in to change notification settings - Fork 414
/
output.ex
255 lines (206 loc) · 5.36 KB
/
output.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
defmodule Credo.CLI.Output do
@moduledoc """
This module provides helper functions regarding command line output.
"""
@category_tag_map %{"refactor" => "F"}
alias Credo.CLI.Output.UI
alias Credo.Execution
alias Credo.Priority
def check_tag(category, in_parens \\ true)
def check_tag(category, in_parens) when is_binary(category) do
default_tag =
category
|> String.at(0)
|> String.upcase()
tag = Map.get(@category_tag_map, category, default_tag)
if in_parens do
"[#{tag}]"
else
tag
end
end
def check_tag(category, in_parens) when is_atom(category) do
category
|> to_string
|> check_tag(in_parens)
end
def check_tag(check_mod, in_parens) do
check_mod.category
|> to_string
|> check_tag(in_parens)
end
def check_color(category) when is_binary(category) do
case category do
"consistency" -> :cyan
"readability" -> :blue
"design" -> :olive
"refactor" -> :yellow
"warning" -> :red
_ -> :magenta
end
end
def check_color(category) when is_atom(category) do
category
|> to_string
|> check_color
end
def check_color(check_mod) do
check_mod.category
|> to_string
|> check_color
end
@doc """
Returns a suitable color for a given priority.
iex> Credo.CLI.Output.issue_color(%Credo.Issue{priority: :higher})
:red
iex> Credo.CLI.Output.issue_color(%Credo.Issue{priority: 20})
:red
"""
def issue_color(issue_or_priority) do
case Priority.to_atom(issue_or_priority) do
:higher -> :red
:high -> :red
:normal -> :yellow
:low -> :blue
:ignore -> :magenta
_ -> "?"
end
end
@doc """
Returns a suitable arrow for a given priority.
iex> Credo.CLI.Output.priority_arrow(:high)
"↗"
iex> Credo.CLI.Output.priority_arrow(10)
"↗"
iex> Credo.CLI.Output.priority_arrow(%Credo.Issue{priority: 10})
"↗"
"""
def priority_arrow(issue_or_priority) do
case Priority.to_atom(issue_or_priority) do
:higher -> "\u2191"
:high -> "\u2197"
:normal -> "\u2192"
:low -> "\u2198"
:ignore -> "\u2193"
_ -> "?"
end
end
@doc """
Returns a suitable name for a given priority.
iex> Credo.CLI.Output.priority_name(:normal)
"normal"
iex> Credo.CLI.Output.priority_name(1)
"normal"
iex> Credo.CLI.Output.priority_name(%Credo.Issue{priority: 1})
"normal"
"""
def priority_name(issue_or_priority) do
case Priority.to_atom(issue_or_priority) do
:higher -> "higher"
:high -> "high"
:normal -> "normal"
:low -> "low"
:ignore -> "ignore"
_ -> "?"
end
end
@doc """
Returns a suitable foreground color for a given `background_color`.
iex> Credo.CLI.Output.foreground_color(:yellow)
:black
iex> Credo.CLI.Output.foreground_color(:blue)
:white
"""
def foreground_color(background_color)
def foreground_color(:cyan), do: :black
def foreground_color(:yellow), do: :black
def foreground_color(_), do: :white
def term_columns(default \\ 80) do
case :io.columns() do
{:ok, columns} ->
columns
_ ->
default
end
end
def complain_about_invalid_source_files([]), do: nil
def complain_about_invalid_source_files(invalid_source_files) do
invalid_source_filenames = Enum.map(invalid_source_files, & &1.filename)
output = [
:reset,
:bright,
:orange,
"info: ",
:red,
"Some source files could not be parsed correctly and are excluded:\n"
]
UI.warn(output)
print_numbered_list(invalid_source_filenames)
end
def complain_about_timed_out_source_files([]), do: nil
def complain_about_timed_out_source_files(large_source_files) do
large_source_filenames = Enum.map(large_source_files, & &1.filename)
output = [
:reset,
:bright,
:orange,
"info: ",
:red,
"Some source files were not parsed in the time allotted:\n"
]
UI.warn(output)
print_numbered_list(large_source_filenames)
end
def print_skipped_checks(%Execution{skipped_checks: []}), do: nil
def print_skipped_checks(%Execution{skipped_checks: skipped_checks}) do
msg = [
:reset,
:bright,
:orange,
"info: ",
:reset,
:faint,
"some checks were skipped because they're not compatible with\n",
:reset,
:faint,
"your version of Elixir (#{System.version()}).\n\n",
"You can deactivate these checks by adding this to the `checks` list in your config:\n"
]
UI.puts("")
UI.puts(msg)
skipped_checks
|> Enum.map(&check_name/1)
|> print_disabled_check_config
end
defp check_name({check, _check_info}), do: check_name({check})
defp check_name({check}) do
check
|> to_string
|> String.replace(~r/^Elixir\./, "")
end
defp print_numbered_list(list) do
list
|> Enum.with_index()
|> Enum.flat_map(fn {string, index} ->
[
:reset,
String.pad_leading("#{index + 1})", 5),
:faint,
" #{string}\n"
]
end)
|> UI.warn()
end
defp print_disabled_check_config(list) do
list
|> Enum.flat_map(fn string ->
[
:reset,
String.pad_leading(" ", 4),
:faint,
"{#{string}, false},\n"
]
end)
|> UI.puts()
end
end