-
Notifications
You must be signed in to change notification settings - Fork 0
/
ignore_moves.py
executable file
·44 lines (35 loc) · 1.28 KB
/
ignore_moves.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/python
import sys
from itertools import *
RED = 31
GREEN = 32
RESET_SEQ = "\033[0m"
COLOR_SEQ = "\033[0;%dm"
stack = []
def inverse(line):
return ('-' if line[0] == '+' else '+') + line[1:].strip()
def reverse_enumerate(l):
for i, x in enumerate(reversed(l)):
yield len(l)-1-i, x
def dumpchanges():
for line in stack:
SEQ = COLOR_SEQ % (GREEN if line.startswith('+') else RED)
print SEQ + line.strip() + RESET_SEQ
stack[:] = []
for line in sys.stdin.readlines():
if not line[1:].strip():
continue # ignore empty lines
if line.startswith(('---', '+++')):
dumpchanges()
print line.strip()
elif line.startswith(('+', '-')):
inverted = inverse(line)
line = line[0] + line[1:].strip()
for i, match in reverse_enumerate(stack):
if inverted == match:
stack.pop(i)
break
else:
stack.append(line)
# finished reading, still have state to be dumped
dumpchanges()