-
Notifications
You must be signed in to change notification settings - Fork 0
/
.lessfilter_pygmentize
executable file
·32 lines (28 loc) · 1.09 KB
/
.lessfilter_pygmentize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python
# from: https://github.com/sashahart/dotfiles/blob/master/.lessfilter
# Approach 1. directly symlink pygmentize as ~/.lessfilter (inflexible)
# Approach 2. ~/.lessfilter is a shell script which calls pygmentize (slow)
# if [[ "$TERM" =~ 256* ]]; then
# pygmentize -f 256 -P style=monokai -O"encoding=utf-8" "$1"
# else
# pygmentize -P style=monokai -O"encoding=utf8" "$1"
# fi
# Approach 3. Cut out the middleman and just use a Python interpreter directly
from sys import stdout, argv
from os import environ
from pygments import highlight
from pygments.lexers import get_lexer_for_filename
from pygments.formatters.terminal256 import Terminal256Formatter
from pygments.formatters.terminal import TerminalFormatter
TERM = environ.get("TERM")
filename = argv[1]
options = {'style': 'monokai', 'encoding': 'utf-8'}
if "256" in TERM:
Formatter = Terminal256Formatter
else:
Formatter = TerminalFormatter
formatter = Formatter(**options)
lexer = get_lexer_for_filename(filename, **options)
with open(filename, 'rb') as f:
code = f.read()
highlight(code, lexer, formatter, stdout)