summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruss <russ@perspexis.com>2013-02-13 10:54:57 -0500
committerruss <russ@perspexis.com>2013-02-13 10:54:57 -0500
commit0219d131aa2b331f04ae34331a92c633b9b6cabf (patch)
tree4dc223d56a75568be23a7db359d81c1cd7779943
parent054c464c70c115f476608a51bb0b4a931d1fa400 (diff)
downloadpygments-0219d131aa2b331f04ae34331a92c633b9b6cabf.tar.gz
added -s option to support line-by-line formatting of stdin stream
-rw-r--r--pygments/cmdline.py35
1 files changed, 32 insertions, 3 deletions
diff --git a/pygments/cmdline.py b/pygments/cmdline.py
index c25204bf..854d567d 100644
--- a/pygments/cmdline.py
+++ b/pygments/cmdline.py
@@ -76,6 +76,12 @@ If no specific lexer can be determined "text" is returned.
The -H option prints detailed help for the object <name> of type <type>,
where <type> is one of "lexer", "formatter" or "filter".
+The -s option processes lines one-by-one until EOF, rather than waiting
+to process the entire file. This only works for processing stdin, and
+is intended for streaming input, such as you get from 'tail -f'.
+This is suitable for scripts to pipe lines to.
+e.g.: "tail -f sql.log | pygmentize -s -l sql"
+
The -h option prints this help.
The -V option prints the package version.
"""
@@ -201,7 +207,7 @@ def main(args=sys.argv):
pass
try:
- popts, args = getopt.getopt(args[1:], "l:f:F:o:O:P:LS:a:N:hVHg")
+ popts, args = getopt.getopt(args[1:], "l:f:F:o:O:P:LS:a:N:hVHgs")
except getopt.GetoptError, err:
print >>sys.stderr, usage
return 2
@@ -360,11 +366,20 @@ def main(args=sys.argv):
print >>sys.stderr, 'Error:', err
return 1
+ #read in the code block...
+ # - end result is that 'code' holds the entire code
+ code = None
+
if args:
if len(args) > 1:
print >>sys.stderr, usage
return 2
+ #if '-s' in opts:
+ #print >>sys.stderr, 'Error: -s option not usable when input ' + \
+ # 'file specified'
+ #return 1
+
infn = args[0]
try:
code = open(infn, 'rb').read()
@@ -388,7 +403,7 @@ def main(args=sys.argv):
print >>sys.stderr, 'Error:', err
return 1
- else:
+ elif '-s' not in opts: #treat stdin as full file (-s support is later)
if '-g' in opts:
code = sys.stdin.read()
try:
@@ -425,7 +440,21 @@ def main(args=sys.argv):
# process filters
for fname, fopts in F_opts:
lexer.add_filter(fname, **fopts)
- highlight(code, lexer, fmter, outfile)
+
+ if '-s' not in opts:
+ #process whole input as per normal...
+ highlight(code, lexer, fmter, outfile)
+ else:
+ # line input processing of stdout (eg: for 'tail -f')...
+ try:
+ while True:
+ line = sys.stdin.readline()
+ if not line:
+ break
+ highlight(line, lexer, fmter, outfile)
+ except KeyboardInterrupt:
+ return 0
+
except Exception, err:
import traceback
info = traceback.format_exception(*sys.exc_info())