Commits

__russ__ committed 8db2dff

added -s option to support line-by-line formatting of stdin stream

Comments (0)

Files changed (1)

pygments/cmdline.py

 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.
 """
             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
             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()
                 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:
         # 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())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.