mirror of https://github.com/cheat/cheat.git
Refactored (5)
- Extracted `Colorize` class out of `Util` class. (The latter now only contains static methods.) - Renamed methods in `Colorize` class for improved clarity. - Refactored as necessary to accommodate the changes above.
This commit is contained in:
parent
878d7e7e1b
commit
7c4fc54681
11
bin/cheat
11
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['<keyword>'])), end="")
|
||||
print(colorize.syntax(sheets.search(options['<keyword>'])), end="")
|
||||
|
||||
# print the cheatsheet
|
||||
else:
|
||||
print(utils.colorize(sheet.read(options['<cheatsheet>'])), end="")
|
||||
print(colorize.syntax(sheet.read(options['<cheatsheet>'])), end="")
|
||||
|
|
|
@ -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())
|
|
@ -1,4 +1,5 @@
|
|||
from __future__ import print_function
|
||||
from cheat.utils import Utils
|
||||
import subprocess
|
||||
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
from cheat.utils import Utils
|
||||
import io
|
||||
import os
|
||||
import shutil
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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 """
|
||||
|
|
Loading…
Reference in New Issue