Introduced CHEAT_HIGHLIGHT

Introduced CHEAT_HIGHLIGHT environment variable to de-couple search-term
highlighting from syntax highlighting.
This commit is contained in:
Chris Lane 2019-01-11 16:13:38 -05:00
parent ba9051e3cd
commit 730c488854
2 changed files with 4 additions and 9 deletions

View File

@ -86,12 +86,7 @@ def search(term):
match = '' match = ''
for line in open(cheatsheet[1]): for line in open(cheatsheet[1]):
if term in line: if term in line:
match += ' ' + highlight(term, line)
# highlight the search term if CHEATCOLORS equals true
if 'CHEATCOLORS' in os.environ:
line = highlight(term, line);
match += ' ' + line
if match != '': if match != '':
result += cheatsheet[0] + ":\n" + match + "\n" result += cheatsheet[0] + ":\n" + match + "\n"

View File

@ -6,8 +6,8 @@ import sys
def highlight(needle, haystack): def highlight(needle, haystack):
""" Highlights a search term matched within a line """ """ Highlights a search term matched within a line """
# if colorization is not configured, exit early # if a highlight color is not configured, exit early
if os.environ.get('CHEATCOLORS') != 'true': if not 'CHEAT_HIGHLIGHT' in os.environ:
return haystack return haystack
# otherwise, attempt to import the termcolor library # otherwise, attempt to import the termcolor library
@ -19,7 +19,7 @@ def highlight(needle, haystack):
return haystack return haystack
# if the import succeeds, colorize the needle in haystack # if the import succeeds, colorize the needle in haystack
return haystack.replace(needle, colored(needle, 'blue')); return haystack.replace(needle, colored(needle, os.environ.get('CHEAT_HIGHLIGHT')));
def colorize(sheet_content): def colorize(sheet_content):
""" Colorizes cheatsheet content if so configured """ """ Colorizes cheatsheet content if so configured """