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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
#!/usr/bin/env python
import argparse
import os
import sys
import hashlib
import pickle
def warn(msg):
sys.stderr.write(msg + '\n')
def error(msg):
print(msg + '\n')
saved_state = os.path.expanduser("~/.tmux-colortag.state")
# add your favorite color code (256) here as candidates
colors = [
63, 64, 65, 68, 103, 107,
166, 168, 172, 208, 203,
160, 161, 167, 137, 131
]
parser = argparse.ArgumentParser(description='Maps tmux window tags to colors.')
parser.add_argument('idx', type=int, help='index of the window')
parser.add_argument('name', type=str, help='name of the window')
parser.add_argument('--color-idx', type=int, help='manually change the color mapping for an index')
parser.add_argument('--color-name', type=int, help='manually change the color mapping for a name')
parser.add_argument('--clear-idx', action='store_true')
parser.add_argument('--clear-name', action='store_true')
parser.add_argument('--clear', action='store_true')
args = parser.parse_args()
state = {}
changed = True
try:
with open(saved_state, "rb") as f:
_state = pickle.load(f)
if type(_state) is dict:
state = _state
changed = False
except Exception:
pass
if args.clear:
state = {}
changed = True
if len(args.name) > 100:
error("invalid name")
if args.idx < 0 or args.idx >= 1024:
error("invalid idx")
if args.clear_idx:
try:
del state[args.idx]
changed = True
except Exception:
pass
if args.clear_name:
try:
del state[args.name]
changed = True
except Exception:
pass
if not (args.color_idx is None):
if args.color_idx < 0 and args.color_idx >= 256:
error("invalid color code")
state[args.idx] = args.color_idx
changed = True
if not (args.color_name is None):
if args.color_name < 0 and args.color_name >= 256:
error("invalid color code")
state[args.name] = args.color_name
changed = True
if changed:
warn("wrote to the state")
try:
with open(saved_state, "wb") as f:
pickle.dump(state, f)
except Exception:
warn("failed to dump file")
sys.exit(0)
c = state.get(args.idx, None)
if c is None:
c = state.get(args.name, None)
if c is None:
h = hashlib.sha1(args.name.encode('utf-8')).digest()
if sys.version_info.major == 3:
hn = int.from_bytes(h, byteorder='big')
else:
hn = 0
for b in bytearray(h):
hn = hn * 256 + int(b)
c = colors[hn % len(colors)]
print("colour{}".format(c))
|