-
-
Save tav/7177592 to your computer and use it in GitHub Desktop.
Syntax highlight files without extensions using source-highlight.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
"""Usage: lezz <language> [<file>] | |
Alternatively, you can pipe some text via stdin, e.g. | |
$ cat Boltfile | lezz go | |
""" | |
import sys | |
from os import close, remove | |
from shutil import copyfile | |
from tempfile import mkstemp | |
from plumbum import FG | |
from plumbum.cmd import less, source_highlight | |
argv = sys.argv[1:] | |
if sys.stdin.isatty(): | |
if len(argv) != 2: | |
print __doc__ | |
sys.exit(1) | |
lang, filename = argv | |
else: | |
if len(argv) != 1: | |
print __doc__ | |
sys.exit(1) | |
lang = argv[0] | |
filename = None | |
fd, path = mkstemp(prefix='lezz-', suffix='.%s' % lang) | |
try: | |
if filename is None: | |
f = open(path, 'wb') | |
f.write(sys.stdin.read()) | |
f.close() | |
else: | |
copyfile(filename, path) | |
pipe = source_highlight['--failsafe', '-f', 'esc', '--infer-lang', '--style-file=esc.style', '-i', path] | less | |
pipe & FG | |
finally: | |
close(fd) | |
remove(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment