1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-08-08 22:36:48 +02:00

JC_COLORS working

This commit is contained in:
Kelly Brazil
2020-04-12 13:03:09 -07:00
parent 4a22e27d6a
commit 421b980957

View File

@@ -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=<keyname_color>,<keyword_color>,<number_color>,<string_color>
JC_COLORS=<keyname_color>,<keyword_color>,<number_color>,<string_color>
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])