-
Notifications
You must be signed in to change notification settings - Fork 10
/
name2color.py
executable file
·146 lines (121 loc) · 3.7 KB
/
name2color.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
import argparse
import os
import sys
import errno
import hashlib
import pickle
def warn(msg):
sys.stderr.write(msg + '\n')
def error(msg):
print("ColorTag: {}\n".format(msg))
# 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('session', type=str, help='session name')
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')
parser.add_argument('--show-state', action='store_true')
args = parser.parse_args()
xdg_config_home_path = os.environ.get("XDG_CONFIG_HOME")
tmux_plugin_manager_path = os.environ.get("TMUX_PLUGIN_MANAGER_PATH") or ""
if xdg_config_home_path is None:
xdg_config_home_path = ""
else:
xdg_config_home_path = os.path.join(xdg_config_home_path, "tmux/plugins")
try_plugin_paths = [os.path.expanduser(path) for path in [
xdg_config_home_path,
tmux_plugin_manager_path,
]]
tmux_plugins_path = None
for plugin_path in try_plugin_paths:
if os.path.exists(plugin_path):
tmux_plugins_path = plugin_path
break
if tmux_plugins_path is None:
tmux_plugins_path = os.path.expanduser("~/.tmux/plugins")
state_prefix = os.path.join(tmux_plugins_path, "tmux-colortag/state")
state = {}
changed = True
try:
os.makedirs(state_prefix)
except OSError as e:
if e.errno != errno.EEXIST:
raise
saved_state = os.path.join(
state_prefix,
"{}.state".format(args.session))
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.show_state:
print("ColorTag: manual coloring for session \"{}\"".format(args.session))
for (k, v) in state.items():
print("{} => colour{}".format(k, v))
sys.exit(0)
if args.clear_idx:
try:
del state[args.idx]
except Exception:
pass
changed = True
if args.clear_name:
try:
del state[args.name]
except Exception:
pass
changed = True
if not (args.color_idx is None):
if args.color_idx < 0 or args.color_idx >= 256:
error("invalid color code")
sys.exit(0)
state[args.idx] = args.color_idx
changed = True
if not (args.color_name is None):
if args.color_name < 0 or args.color_name >= 256:
error("invalid color code")
sys.exit(0)
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))