summaryrefslogtreecommitdiffstats
path: root/python/mozlint/mozlint/formatters/stylish.py
blob: 3f80bc7ad2f532b9049ffdd84086caab17a46207 (plain)
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from mozterm import Terminal

from ..result import Issue
from ..util.string import pluralize


class StylishFormatter(object):
    """Formatter based on the eslint default."""

    _indent_ = "  "

    # Colors later on in the list are fallbacks in case the terminal
    # doesn't support colors earlier in the list.
    # See http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html
    _colors = {
        "grey": [247, 8, 7],
        "red": [1],
        "green": [2],
        "yellow": [3],
        "brightred": [9, 1],
        "brightyellow": [11, 3],
    }

    fmt = """
  {c1}{lineno}{column}  {c2}{level}{normal}  {message}  {c1}{rule}({linter}){normal}
{diff}""".lstrip(
        "\n"
    )
    fmt_summary = (
        "{t.bold}{c}\u2716 {problem} ({error}, {warning}{failure}, {fixed}){t.normal}"
    )

    def __init__(self, disable_colors=False):
        self.term = Terminal(disable_styling=disable_colors)
        self.num_colors = self.term.number_of_colors

    def color(self, color):
        for num in self._colors[color]:
            if num < self.num_colors:
                return self.term.color(num)
        return ""

    def _reset_max(self):
        self.max_lineno = 0
        self.max_column = 0
        self.max_level = 0
        self.max_message = 0

    def _update_max(self, err):
        """Calculates the longest length of each token for spacing."""
        self.max_lineno = max(self.max_lineno, len(str(err.lineno)))
        if err.column:
            self.max_column = max(self.max_column, len(str(err.column)))
        self.max_level = max(self.max_level, len(str(err.level)))
        self.max_message = max(self.max_message, len(err.message))

    def _get_colored_diff(self, diff):
        if not diff:
            return ""

        new_diff = ""
        for line in diff.split("\n"):
            if line.startswith("+"):
                new_diff += self.color("green")
            elif line.startswith("-"):
                new_diff += self.color("red")
            else:
                new_diff += self.term.normal
            new_diff += self._indent_ + line + "\n"
        return new_diff

    def __call__(self, result):
        message = []
        failed = result.failed

        num_errors = 0
        num_warnings = 0
        num_fixed = result.fixed
        for path, errors in sorted(result.issues.items()):
            self._reset_max()

            message.append(self.term.underline(path))
            # Do a first pass to calculate required padding
            for err in errors:
                assert isinstance(err, Issue)
                self._update_max(err)
                if err.level == "error":
                    num_errors += 1
                else:
                    num_warnings += 1

            for err in sorted(
                errors, key=lambda e: (int(e.lineno), int(e.column or 0))
            ):
                if err.column:
                    col = ":" + str(err.column).ljust(self.max_column)
                else:
                    col = "".ljust(self.max_column + 1)

                args = {
                    "normal": self.term.normal,
                    "c1": self.color("grey"),
                    "c2": self.color("red")
                    if err.level == "error"
                    else self.color("yellow"),
                    "lineno": str(err.lineno).rjust(self.max_lineno),
                    "column": col,
                    "level": err.level.ljust(self.max_level),
                    "rule": "{} ".format(err.rule) if err.rule else "",
                    "linter": err.linter.lower(),
                    "message": err.message.ljust(self.max_message),
                    "diff": self._get_colored_diff(err.diff).ljust(self.max_message),
                }
                message.append(self.fmt.format(**args).rstrip().rstrip("\n"))

            message.append("")  # newline

        # If there were failures, make it clear which linters failed
        for fail in failed:
            message.append(
                "{c}A failure occurred in the {name} linter.".format(
                    c=self.color("brightred"), name=fail
                )
            )

        # Print a summary
        message.append(
            self.fmt_summary.format(
                t=self.term,
                c=self.color("brightred")
                if num_errors or failed
                else self.color("brightyellow"),
                problem=pluralize("problem", num_errors + num_warnings + len(failed)),
                error=pluralize("error", num_errors),
                warning=pluralize(
                    "warning", num_warnings or result.total_suppressed_warnings
                ),
                failure=", {}".format(pluralize("failure", len(failed)))
                if failed
                else "",
                fixed="{} fixed".format(num_fixed),
            )
        )

        if result.total_suppressed_warnings > 0 and num_errors == 0:
            message.append(
                "(pass {c1}-W/--warnings{c2} to see warnings.)".format(
                    c1=self.color("grey"), c2=self.term.normal
                )
            )

        return "\n".join(message)