Skip to content

Instantly share code, notes, and snippets.

@tav
Last active December 26, 2015 16:09
Show Gist options
  • Save tav/7177592 to your computer and use it in GitHub Desktop.
Save tav/7177592 to your computer and use it in GitHub Desktop.
Syntax highlight files without extensions using source-highlight.
#! /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