From b300dfb3d75fe76e2ddbb087ff742dd0b5e6155b Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 7 Dec 2021 15:34:20 -0800 Subject: [PATCH 01/10] Add support for NO_COLOR env variable --- jc/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jc/cli.py b/jc/cli.py index a81a1c32..8b9bd286 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -526,7 +526,7 @@ def main(): about = 'a' in options debug = 'd' in options verbose_debug = options.count('d') > 1 - mono = 'm' in options + mono = bool('m' in options or os.getenv('NO_COLOR')) help_me = 'h' in options pretty = 'p' in options quiet = 'q' in options From 82e0160de820ad2ed6143f58458711d600b929a3 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 7 Dec 2021 15:45:11 -0800 Subject: [PATCH 02/10] refactor NO_COLOR test --- jc/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jc/cli.py b/jc/cli.py index 8b9bd286..5ea295e7 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -526,7 +526,7 @@ def main(): about = 'a' in options debug = 'd' in options verbose_debug = options.count('d') > 1 - mono = bool('m' in options or os.getenv('NO_COLOR')) + mono = 'm' in options or bool(os.getenv('NO_COLOR')) help_me = 'h' in options pretty = 'p' in options quiet = 'q' in options From 9621475e86fe20fcdc2902b6ce7860a833ae2ca0 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 7 Dec 2021 15:46:02 -0800 Subject: [PATCH 03/10] changelog update --- CHANGELOG | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 5c1e86b3..4d17da33 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,8 @@ jc changelog +20211207 v1.17.4 +- Add support for the NO_COLOR environment variable to force mono + 20211202 v1.17.3 - Update parsers to exit with error if non-string input is detected (raise TypeError) - Update streaming parsers to exit with error if non-iterable input is detected (raise TypeError) From df190aa299f1669d1fe09380026ae086839abc02 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 8 Dec 2021 08:14:28 -0800 Subject: [PATCH 04/10] add -C option to force color even with pipes --- jc/cli.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/jc/cli.py b/jc/cli.py index 5ea295e7..88fc4001 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -235,9 +235,11 @@ def set_env_colors(env_colors=None): } -def piped_output(): - """Return False if stdout is a TTY. True if output is being piped to another program""" - return not sys.stdout.isatty() +def piped_output(force_color): + """Return False if stdout is a TTY. True if output is being piped to another program + and foce_color is True. This allows forcing of ANSI color codes even when using pipes. + """ + return not sys.stdout.isatty() and not force_color def ctrlc(signum, frame): @@ -335,6 +337,7 @@ def helptext(): {parsers_string} Options: -a about jc + -C force color output even when using pipes (overrides -m) -d debug (-dd for verbose debug) -h help (-h --parser_name for parser documentation) -m monochrome output @@ -526,7 +529,8 @@ def main(): about = 'a' in options debug = 'd' in options verbose_debug = options.count('d') > 1 - mono = 'm' in options or bool(os.getenv('NO_COLOR')) + force_color = 'C' in options + mono = ('m' in options or bool(os.getenv('NO_COLOR'))) and not force_color help_me = 'h' in options pretty = 'p' in options quiet = 'q' in options @@ -542,7 +546,11 @@ def main(): mono = True if about: - print(json_out(about_jc(), pretty=pretty, env_colors=jc_colors, mono=mono, piped_out=piped_output())) + print(json_out(about_jc(), + pretty=pretty, + env_colors=jc_colors, + mono=mono, + piped_out=piped_output(force_color))) sys.exit(0) if help_me: @@ -632,7 +640,7 @@ def main(): pretty=pretty, env_colors=jc_colors, mono=mono, - piped_out=piped_output()), + piped_out=piped_output(force_color)), flush=unbuffer) sys.exit(combined_exit_code(magic_exit_code, 0)) @@ -645,7 +653,7 @@ def main(): pretty=pretty, env_colors=jc_colors, mono=mono, - piped_out=piped_output()), + piped_out=piped_output(force_color)), flush=unbuffer) sys.exit(combined_exit_code(magic_exit_code, 0)) From 7ac468e35aac1a63c4e22f33aead74a6e41a22a6 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 8 Dec 2021 08:14:36 -0800 Subject: [PATCH 05/10] changelog update --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index 4d17da33..8a2ab321 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ jc changelog 20211207 v1.17.4 - Add support for the NO_COLOR environment variable to force mono +- Add -C option to force color output even when using pipes (overrides -m and NO_COLOR) 20211202 v1.17.3 - Update parsers to exit with error if non-string input is detected (raise TypeError) From 4fdb34c7d5fcaa65e2ccab814c3acf2f0f70c442 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 8 Dec 2021 08:19:43 -0800 Subject: [PATCH 06/10] add no-color.org --- CHANGELOG | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 8a2ab321..2f25f31a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,7 +1,7 @@ jc changelog 20211207 v1.17.4 -- Add support for the NO_COLOR environment variable to force mono +- Add support for the NO_COLOR environment variable to set mono (http://no-color.org/) - Add -C option to force color output even when using pipes (overrides -m and NO_COLOR) 20211202 v1.17.3 From ac7c13fcc013d386f34db51d634298fa3e97eccd Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 8 Dec 2021 08:22:28 -0800 Subject: [PATCH 07/10] add -C option to docs --- README.md | 1 + man/jc.1 | 6 +++++- templates/manpage_template | 4 ++++ templates/readme_template | 1 + 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e11519a5..74ba9ad3 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,7 @@ The JSON output can be compact (default) or pretty formatted with the `-p` optio ### Options - `-a` about `jc`. Prints information about `jc` and the parsers (in JSON, of course!) +- `-C` force color output even when using pipes (overrides `-m` and the `NO_COLOR` env variable) - `-d` debug mode. Prints trace messages if parsing issues are encountered (use `-dd` for verbose debugging) - `-h` help. Use `jc -h --parser_name` for parser documentation - `-m` monochrome JSON output diff --git a/man/jc.1 b/man/jc.1 index 4c990e69..812da859 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -1,4 +1,4 @@ -.TH jc 1 2021-12-02 1.17.3 "JSON CLI output utility" +.TH jc 1 2021-12-08 1.17.3 "JSON CLI output utility" .SH NAME jc \- JSONifies the output of many CLI tools and file-types .SH SYNOPSIS @@ -440,6 +440,10 @@ Options: about jc (JSON output) .TP .B +\fB-C\fP +force color output even when using pipes (overrides `-m` and the `NO_COLOR` env variable) +.TP +.B \fB-d\fP debug - show traceback (use \fB-dd\fP for verbose traceback) .TP diff --git a/templates/manpage_template b/templates/manpage_template index c07b64c1..a35c93d0 100644 --- a/templates/manpage_template +++ b/templates/manpage_template @@ -35,6 +35,10 @@ Options: about jc (JSON output) .TP .B +\fB-C\fP +force color output even when using pipes (overrides `-m` and the `NO_COLOR` env variable) +.TP +.B \fB-d\fP debug - show traceback (use \fB-dd\fP for verbose traceback) .TP diff --git a/templates/readme_template b/templates/readme_template index ea81e823..c2c151f2 100644 --- a/templates/readme_template +++ b/templates/readme_template @@ -110,6 +110,7 @@ The JSON output can be compact (default) or pretty formatted with the `-p` optio ### Options - `-a` about `jc`. Prints information about `jc` and the parsers (in JSON, of course!) +- `-C` force color output even when using pipes (overrides `-m` and the `NO_COLOR` env variable) - `-d` debug mode. Prints trace messages if parsing issues are encountered (use `-dd` for verbose debugging) - `-h` help. Use `jc -h --parser_name` for parser documentation - `-m` monochrome JSON output From 34df643f60712ae25645cb7cddff80e43c12262b Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 8 Dec 2021 08:35:09 -0800 Subject: [PATCH 08/10] add disable colors section --- README.md | 3 +++ man/jc.1 | 6 ++++++ templates/manpage_template | 6 ++++++ templates/readme_template | 3 +++ 4 files changed, 18 insertions(+) diff --git a/README.md b/README.md index 74ba9ad3..de82c8f0 100644 --- a/README.md +++ b/README.md @@ -230,6 +230,9 @@ or JC_COLORS=default,default,default,default ``` +### Disable Colors via Environment Variable +You can set the [`NO_COLOR`](http://no-color.org/) environment variable to any value to disable color output in `jc`. Note that using the `-C` option to force color output will override both the `NO_COLOR` environment variable and the `-m` option. + ### Streaming Parsers Most parsers load all of the data from STDIN, parse it, then output the entire JSON document serially. There are some streaming parsers (e.g. `ls-s` and `ping-s`) that immediately start processing and outputing the data line-by-line as [JSON Lines](https://jsonlines.org/) (aka [NDJSON](http://ndjson.org/)) while it is being received from STDIN. This can significantly reduce the amount of memory required to parse large amounts of command output (e.g. `ls -lR /`) and can sometimes process the data more quickly. Streaming parsers have slightly different behavior than standard parsers as outlined below. diff --git a/man/jc.1 b/man/jc.1 index 812da859..cf2b1ddd 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -491,6 +491,9 @@ ifconfig exit code = \fB1\fP, jc exit code = \fB100\fP, combined exit code = \fB .RE .SH ENVIRONMENT + +\fBCustom Colors\fP + You can specify custom colors via the \fBJC_COLORS\fP environment variable. The \fBJC_COLORS\fP environment variable takes four comma separated string values in the following format: JC_COLORS=,,, @@ -507,6 +510,9 @@ or JC_COLORS=default,default,default,default .RE +\fBDisable Color Output\fP + +You can set the `NO_COLOR` environment variable to any value to disable color output in \fBjc\fP. Note that using the \fB-C\fP option to force color output will override both the \fBNO_COLOR\fP environment variable and the \fB-m\fP option. .SH STREAMING PARSERS Most parsers load all of the data from \fBSTDIN\fP, parse it, then output the entire JSON document serially. There are some streaming parsers (e.g. \fBls-s\fP and \fBping-s\fP) that immediately start processing and outputing the data line-by-line as JSON Lines (aka NDJSON) while it is being received from \fBSTDIN\fP. This can significantly reduce the amount of memory required to parse large amounts of command output (e.g. \fBls -lR /\fP) and can sometimes process the data more quickly. Streaming parsers have slightly different behavior than standard parsers as outlined below. diff --git a/templates/manpage_template b/templates/manpage_template index a35c93d0..8748671e 100644 --- a/templates/manpage_template +++ b/templates/manpage_template @@ -86,6 +86,9 @@ ifconfig exit code = \fB1\fP, jc exit code = \fB100\fP, combined exit code = \fB .RE .SH ENVIRONMENT + +\fBCustom Colors\fP + You can specify custom colors via the \fBJC_COLORS\fP environment variable. The \fBJC_COLORS\fP environment variable takes four comma separated string values in the following format: JC_COLORS=,,, @@ -102,6 +105,9 @@ or JC_COLORS=default,default,default,default .RE +\fBDisable Color Output\fP + +You can set the `NO_COLOR` environment variable to any value to disable color output in \fBjc\fP. Note that using the \fB-C\fP option to force color output will override both the \fBNO_COLOR\fP environment variable and the \fB-m\fP option. .SH STREAMING PARSERS Most parsers load all of the data from \fBSTDIN\fP, parse it, then output the entire JSON document serially. There are some streaming parsers (e.g. \fBls-s\fP and \fBping-s\fP) that immediately start processing and outputing the data line-by-line as JSON Lines (aka NDJSON) while it is being received from \fBSTDIN\fP. This can significantly reduce the amount of memory required to parse large amounts of command output (e.g. \fBls -lR /\fP) and can sometimes process the data more quickly. Streaming parsers have slightly different behavior than standard parsers as outlined below. diff --git a/templates/readme_template b/templates/readme_template index c2c151f2..f4b38b68 100644 --- a/templates/readme_template +++ b/templates/readme_template @@ -149,6 +149,9 @@ or JC_COLORS=default,default,default,default ``` +### Disable Colors via Environment Variable +You can set the [`NO_COLOR`](http://no-color.org/) environment variable to any value to disable color output in `jc`. Note that using the `-C` option to force color output will override both the `NO_COLOR` environment variable and the `-m` option. + ### Streaming Parsers Most parsers load all of the data from STDIN, parse it, then output the entire JSON document serially. There are some streaming parsers (e.g. `ls-s` and `ping-s`) that immediately start processing and outputing the data line-by-line as [JSON Lines](https://jsonlines.org/) (aka [NDJSON](http://ndjson.org/)) while it is being received from STDIN. This can significantly reduce the amount of memory required to parse large amounts of command output (e.g. `ls -lR /`) and can sometimes process the data more quickly. Streaming parsers have slightly different behavior than standard parsers as outlined below. From 34cb75a09697a06c3878f2f9a84eb3bd2a90ae62 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 8 Dec 2021 08:46:00 -0800 Subject: [PATCH 09/10] version bump --- jc/__init__.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jc/__init__.py b/jc/__init__.py index c7a39794..88dc4416 100644 --- a/jc/__init__.py +++ b/jc/__init__.py @@ -73,4 +73,4 @@ Module Example: """ name = 'jc' -__version__ = '1.17.3' +__version__ = '1.17.4' diff --git a/setup.py b/setup.py index c9ed7588..8fb44cc3 100755 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open('README.md', 'r') as f: setuptools.setup( name='jc', - version='1.17.3', + version='1.17.4', author='Kelly Brazil', author_email='kellyjonbrazil@gmail.com', description='Converts the output of popular command-line tools and file-types to JSON.', From b4506976e3c865397bc657183d49c484d8bcfd7a Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 8 Dec 2021 11:21:12 -0800 Subject: [PATCH 10/10] formatting --- man/jc.1 | 8 ++++---- templates/manpage_template | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/man/jc.1 b/man/jc.1 index cf2b1ddd..f953c5a6 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -1,4 +1,4 @@ -.TH jc 1 2021-12-08 1.17.3 "JSON CLI output utility" +.TH jc 1 2021-12-08 1.17.4 "JSON CLI output utility" .SH NAME jc \- JSONifies the output of many CLI tools and file-types .SH SYNOPSIS @@ -441,7 +441,7 @@ about jc (JSON output) .TP .B \fB-C\fP -force color output even when using pipes (overrides `-m` and the `NO_COLOR` env variable) +force color output even when using pipes (overrides \fB-m\fP and the \fBNO_COLOR\fP env variable) .TP .B \fB-d\fP @@ -478,7 +478,7 @@ version information .SH EXIT CODES Any fatal errors within jc will generate an exit code of \fB100\fP, otherwise the exit code will be \fB0\fP. When using the "Magic" syntax (e.g. \fBjc ifconfig eth0\fP), jc will store the exit code of the program being parsed and add it to the jc exit code. This way it is easier to determine if an error was from the parsed program or jc. -Consider the following examples using `ifconfig`: +Consider the following examples using \fBifconfig\fP: .RS ifconfig exit code = \fB0\fP, jc exit code = \fB0\fP, combined exit code = \fB0\fP (no errors) @@ -512,7 +512,7 @@ JC_COLORS=default,default,default,default \fBDisable Color Output\fP -You can set the `NO_COLOR` environment variable to any value to disable color output in \fBjc\fP. Note that using the \fB-C\fP option to force color output will override both the \fBNO_COLOR\fP environment variable and the \fB-m\fP option. +You can set the \fBNO_COLOR\fB environment variable to any value to disable color output in \fBjc\fP. Note that using the \fB-C\fP option to force color output will override both the \fBNO_COLOR\fP environment variable and the \fB-m\fP option. .SH STREAMING PARSERS Most parsers load all of the data from \fBSTDIN\fP, parse it, then output the entire JSON document serially. There are some streaming parsers (e.g. \fBls-s\fP and \fBping-s\fP) that immediately start processing and outputing the data line-by-line as JSON Lines (aka NDJSON) while it is being received from \fBSTDIN\fP. This can significantly reduce the amount of memory required to parse large amounts of command output (e.g. \fBls -lR /\fP) and can sometimes process the data more quickly. Streaming parsers have slightly different behavior than standard parsers as outlined below. diff --git a/templates/manpage_template b/templates/manpage_template index 8748671e..cb9cfef2 100644 --- a/templates/manpage_template +++ b/templates/manpage_template @@ -36,7 +36,7 @@ about jc (JSON output) .TP .B \fB-C\fP -force color output even when using pipes (overrides `-m` and the `NO_COLOR` env variable) +force color output even when using pipes (overrides \fB-m\fP and the \fBNO_COLOR\fP env variable) .TP .B \fB-d\fP @@ -73,7 +73,7 @@ version information .SH EXIT CODES Any fatal errors within jc will generate an exit code of \fB100\fP, otherwise the exit code will be \fB0\fP. When using the "Magic" syntax (e.g. \fBjc ifconfig eth0\fP), jc will store the exit code of the program being parsed and add it to the jc exit code. This way it is easier to determine if an error was from the parsed program or jc. -Consider the following examples using `ifconfig`: +Consider the following examples using \fBifconfig\fP: .RS ifconfig exit code = \fB0\fP, jc exit code = \fB0\fP, combined exit code = \fB0\fP (no errors) @@ -107,7 +107,7 @@ JC_COLORS=default,default,default,default \fBDisable Color Output\fP -You can set the `NO_COLOR` environment variable to any value to disable color output in \fBjc\fP. Note that using the \fB-C\fP option to force color output will override both the \fBNO_COLOR\fP environment variable and the \fB-m\fP option. +You can set the \fBNO_COLOR\fB environment variable to any value to disable color output in \fBjc\fP. Note that using the \fB-C\fP option to force color output will override both the \fBNO_COLOR\fP environment variable and the \fB-m\fP option. .SH STREAMING PARSERS Most parsers load all of the data from \fBSTDIN\fP, parse it, then output the entire JSON document serially. There are some streaming parsers (e.g. \fBls-s\fP and \fBping-s\fP) that immediately start processing and outputing the data line-by-line as JSON Lines (aka NDJSON) while it is being received from \fBSTDIN\fP. This can significantly reduce the amount of memory required to parse large amounts of command output (e.g. \fBls -lR /\fP) and can sometimes process the data more quickly. Streaming parsers have slightly different behavior than standard parsers as outlined below.