From 4a22e27d6a80a988f4de17bce421110d6abb8867 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 12 Apr 2020 12:43:51 -0700 Subject: [PATCH 1/5] add set_env_colors function --- jc/cli.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/jc/cli.py b/jc/cli.py index a031aadf..5203156f 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -89,6 +89,70 @@ class JcStyle(Style): } +def set_env_colors(keyname_color, keyword_color, number_color, string_color): + """ + This function does not return a value. It just updates the JcStyles.styles dictionary. + + Grab custom colors from JELLO_COLORS environment variable. + + JELLO_COLORS env variable takes 4 comma separated string values and should be in the format of: + + JELLO_COLORS=,,, + + Where colors are: black, red, green, yellow, blue, magenta, cyan, gray, brightblack, brightred, + brightgreen, brightyellow, brightblue, brightmagenta, brightcyan, white, default + + Alternatively, numeric color codes can be used. For example: #ff0000 = red + + Default colors: + + JELLO_COLORS=blue,brightblack,magenta,green + or + JELLO_COLORS=default,default,default,default + + """ + env_colors = os.getenv('JELLO_COLORS') + input_error = False + + if env_colors: + color_list = env_colors.split(',') + else: + input_error = True + + if env_colors and len(color_list) != 4: + print('jc: Warning: could not parse JC_COLORS environment variable\n', file=sys.stderr) + input_error = True + + if env_colors: + for color in color_list: + if color not in ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'gray', 'brightblack', 'brightred', + 'brightgreen', 'brightyellow', 'brightblue', 'brightmagenta', 'brightcyan', 'white', 'default']: + print('jc: Warning: could not parse JC_COLORS environment variable\n', file=sys.stderr) + input_error = True + + # if there is an issue with the env variable, just set all colors to default and move on + if input_error: + color_list = ['default', 'default', 'default', 'default'] + + # Try the color set in the JELLO_COLORS env variable first. If it is set to default, then fall back to default colors + class JcStyle(Style): + styles = { + Name.Tag: f'bold ansi{color_list[0]}' if not color_list[0] == 'default' else f'bold ansiblue' # key names + Keyword: f'bold ansi{color_list[1]}' if not color_list[1] == 'default' else f'ansibrightblack' # true, false, null + Number: f'bold ansi{color_list[2]}' if not color_list[2] == 'default' else f'magenta' # numbers + String: f'bold ansi{color_list[3]}' if not color_list[3] == 'default' else f'green' # strings + } + + JelloTheme.colors = { + 'key_name': color_map[color_list[0]] if not color_list[0] == 'default' else color_map[keyname_color] if keyname_color else color_map['blue'], + 'keyword': color_map[color_list[1]] if not color_list[1] == 'default' else color_map[keyword_color] if keyword_color else color_map['brightblack'], + 'number': color_map[color_list[2]] if not color_list[2] == 'default' else color_map[number_color] if number_color else color_map['magenta'], + 'string': color_map[color_list[3]] if not color_list[3] == 'default' else color_map[string_color] if string_color else color_map['green'], + 'array_id': color_map[color_list[4]] if not color_list[4] == 'default' else color_map[arrayid_color] if arrayid_color else color_map['red'], + 'array_bracket': color_map[color_list[5]] if not color_list[5] == 'default' else color_map[arraybracket_color] if arraybracket_color else color_map['magenta'] + } + + def piped_output(): """returns False if stdout is a TTY. True if output is being piped to another program""" if sys.stdout.isatty(): From 421b9809575a8de00229af4b851739889638ac74 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 12 Apr 2020 13:03:09 -0700 Subject: [PATCH 2/5] JC_COLORS working --- jc/cli.py | 55 ++++++++++++++++++------------------------------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/jc/cli.py b/jc/cli.py index 5203156f..cf48f898 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -80,38 +80,24 @@ parsers = [ ] -class JcStyle(Style): - styles = { - Name.Tag: 'bold ansiblue', # key names - Keyword: 'ansibrightblack', # true, false, null - Number: 'ansimagenta', # int, float - String: 'ansigreen' # string - } - - -def set_env_colors(keyname_color, keyword_color, number_color, string_color): +def set_env_colors(): """ - This function does not return a value. It just updates the JcStyles.styles dictionary. + Grab custom colors from JC_COLORS environment variable. JC_COLORS env variable takes 4 comma + separated string values and should be in the format of: - Grab custom colors from JELLO_COLORS environment variable. - - JELLO_COLORS env variable takes 4 comma separated string values and should be in the format of: - - JELLO_COLORS=,,, + JC_COLORS=,,, Where colors are: black, red, green, yellow, blue, magenta, cyan, gray, brightblack, brightred, brightgreen, brightyellow, brightblue, brightmagenta, brightcyan, white, default - Alternatively, numeric color codes can be used. For example: #ff0000 = red - Default colors: - JELLO_COLORS=blue,brightblack,magenta,green + JC_COLORS=blue,brightblack,magenta,green or - JELLO_COLORS=default,default,default,default + JC_COLORS=default,default,default,default """ - env_colors = os.getenv('JELLO_COLORS') + env_colors = os.getenv('JC_COLORS') input_error = False if env_colors: @@ -134,22 +120,12 @@ def set_env_colors(keyname_color, keyword_color, number_color, string_color): if input_error: color_list = ['default', 'default', 'default', 'default'] - # Try the color set in the JELLO_COLORS env variable first. If it is set to default, then fall back to default colors - class JcStyle(Style): - styles = { - Name.Tag: f'bold ansi{color_list[0]}' if not color_list[0] == 'default' else f'bold ansiblue' # key names - Keyword: f'bold ansi{color_list[1]}' if not color_list[1] == 'default' else f'ansibrightblack' # true, false, null - Number: f'bold ansi{color_list[2]}' if not color_list[2] == 'default' else f'magenta' # numbers - String: f'bold ansi{color_list[3]}' if not color_list[3] == 'default' else f'green' # strings - } - - JelloTheme.colors = { - 'key_name': color_map[color_list[0]] if not color_list[0] == 'default' else color_map[keyname_color] if keyname_color else color_map['blue'], - 'keyword': color_map[color_list[1]] if not color_list[1] == 'default' else color_map[keyword_color] if keyword_color else color_map['brightblack'], - 'number': color_map[color_list[2]] if not color_list[2] == 'default' else color_map[number_color] if number_color else color_map['magenta'], - 'string': color_map[color_list[3]] if not color_list[3] == 'default' else color_map[string_color] if string_color else color_map['green'], - 'array_id': color_map[color_list[4]] if not color_list[4] == 'default' else color_map[arrayid_color] if arrayid_color else color_map['red'], - 'array_bracket': color_map[color_list[5]] if not color_list[5] == 'default' else color_map[arraybracket_color] if arraybracket_color else color_map['magenta'] + # Try the color set in the JC_COLORS env variable first. If it is set to default, then fall back to default colors + return { + Name.Tag: f'bold ansi{color_list[0]}' if not color_list[0] == 'default' else f'bold ansiblue', # key names + Keyword: f'ansi{color_list[1]}' if not color_list[1] == 'default' else f'ansibrightblack', # true, false, null + Number: f'ansi{color_list[2]}' if not color_list[2] == 'default' else f'ansimagenta', # numbers + String: f'ansi{color_list[3]}' if not color_list[3] == 'default' else f'ansigreen' # strings } @@ -269,6 +245,11 @@ def helptext(message): def json_out(data, pretty=False, mono=False, piped_out=False): + # set colors + class JcStyle(Style): + styles = set_env_colors() + + if not mono and not piped_out: if pretty: print(highlight(json.dumps(data, indent=2), JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1]) From 3d6a76024de0f9dcf91a4ab49cabe461074114fb Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 12 Apr 2020 13:10:57 -0700 Subject: [PATCH 3/5] update with JC_COLORS info --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 2f09f52f..c0729645 100755 --- a/README.md +++ b/README.md @@ -144,6 +144,24 @@ The JSON output can be compact (default) or pretty formatted with the `-p` optio - `-q` quiet mode. Suppresses warning messages - `-r` raw output. Provides a more literal JSON output with all values as text and no additional sematic processing +### Setting Custom Colors via Environment Variable +You can specify custom colors via the `JC_COLORS` environment variable. + +The `JC_COLORS` environment variable takes four comma separated string values in the following format: +``` +JELLO_COLORS=,,, +``` +Where colors are: `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `gray`, `brightblack`, `brightred`, `brightgreen`, `brightyellow`, `brightblue`, `brightmagenta`, `brightcyan`, `white`, or `default` + +For example, to set to the default colors: +``` +JC_COLORS=blue,brightblack,magenta,green +``` +or +``` +JC_COLORS=default,default,default,default +``` + ## Contributions Feel free to add/improve code or parsers! You can use the [`jc/parsers/foo.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/foo.py) parser as a template and submit your parser with a pull request. From 7454b53e395e9122c00d4cf06b1c2af109913d7a Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 12 Apr 2020 13:13:28 -0700 Subject: [PATCH 4/5] formatting --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index c0729645..49102b24 100755 --- a/README.md +++ b/README.md @@ -145,9 +145,7 @@ The JSON output can be compact (default) or pretty formatted with the `-p` optio - `-r` raw output. Provides a more literal JSON output with all values as text and no additional sematic processing ### Setting Custom Colors via Environment Variable -You can specify custom colors via the `JC_COLORS` environment variable. - -The `JC_COLORS` environment variable takes four comma separated string values in the following format: +You can specify custom colors via the `JC_COLORS` environment variable. The `JC_COLORS` environment variable takes four comma separated string values in the following format: ``` JELLO_COLORS=,,, ``` From 07b8d9e0c0d723d93e6c652f5b6156c467c451c7 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 12 Apr 2020 13:18:28 -0700 Subject: [PATCH 5/5] version bump --- changelog.txt | 3 +++ jc/cli.py | 2 +- setup.py | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/changelog.txt b/changelog.txt index 9f55b05d..16b8b1a3 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,8 @@ jc changelog +20200412 v1.10.4 +- Add color customization via JC_COLORS env variable + 20200409 v1.10.3 - Fix break on pipe error diff --git a/jc/cli.py b/jc/cli.py index cf48f898..3feee9bc 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -18,7 +18,7 @@ import jc.utils class info(): - version = '1.10.3' + version = '1.10.4' description = 'jc cli output JSON conversion tool' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' diff --git a/setup.py b/setup.py index b63691f9..76759dda 100755 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open('README.md', 'r') as f: setuptools.setup( name='jc', - version='1.10.3', + version='1.10.4', author='Kelly Brazil', author_email='kellyjonbrazil@gmail.com', description='This tool serializes the output of popular command line tools and filetypes to structured JSON output.',