mirror of
https://github.com/Erreur32/cheat.git
synced 2024-12-22 21:52:12 +01:00
6e36b13df7
add color + other stuff
77 lines
2 KiB
Python
77 lines
2 KiB
Python
from __future__ import print_function
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
|
|
|
|
def colorize(sheet_content):
|
|
""" Colorizes cheatsheet content if so configured """
|
|
|
|
# only colorize if so configured
|
|
if not 'CHEATCOLORS' in os.environ:
|
|
return sheet_content
|
|
|
|
try:
|
|
from pygments import highlight
|
|
from pygments.lexers import get_lexer_by_name
|
|
from pygments.formatters import TerminalFormatter
|
|
|
|
# if pygments can't load, just return the uncolorized text
|
|
except ImportError:
|
|
return sheet_content
|
|
|
|
# GMFTBY add
|
|
if sheet_content == '' :
|
|
# can not find the content about the term
|
|
die('Can not find the content !')
|
|
return
|
|
|
|
first_line = sheet_content.splitlines()[0]
|
|
lexer = get_lexer_by_name('bash')
|
|
if first_line.startswith('```'):
|
|
sheet_content = '\n'.join(sheet_content.split('\n')[1:-2])
|
|
try:
|
|
lexer = get_lexer_by_name(first_line[3:])
|
|
except Exception:
|
|
pass
|
|
|
|
return highlight(sheet_content, lexer, TerminalFormatter())
|
|
|
|
|
|
def die(message):
|
|
""" Prints a message to stderr and then terminates """
|
|
warn(message)
|
|
exit(1)
|
|
|
|
|
|
def editor():
|
|
""" Determines the user's preferred editor """
|
|
|
|
# determine which editor to use
|
|
editor = os.environ.get('CHEAT_EDITOR') \
|
|
or os.environ.get('VISUAL') \
|
|
or os.environ.get('EDITOR') \
|
|
or False
|
|
|
|
# assert that the editor is set
|
|
if editor == False:
|
|
die(
|
|
'You must set a CHEAT_EDITOR, VISUAL, or EDITOR environment '
|
|
'variable in order to create/edit a cheatsheet.'
|
|
)
|
|
|
|
return editor
|
|
|
|
|
|
def open_with_editor(filepath):
|
|
""" Open `filepath` using the EDITOR specified by the environment variables """
|
|
editor_cmd = editor().split()
|
|
try:
|
|
subprocess.call(editor_cmd + [filepath])
|
|
except OSError:
|
|
die('Could not launch ' + editor())
|
|
|
|
|
|
def warn(message):
|
|
""" Prints a message to stderr """
|
|
print((message), file=sys.stderr)
|