From bb1a073288674d3207f0a7d29b7d2775add4cb7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Alet?= Date: Thu, 17 Mar 2022 11:25:08 +1100 Subject: [PATCH] feat. Adds support for colored summary. Closes #38. --- README.md | 19 ++++++++++--------- setup.cfg | 2 +- yamlfixer/__init__.py | 19 ++++++++++++++++--- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 68aaef6..425538f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/setup.cfg b/setup.cfg index f8949d1..55e82c4 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/yamlfixer/__init__.py b/yamlfixer/__init__.py index d571287..5bd9f0a 100755 --- a/yamlfixer/__init__.py +++ b/yamlfixer/__init__.py @@ -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", @@ -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 = {} @@ -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") @@ -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), @@ -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`") @@ -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.")