diff --git a/bin/cheat b/bin/cheat index 7358e93..a1a5aa6 100755 --- a/bin/cheat +++ b/bin/cheat @@ -36,11 +36,11 @@ Examples: # require the dependencies from __future__ import print_function +from cheat.colorize import Colorize from cheat.configuration import Configuration from cheat.editor import Editor from cheat.sheet import Sheet from cheat.sheets import Sheets -from cheat.utils import Utils from docopt import docopt if __name__ == '__main__': @@ -54,8 +54,9 @@ if __name__ == '__main__': # bootsrap editor = Editor(config) - sheets = Sheets(config) - utils = Utils(config) + colorize = Colorize(config) + + sheets = Sheets(config, colorize) sheet = Sheet(sheets, editor) # list directories @@ -72,8 +73,8 @@ if __name__ == '__main__': # search among the cheatsheets elif options['--search']: - print(utils.colorize(sheets.search(options[''])), end="") + print(colorize.syntax(sheets.search(options[''])), end="") # print the cheatsheet else: - print(utils.colorize(sheet.read(options[''])), end="") + print(colorize.syntax(sheet.read(options[''])), end="") diff --git a/cheat/colorize.py b/cheat/colorize.py new file mode 100644 index 0000000..f9ca6ef --- /dev/null +++ b/cheat/colorize.py @@ -0,0 +1,61 @@ +from __future__ import print_function +import sys + + +class Colorize: + + def __init__(self, config): + self._config = config + + def search(self, needle, haystack): + """ Colorizes search results matched within a line """ + + # if a highlight color is not configured, exit early + if not self._config.cheat_highlight: + return haystack + + # otherwise, attempt to import the termcolor library + try: + from termcolor import colored + + # if the import fails, return uncolored text + except ImportError: + return haystack + + # if the import succeeds, colorize the needle in haystack + return haystack.replace(needle, colored(needle, self._config.cheat_highlight)) + + def syntax(self, sheet_content): + """ Applies syntax highlighting """ + + # only colorize if cheat_colors is true, and stdout is a tty + if (self._config.cheat_colors is False 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 + from pygments.lexers import get_lexer_by_name + from pygments.formatters import TerminalFormatter + + # if the import fails, return uncolored text + except ImportError: + return 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: + lexer = get_lexer_by_name(first_line[3:]) + except Exception: + pass + + return highlight(sheet_content, lexer, TerminalFormatter()) diff --git a/cheat/editor.py b/cheat/editor.py index cb35bd9..0da2fc4 100644 --- a/cheat/editor.py +++ b/cheat/editor.py @@ -1,4 +1,5 @@ from __future__ import print_function +from cheat.utils import Utils import subprocess diff --git a/cheat/sheet.py b/cheat/sheet.py index cf7fbfe..2221084 100644 --- a/cheat/sheet.py +++ b/cheat/sheet.py @@ -1,3 +1,4 @@ +from cheat.utils import Utils import io import os import shutil diff --git a/cheat/sheets.py b/cheat/sheets.py index f45c225..4bbcdde 100644 --- a/cheat/sheets.py +++ b/cheat/sheets.py @@ -6,9 +6,9 @@ from cheat.utils import Utils class Sheets: - def __init__(self, config): + def __init__(self, config, colorize): self._config = config - self._utils = Utils(config) + self._colorize = colorize; def default_path(self): """ Returns the default cheatsheet path """ @@ -95,7 +95,7 @@ class Sheets: match = '' for line in io.open(cheatsheet[1], encoding='utf-8'): if term in line: - match += ' ' + self._utils.highlight(term, line) + match += ' ' + self._colorize.search(term, line) if match != '': result += cheatsheet[0] + ":\n" + match + "\n" diff --git a/cheat/utils.py b/cheat/utils.py index b865756..01bd8a3 100644 --- a/cheat/utils.py +++ b/cheat/utils.py @@ -4,62 +4,6 @@ import sys class Utils: - def __init__(self, config): - self._config = config - - def highlight(self, needle, haystack): - """ Highlights a search term matched within a line """ - - # if a highlight color is not configured, exit early - if not self._config.cheat_highlight: - return haystack - - # otherwise, attempt to import the termcolor library - try: - from termcolor import colored - - # if the import fails, return uncolored text - except ImportError: - return haystack - - # if the import succeeds, colorize the needle in haystack - return haystack.replace(needle, colored(needle, self._config.cheat_highlight)) - - def colorize(self, sheet_content): - """ Colorizes cheatsheet content if so configured """ - - # only colorize if cheat_colors is true, and stdout is a tty - if (self._config.cheat_colors is False 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 - from pygments.lexers import get_lexer_by_name - from pygments.formatters import TerminalFormatter - - # if the import fails, return uncolored text - except ImportError: - return 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: - lexer = get_lexer_by_name(first_line[3:]) - except Exception: - pass - - return highlight(sheet_content, lexer, TerminalFormatter()) - @staticmethod def die(message): """ Prints a message to stderr and then terminates """