Advertisement
dyeske

diff.py

Oct 2nd, 2023
1,246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """ This takes unified diff output and adds colors """
  3.  
  4. from __future__ import print_function
  5. import re
  6. import sys
  7.  
  8. def main():
  9.     """ main routine """
  10.     RED = '\033[0;31m'
  11.     GREEN = '\033[0;32m'
  12.     YELLOW = '\033[0;33m'
  13.     BLUE = '\033[0;34m'
  14.     MAGENTA = '\033[0;35m'
  15.     ENDCOLOR = '\033[0m'
  16.     added = 0
  17.     deleted = 0
  18.  
  19.     try:
  20.         for line in sys.stdin:
  21.             if re.search('(^Index: .*)', line):
  22.                 line = "%s%s%s" % (BLUE, line, ENDCOLOR)
  23.             elif re.search('(^\+\+\+ .*)', line):
  24.                 line = "%s%s%s" % (MAGENTA, line, ENDCOLOR)
  25.             elif re.search('(^\+.*)', line):
  26.                 line = "%s%s%s" % (GREEN, line, ENDCOLOR)
  27.                 added = added + 1
  28.             elif re.search('(^\-\-\- .*)', line):
  29.                 line = "%s%s%s" % (YELLOW, line, ENDCOLOR)
  30.             elif re.search('(^\-.*)', line):
  31.                 line = "%s%s%s" % (RED, line, ENDCOLOR)
  32.                 deleted = deleted + 1
  33.             else:
  34.                 pass
  35.             sys.stdout.write(line)
  36.         print('added ' + str(added) + ' deleted ' + str(deleted))
  37.     except:
  38.         print('exception')
  39.  
  40. if __name__ == '__main__':
  41.     main()
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement