Skip to content

Instantly share code, notes, and snippets.

@kmwhite
Last active December 28, 2015 19:59
Show Gist options
  • Save kmwhite/7553865 to your computer and use it in GitHub Desktop.
Save kmwhite/7553865 to your computer and use it in GitHub Desktop.
Colorize `git-stash list` output
#!/usr/bin/env python
from __future__ import print_function
import re
import subprocess
import sys
GIT_STASH_COMMAND=['git', 'stash', 'list']
GIT_STASH_MATCH='(?P<id>stash@\{\d+\}:)\s+(?P<source>(?:WIP\s+o|O)n.[-0-9a-zA-Z_]+:)\s+(?P<desc>\w+\s.+)'
GREEN = '\033[32m'
RED = '\033[31m'
ENDC = '\033[0m'
def exec_stash():
return subprocess.Popen(GIT_STASH_COMMAND,
stdout=subprocess.PIPE).communicate()[0]
for line in exec_stash().split('\n'):
match = re.match(GIT_STASH_MATCH, line)
if sys.stdout.isatty() and match is not None:
print("{0}{1} ".format(RED, match.group('id')), end='')
print("{0}{1} ".format(GREEN, match.group('source')), end='')
print("{0}{1}".format(ENDC, match.group('desc')))
else:
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment