Merge branch 'master' of https://github.com/liuyang1/cheat into liuyang1-master

Resolving merge-conflicts.
This commit is contained in:
Chris Lane 2019-01-11 17:00:39 -05:00
commit 22b64d2d08
2 changed files with 13 additions and 5 deletions

View File

@ -35,6 +35,7 @@ Examples:
"""
# require the dependencies
from __future__ import print_function
from cheat import sheets, sheet
from cheat.utils import colorize
from cheat.utils import die
@ -64,7 +65,7 @@ if __name__ == '__main__':
# list cheatsheets
elif options['--list']:
print(sheets.list())
print(sheets.list(), end="")
# create/edit cheatsheet
elif options['--edit']:
@ -72,8 +73,8 @@ if __name__ == '__main__':
# search among the cheatsheets
elif options['--search']:
print(colorize(sheets.search(options['<keyword>'])))
print(colorize(sheets.search(options['<keyword>'])), end="")
# print the cheatsheet
else:
print(colorize(sheet.read(options['<cheatsheet>'])))
print(colorize(sheet.read(options['<cheatsheet>'])), end="")

View File

@ -21,13 +21,18 @@ def highlight(needle, haystack):
# if the import succeeds, colorize the needle in haystack
return haystack.replace(needle, colored(needle, os.environ.get('CHEAT_HIGHLIGHT')));
def colorize(sheet_content):
""" Colorizes cheatsheet content if so configured """
# if colorization is not configured, exit early
if os.environ.get('CHEATCOLORS') != 'true':
# only colorize if configured to do so, and if stdout is a tty
if os.environ.get('CHEATCOLORS') != 'true' or not sys.stdout.isatty():
return sheet_content
# don't attempt to colorize an empty cheatsheet
if not sheet_content.strip():
return ""
# otherwise, attempt to import the pygments library
try:
from pygments import highlight
@ -41,6 +46,8 @@ def colorize(sheet_content):
# otherwise, attempt to colorize
first_line = sheet_content.splitlines()[0]
lexer = get_lexer_by_name('bash')
# apply syntax-highlighting if the first line is a code-fence
if first_line.startswith('```'):
sheet_content = '\n'.join(sheet_content.split('\n')[1:-2])
try: