summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--patternutils/commands/patterncfg.py23
-rw-r--r--requirements.txt1
-rw-r--r--setup.py1
3 files changed, 22 insertions, 3 deletions
diff --git a/patternutils/commands/patterncfg.py b/patternutils/commands/patterncfg.py
index 3cb7067..07d206e 100644
--- a/patternutils/commands/patterncfg.py
+++ b/patternutils/commands/patterncfg.py
@@ -5,6 +5,16 @@ from typing import Optional
import click
+try:
+ from pygments import highlight
+ from pygments.formatters import TerminalFormatter
+ from pygments.lexers import Python3Lexer, DjangoLexer
+except ImportError:
+ has_pygments = False
+else:
+ has_pygments = True
+
+
from .. import config
@@ -76,9 +86,10 @@ def remove(command: Optional[str], name: str) -> None:
@patterncfg.command(name='view', help='view template file')
@click.option('-c', '--command', default=None, type=click.Choice(CURRENT_SUPPORTED_PROGRAMS),
help='select the command from which to view the file')
+@click.option('--color/--no-color', is_flag=True, default=has_pygments,
+ help='Colorize output using pygments')
@click.argument('name')
-def view(name: str, command: str) -> None:
-
+def view(name: str, command: str, color: bool) -> None:
files = config.find_templates(name, command)
if not files:
@@ -99,4 +110,10 @@ def view(name: str, command: str) -> None:
click.secho('File not found!', fg='red', err=True)
raise SystemExit
else:
- click.echo_via_pager(text)
+ if color:
+ if filename.endswith('jinja2'):
+ lexer = DjangoLexer
+ else:
+ lexer = Python3Lexer
+ text = highlight(text, lexer(), TerminalFormatter())
+ click.echo(text[:-1])
diff --git a/requirements.txt b/requirements.txt
index d75dc4d..01156a3 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,4 @@
Click==7.0
Jinja2==2.10.3
MarkupSafe==1.1.1
+Pygments==2.4.2
diff --git a/setup.py b/setup.py
index fda381d..71f1e7a 100644
--- a/setup.py
+++ b/setup.py
@@ -27,6 +27,7 @@ REQUIRED = [
OPTIONAL = {
'jinja2': ['Jinja2==2.10.1'],
+ 'pygments': ['Pygments==2.4.2'],
}
setup(