-
Notifications
You must be signed in to change notification settings - Fork 413
/
output.ex
125 lines (111 loc) · 3.48 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
defmodule Credo.CLI.Output do
alias Credo.CLI.Output.UI
alias Credo.Config
@category_tag_map %{"refactor" => "F"}
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
def issue_color(issue) do
priority = issue.priority
cond do
priority in 20..999 -> :red
priority in 10..19 -> :red
priority in 0..9 -> :yellow
priority in -10..-1 -> :blue
priority in -999..-11 -> :magenta
true -> "?"
end
end
def priority_arrow(priority) do
cond do
priority in 20..999 -> "\u2191"
priority in 10..19 -> "\u2197"
priority in 0..9 -> "\u2192"
priority in -2..-1 -> "\u2198"
priority in -999..-1 -> "\u2193"
true -> "?"
end
end
def priority_name(priority) do
cond do
priority in 20..999 -> "high"
priority in 10..19 -> "medium"
priority in 0..9 -> "normal"
priority in -2..-1 -> "low"
priority in -999..-1 -> "lower"
true -> "?"
end
end
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 = [
:red, "Some source files could not be parsed correctly and are excluded:\n",
]
UI.puts(output)
invalid_source_filenames
|> Enum.with_index
|> Enum.flat_map(fn({filename, index}) ->
[:reset, String.rjust("#{index+1})", 5), :faint, " #{filename}\n"]
end)
|> UI.puts
end
def print_skipped_checks(%Config{skipped_checks: []}), do: nil
def print_skipped_checks(%Config{skipped_checks: skipped_checks}) do
msg =
[
:reset, :bright, :orange, "info: ", :reset, :faint, "the following checks were skipped because they're not compatible with\n",
:reset, :faint, "your version of Elixir (#{System.version()}). Upgrade to the newest version of Elixir to\n",
:reset, :faint, "get the most out of Credo!\n",
]
UI.puts
UI.puts(msg, :faint)
Enum.each(skipped_checks, &print_skipped_check_name/1)
UI.puts
end
defp print_skipped_check_name({check, _check_info}) do
check_name =
check
|> to_string
|> String.replace(~r/^Elixir\./, "")
UI.puts(" - #{check_name}", :faint)
end
end