Skip to content

Commit

Permalink
feat. Adds support for colored summary. Closes #38.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamere-allo-peter committed Mar 17, 2022
1 parent 155a579 commit bb1a073
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,23 @@ This software automatically fixes some errors and warnings reported by
`yamllint`.

```shell
yamlfixer --help
usage: yamlfixer [-h] [-v] [-b] [-d] [-j | -s] [file [file ...]]
usage: yamlfixer [-h] [-v] [-b] [-d] [-j | -c | -s] [file [file ...]]

Fix formatting problems in YAML documents. If no file is specified,
then reads input from `stdin`.

positional arguments:
file the YAML files to fix. Use `-` to read from `stdin`.
file the YAML files to fix. Use `-` to read from `stdin`.

optional arguments:
-h, --help show this help message and exit
-v, --version display this program's version number
-b, --backup make a backup copy of original files as `.orig`
-d, --debug output debug information to stderr.
-j, --jsonsummary output JSON summary to stderr.
-s, --summary output plain text summary to stderr.
-h, --help show this help message and exit
-v, --version display this program's version number and exit.
-b, --backup make a backup copy of original files as `.orig`
-d, --debug output debug information to stderr.
-j, --jsonsummary output JSON summary to stderr.
-c, --colorsummary output colored plain text summary to stderr. If stderr
is not a TTY output is identical to --summary.
-s, --summary output plain text summary to stderr.
```
yamlfixer launches `yamllint` on each specified filename, then parses
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[metadata]
version = 0.3.3
version = 0.3.4
name = yamlfixer-opt-nc
description = automates the fixing of problems reported by yamllint
long_description = file: README.md
Expand Down
19 changes: 16 additions & 3 deletions yamlfixer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import argparse
import json

__version__ = "0.3.3"
__version__ = "0.3.4"
__author__ = "OPT-NC"
__license__ = "GPLv3+"
__copyright__ = "Copyright (C) 2021-%s %s" % (time.strftime("%Y",
Expand Down Expand Up @@ -46,6 +46,13 @@
EXIT_NOK = -1
EXIT_PROBLEM = -2

COLORSEQ = {"PASSED": "32m",
"MODIFIED": "34m",
"SKIPPED": "36m",
"ERROR": "31m",
"UNKNOWN": "33m",
}

class ProblemFixer:
"""To hold problem fixing logic."""
fixers = {}
Expand Down Expand Up @@ -403,7 +410,7 @@ def error(self, message): # pylint: disable=no-self-use

def statistics(self):
"""Output some statistics."""
if self.arguments.summary:
if self.arguments.summary or self.arguments.colorsummary:
self.info(f"Files to fix: {len(self.arguments.filenames)}")
self.info(f"{self.passed} files successfully passed yamllint strict mode")
self.info(f"{self.modified} files were modified")
Expand All @@ -415,6 +422,8 @@ def statistics(self):
msg = f" (handled {handled}/{issues})"
else:
msg = ""
if self.arguments.colorsummary and sys.stderr.isatty():
status = f"\033[{COLORSEQ.get(status.strip())}{status}\033[0m"
self.info(f"{status} {filename}{msg}")
elif self.arguments.jsonsummary:
summarymapping = {"filestofix": len(self.arguments.filenames),
Expand Down Expand Up @@ -488,7 +497,7 @@ def run():
cmdline.add_argument("-v", "--version",
action="version",
version=f"yamlfixer v{__version__}",
help="display this program's version number")
help="display this program's version number and exit.")
cmdline.add_argument("-b", "--backup",
action="store_true",
help="make a backup copy of original files as `.orig`")
Expand All @@ -499,6 +508,10 @@ def run():
mutuallyexclusive.add_argument("-j", "--jsonsummary",
action="store_true",
help="output JSON summary to stderr.")
mutuallyexclusive.add_argument("-c", "--colorsummary",
action="store_true",
help="output colored plain text summary to stderr. "
"If stderr is not a TTY output is identical to --summary.")
mutuallyexclusive.add_argument("-s", "--summary",
action="store_true",
help="output plain text summary to stderr.")
Expand Down

0 comments on commit bb1a073

Please sign in to comment.