|  | @@ -17,6 +17,7 @@ _is_py3 = sys.hexversion >= 0x03000000
 | 
	
		
			
			| 17 | 17 |  
 | 
	
		
			
			| 18 | 18 |  import os
 | 
	
		
			
			| 19 | 19 |  import re
 | 
	
		
			
			|  | 20 | +import errno
 | 
	
		
			
			| 20 | 21 |  import difflib
 | 
	
		
			
			| 21 | 22 |  
 | 
	
		
			
			| 22 | 23 |  
 | 
	
	
		
			
			|  | @@ -451,6 +452,21 @@ class DiffMarkup(object):
 | 
	
		
			
			| 451 | 452 |                  yield line
 | 
	
		
			
			| 452 | 453 |  
 | 
	
		
			
			| 453 | 454 |  
 | 
	
		
			
			|  | 455 | +def markup_to_pager(stream):
 | 
	
		
			
			|  | 456 | +    markup = DiffMarkup(stream)
 | 
	
		
			
			|  | 457 | +    color_diff = markup.markup(side_by_side=opts.side_by_side,
 | 
	
		
			
			|  | 458 | +            width=opts.width)
 | 
	
		
			
			|  | 459 | +
 | 
	
		
			
			|  | 460 | +    # args stolen fron git source: github.com/git/git/blob/master/pager.c
 | 
	
		
			
			|  | 461 | +    pager = subprocess.Popen(['less', '-FRSXK'],
 | 
	
		
			
			|  | 462 | +            stdin=subprocess.PIPE, stdout=sys.stdout)
 | 
	
		
			
			|  | 463 | +    for line in color_diff:
 | 
	
		
			
			|  | 464 | +        pager.stdin.write(line.encode('utf-8'))
 | 
	
		
			
			|  | 465 | +
 | 
	
		
			
			|  | 466 | +    pager.stdin.close()
 | 
	
		
			
			|  | 467 | +    pager.wait()
 | 
	
		
			
			|  | 468 | +
 | 
	
		
			
			|  | 469 | +
 | 
	
		
			
			| 454 | 470 |  if __name__ == '__main__':
 | 
	
		
			
			| 455 | 471 |      import optparse
 | 
	
		
			
			| 456 | 472 |      import subprocess
 | 
	
	
		
			
			|  | @@ -491,18 +507,12 @@ if __name__ == '__main__':
 | 
	
		
			
			| 491 | 507 |          diff_hdl.close()
 | 
	
		
			
			| 492 | 508 |  
 | 
	
		
			
			| 493 | 509 |      if sys.stdout.isatty():
 | 
	
		
			
			| 494 |  | -        markup = DiffMarkup(stream)
 | 
	
		
			
			| 495 |  | -        color_diff = markup.markup(side_by_side=opts.side_by_side,
 | 
	
		
			
			| 496 |  | -                width=opts.width)
 | 
	
		
			
			| 497 |  | -
 | 
	
		
			
			| 498 |  | -        # args stolen fron git source: github.com/git/git/blob/master/pager.c
 | 
	
		
			
			| 499 |  | -        pager = subprocess.Popen(['less', '-FRSXK'],
 | 
	
		
			
			| 500 |  | -                stdin=subprocess.PIPE, stdout=sys.stdout)
 | 
	
		
			
			| 501 |  | -        for line in color_diff:
 | 
	
		
			
			| 502 |  | -            pager.stdin.write(line.encode('utf-8'))
 | 
	
		
			
			| 503 |  | -
 | 
	
		
			
			| 504 |  | -        pager.stdin.close()
 | 
	
		
			
			| 505 |  | -        pager.wait()
 | 
	
		
			
			|  | 510 | +        try:
 | 
	
		
			
			|  | 511 | +            markup_to_pager(stream)
 | 
	
		
			
			|  | 512 | +        except IOError:
 | 
	
		
			
			|  | 513 | +            e = sys.exc_info()[1]
 | 
	
		
			
			|  | 514 | +            if e.errno == errno.EPIPE:
 | 
	
		
			
			|  | 515 | +                pass
 | 
	
		
			
			| 506 | 516 |      else:
 | 
	
		
			
			| 507 | 517 |          # pipe out stream untouched to make sure it is still a patch
 | 
	
		
			
			| 508 | 518 |          sys.stdout.write(''.join(stream))
 |