diff options
author | Georg Brandl <georg@python.org> | 2014-10-08 09:00:03 +0200 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2014-10-08 09:00:03 +0200 |
commit | ac3a01c3b86d36b4dc88a11dfe2dfe042ea1c208 (patch) | |
tree | bf18e7742a74e7be8d661b31d06832f080d8c360 /pygments/cmdline.py | |
parent | ab509e4ea2a8bd3c7e8e355b0e83b3e2de9f7a01 (diff) | |
download | pygments-ac3a01c3b86d36b4dc88a11dfe2dfe042ea1c208.tar.gz |
Refine streaming mode a bit:
* flush output file after writing a line (important in Py3)
* disallow -s and input file name
* disallow -s without -l
* use binary stdin on Py3
* add to usage doc.
Diffstat (limited to 'pygments/cmdline.py')
-rw-r--r-- | pygments/cmdline.py | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/pygments/cmdline.py b/pygments/cmdline.py index c77f0a32..907c51f0 100644 --- a/pygments/cmdline.py +++ b/pygments/cmdline.py @@ -30,7 +30,7 @@ from pygments.styles import get_all_styles, get_style_by_name USAGE = """\ Usage: %s [-l <lexer> | -g] [-F <filter>[:<options>]] [-f <formatter>] - [-O <options>] [-P <option=value>] [-o <outfile>] [<infile>] + [-O <options>] [-P <option=value>] [-s] [-o <outfile>] [<infile>] %s -S <style> -f <formatter> [-a <arg>] [-O <options>] [-P <option=value>] %s -L [<which> ...] @@ -42,6 +42,10 @@ Highlight the input file and write the result to <outfile>. If no input file is given, use stdin, if -o is not given, use stdout. +If -s is passed, lexing will be done in "streaming" mode, reading and +highlighting one line at a time. This will only work properly with +lexers that have no constructs spanning multiple lines! + <lexer> is a lexer name (query all lexer names with -L). If -l is not given, the lexer is guessed from the extension of the input file name (this obviously doesn't work if the input is stdin). If -g is passed, @@ -380,10 +384,10 @@ def main(args=sys.argv): print(usage, file=sys.stderr) return 2 - #if '-s' in opts: - #print >>sys.stderr, 'Error: -s option not usable when input ' + \ - # 'file specified' - #return 1 + if '-s' in opts: + print('Error: -s option not usable when input file specified', + file=sys.stderr) + return 1 infn = args[0] try: @@ -457,13 +461,25 @@ def main(args=sys.argv): # process whole input as per normal... highlight(code, lexer, fmter, outfile) else: + if not lexer: + print('Error: when using -s a lexer has to be selected with -l', + file=sys.stderr) + return 1 # line by line processing of stdin (eg: for 'tail -f')... try: while 1: - line = sys.stdin.readline() + if sys.version_info > (3,): + # Python 3: we have to use .buffer to get a binary stream + line = sys.stdin.buffer.readline() + else: + line = sys.stdin.readline() if not line: break + if not inencoding: + line = guess_decode_from_terminal(line, sys.stdin)[0] highlight(line, lexer, fmter, outfile) + if hasattr(outfile, 'flush'): + outfile.flush() except KeyboardInterrupt: return 0 |