diff --git a/README.md b/README.md index 2f09f52f..49102b24 100755 --- a/README.md +++ b/README.md @@ -144,6 +144,22 @@ 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. 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 a031aadf..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' @@ -80,12 +80,52 @@ 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(): + """ + 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: + + JC_COLORS=,,, + + Where colors are: black, red, green, yellow, blue, magenta, cyan, gray, brightblack, brightred, + brightgreen, brightyellow, brightblue, brightmagenta, brightcyan, white, default + + Default colors: + + JC_COLORS=blue,brightblack,magenta,green + or + JC_COLORS=default,default,default,default + + """ + env_colors = os.getenv('JC_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 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 } @@ -205,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]) 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.',