mirror of
https://github.com/cheat/cheat.git
synced 2024-11-25 15:31:36 +01:00
fix except case
- when redirect stdout to pipe but not tty, it throw exception. - when have no content, it throw exception. - remove reductant newline at end of file
This commit is contained in:
parent
aa1e12625e
commit
c0fe871b33
@ -34,6 +34,7 @@ Examples:
|
||||
cheat -s ssh
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
# require the dependencies
|
||||
from cheat import sheets, sheet
|
||||
from cheat.utils import colorize
|
||||
@ -50,7 +51,7 @@ if __name__ == '__main__':
|
||||
|
||||
# list cheatsheets
|
||||
elif options['--list']:
|
||||
print(sheets.list())
|
||||
print(sheets.list(), end="")
|
||||
|
||||
# create/edit cheatsheet
|
||||
elif options['--edit']:
|
||||
@ -58,8 +59,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="")
|
||||
|
@ -10,6 +10,9 @@ def colorize(sheet_content):
|
||||
# only colorize if so configured
|
||||
if not 'CHEATCOLORS' in os.environ:
|
||||
return sheet_content
|
||||
# only colorize if stdout is tty
|
||||
if not sys.stdout.isatty():
|
||||
return sheet_content
|
||||
|
||||
try:
|
||||
from pygments import highlight
|
||||
@ -20,7 +23,10 @@ def colorize(sheet_content):
|
||||
except ImportError:
|
||||
return sheet_content
|
||||
|
||||
first_line = sheet_content.splitlines()[0]
|
||||
lines = sheet_content.splitlines()
|
||||
if len(lines) == 0:
|
||||
return ""
|
||||
first_line = lines[0]
|
||||
lexer = get_lexer_by_name('bash')
|
||||
if first_line.startswith('```'):
|
||||
sheet_content = '\n'.join(sheet_content.split('\n')[1:-2])
|
||||
|
Loading…
Reference in New Issue
Block a user