-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree.py
63 lines (46 loc) · 1.39 KB
/
tree.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#! /usr/bin/env python
import sys
from functools import reduce
class ANSIColor:
DEFAULT = "\033[0m"
GREEN = "\033[32m"
RED = "\033[31m"
MAGENTA = "\033[35m"
YELLOW = "\033[33m"
def git_status_to_color(status):
return {
" M": ANSIColor.RED,
"M ": ANSIColor.GREEN,
"??": ANSIColor.RED,
}[status]
def git_status_to_letter(status):
return {
" M": "M",
"M ": "A",
"??": "U",
}[status]
def main():
lines = [i for i in sys.stdin.read().splitlines() if not i.endswith("/")]
def lst_idx(la, lb):
for i, j in enumerate(la):
if i >= len(lb) or j != lb[i]:
return i
return 0
last_path = []
for line in lines:
status = line[:2]
path = line[3:].split("/")
idx = lst_idx(last_path, path)
for i in range(idx, len(path)):
letter = git_status_to_letter(status)
indent = reduce(lambda acc, x: acc + len(x), path[:i], 0)
padding = "{:<%d}" % (indent + len(letter) + 1)
print("%s%s%s%s %s" % (padding.format(""),
git_status_to_color(status),
letter,
ANSIColor.DEFAULT,
path[i]))
last_path = path
return 0
if __name__ == "__main__":
sys.exit(main())