mirror of https://github.com/cheat/cheat.git
CheatSheets helpers added to the CheatSheets class, including pretty_print. display_sheet method also added which cleans up the argparse logic.
This commit is contained in:
parent
e651de5dde
commit
2683ebe99a
112
cheat
112
cheat
|
@ -42,25 +42,6 @@ if os.name == 'posix' and 'CHEATCOLORS' in os.environ:
|
|||
pass
|
||||
|
||||
|
||||
def pretty_print(filename):
|
||||
"""Applies syntax highlighting to a cheatsheet and writes it to stdout"""
|
||||
try:
|
||||
if os.path.splitext(filename)[1]:
|
||||
lexer = get_lexer_for_filename(filename)
|
||||
else:
|
||||
# shell is a sensible default when there is no extension
|
||||
lexer = get_lexer_for_filename(filename + '.sh')
|
||||
|
||||
except ClassNotFound:
|
||||
lexer = TextLexer()
|
||||
|
||||
with open(filename) as istream:
|
||||
code = istream.read()
|
||||
|
||||
fmt = TerminalFormatter()
|
||||
highlight(code, lexer, fmt, sys.stdout)
|
||||
|
||||
|
||||
class CheatSheets(object):
|
||||
|
||||
dirs = None
|
||||
|
@ -77,7 +58,7 @@ class CheatSheets(object):
|
|||
|
||||
def __cheat_directories(self):
|
||||
"""Assembles a list of directories containing cheatsheets."""
|
||||
default_directories = [DEFAULT_CHEAT_DIR]
|
||||
default_directories = [DEFAULT_CHEAT_DIR, ]
|
||||
try:
|
||||
import cheatsheets
|
||||
default_directories.append(cheatsheets.cheat_dir)
|
||||
|
@ -104,7 +85,7 @@ class CheatSheets(object):
|
|||
return cheats
|
||||
|
||||
def edit(self, cheat):
|
||||
"""Creates or edits a cheatsheet"""
|
||||
"""Creates or edits a cheatsheet."""
|
||||
|
||||
# Assert that the EDITOR environment variable is set and that at least 3
|
||||
# arguments have been given
|
||||
|
@ -144,42 +125,50 @@ class CheatSheets(object):
|
|||
exit(1)
|
||||
|
||||
def list(self):
|
||||
"""Lists the cheatsheets that are currently available"""
|
||||
"""Lists the cheatsheets that are currently available."""
|
||||
max_command = max([len(x) for x in self.sheets.keys()]) + 3
|
||||
return ('\n'.join(sorted(['%s [%s]' % (key.ljust(max_command), value)
|
||||
for key, value in self.sheets.items()])))
|
||||
|
||||
def display_sheet(self, sheet):
|
||||
"""Display a single sheet."""
|
||||
filepath = os.path.join(self.sheets[sheet], sheet)
|
||||
if self.is_vim_crypted(filepath):
|
||||
self.vim_view(filepath)
|
||||
elif USE_PYGMENTS:
|
||||
self.pretty_print(filepath)
|
||||
else:
|
||||
with open(filepath) as istream:
|
||||
for l in istream:
|
||||
sys.stdout.write(l)
|
||||
|
||||
# Custom action for argparse
|
||||
class ListDirectories(argparse.Action):
|
||||
"""List cheat directories and exit"""
|
||||
def __call__(self, parser, namespace, values, option_string=None):
|
||||
print('\n'.join(sheets.dirs))
|
||||
parser.exit()
|
||||
def pretty_print(self, filepath):
|
||||
"""Applies syntax highlighting to a cheatsheet and writes it to
|
||||
stdout."""
|
||||
try:
|
||||
if os.path.splitext(filepath)[1]:
|
||||
lexer = get_lexer_for_filename(filepath)
|
||||
else:
|
||||
# shell is a sensible default when there is no extension
|
||||
lexer = get_lexer_for_filename(filepath + '.sh')
|
||||
|
||||
except ClassNotFound:
|
||||
lexer = TextLexer()
|
||||
|
||||
class ListCheatsheets(argparse.Action):
|
||||
"""List cheatsheets and exit"""
|
||||
def __call__(self, parser, namespace, values, option_string=None):
|
||||
print sheets.list()
|
||||
parser.exit()
|
||||
with open(filepath) as istream:
|
||||
code = istream.read()
|
||||
|
||||
fmt = TerminalFormatter()
|
||||
highlight(code, lexer, fmt, sys.stdout)
|
||||
|
||||
class EditSheet(argparse.Action):
|
||||
"""If the user wants to edit a cheatsheet"""
|
||||
def __call__(self, parser, namespace, values, option_string=None):
|
||||
sheets.edit(values[0])
|
||||
parser.exit()
|
||||
|
||||
|
||||
def is_vim_crypted(filepath):
|
||||
"""Returns True if the canonical file path is a Vim Encrypted file."""
|
||||
def is_vim_crypted(self, filepath):
|
||||
"""Helper method that returns True if the canonical file path is a Vim
|
||||
Encrypted file."""
|
||||
canonical_path = os.path.realpath(filepath)
|
||||
return 'vim encrypted' in magic.from_file(canonical_path).lower()
|
||||
|
||||
|
||||
def vim_viewer(filepath):
|
||||
"""Use Vim to read an Vim Encrypted cheat file."""
|
||||
def vim_view(self, filepath):
|
||||
"""Use Vim to read an Vim Encrypted file."""
|
||||
canonical_path = os.path.realpath(filepath)
|
||||
env_cmd = '/usr/bin/env'
|
||||
if not os.path.isfile(env_cmd):
|
||||
|
@ -193,6 +182,28 @@ def vim_viewer(filepath):
|
|||
exit(1)
|
||||
|
||||
|
||||
# Custom action for argparse
|
||||
class ListDirectories(argparse.Action):
|
||||
"""List cheat directories and exit."""
|
||||
def __call__(self, parser, namespace, values, option_string=None):
|
||||
print('\n'.join(sheets.dirs))
|
||||
parser.exit()
|
||||
|
||||
|
||||
class ListCheatsheets(argparse.Action):
|
||||
"""List cheatsheets and exit."""
|
||||
def __call__(self, parser, namespace, values, option_string=None):
|
||||
print sheets.list()
|
||||
parser.exit()
|
||||
|
||||
|
||||
class EditSheet(argparse.Action):
|
||||
"""If the user wants to edit a cheatsheet."""
|
||||
def __call__(self, parser, namespace, values, option_string=None):
|
||||
sheets.edit(values[0])
|
||||
parser.exit()
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
global sheets
|
||||
|
@ -243,16 +254,7 @@ def main():
|
|||
if not sheet or sheet in ['help', 'cheat']:
|
||||
parser.print_help()
|
||||
elif sheet in sheets.sheets:
|
||||
filename = os.path.join(sheets.sheets[sheet], sheet)
|
||||
if is_vim_crypted(filename):
|
||||
vim_viewer(filename)
|
||||
elif USE_PYGMENTS:
|
||||
pretty_print(filename)
|
||||
else:
|
||||
with open(filename) as istream:
|
||||
for l in istream:
|
||||
sys.stdout.write(l)
|
||||
|
||||
sheets.display_sheet(sheet)
|
||||
# if it does not, say so
|
||||
else:
|
||||
print >> sys.stderr, ('No cheatsheet found for %s.' % sheet)
|
||||
|
|
Loading…
Reference in New Issue