-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimate_title.py
More file actions
354 lines (285 loc) · 9.91 KB
/
animate_title.py
File metadata and controls
354 lines (285 loc) · 9.91 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/Users/bfeld/.claude/iterm/.venv/bin/python3
"""
Background process that animates iTerm session name with moon phases.
Usage: animate_title.py start|stop|run
"""
import sys
import os
import signal
import subprocess
MOON_PHASES = ['🌑', '🌒', '🌓', '🌔', '🌕', '🌖', '🌗', '🌘']
# All the animation options for the end of title
STARS = ['🌟', '✨', '💫', '⭐']
PLANETS = ['🪐', '🌍', '🌎', '🌏']
ROCKET = ['🚀', '·🚀', '.·🚀', '·.·🚀', ':·.·🚀', '·:·.·🚀', '.·:·.·🚀']
BRAILLE = ['⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷']
HOURGLASS = ['⏳', '⌛']
ARROWS = ['🔄', '🔃']
CIRCLE = ['◐', '◓', '◑', '◒']
CAT = ['🐱', '😺', '😸', '😹']
MUSIC = ['🎵', '🎶', '♪', '♫']
RAINBOW = ['🟥', '🟧', '🟨', '🟩', '🟦', '🟪']
THINKING = ['🧠', '💭', '💡']
ROBOT = ['🤖', '🤖'] # Could add eye states
ENERGY = ['⚡', '🔋', '🪫', '🔋']
WAVE = ['~', '〰️', '~', '〰️']
FIRE = ['🔥', '🔥', '🔥', '🔥']
SNOW = ['❄️', '✳️', '❇️', '✴️']
# Selected animations for end of title
END_ANIMATIONS = [STARS, HOURGLASS, RAINBOW, FIRE]
# Fire burst animation (grows and shrinks) - for Stop event
FIRE_BURST = [
'🔥',
'🔥🔥',
'🔥🔥🔥',
'🔥🔥🔥🔥',
'🔥🔥🔥🔥🔥',
'🔥🔥🔥🔥',
'🔥🔥🔥',
'🔥🔥',
'🔥',
]
def get_ancestor_pids():
"""Walk up the process tree and return all ancestor PIDs."""
pids = set()
pid = os.getpid()
while pid > 1:
pids.add(pid)
try:
result = subprocess.run(
['ps', '-o', 'ppid=', '-p', str(pid)],
capture_output=True, text=True
)
if result.returncode != 0:
break
pid = int(result.stdout.strip())
except (ValueError, subprocess.SubprocessError):
break
return pids
def find_session_id_by_process_tree():
"""Find the iTerm session ID by matching process tree."""
import iterm2
ancestor_pids = get_ancestor_pids()
async def find_session(connection):
app = await iterm2.async_get_app(connection)
for window in app.terminal_windows:
for tab in window.tabs:
for session in tab.sessions:
try:
session_pid = await session.async_get_variable('pid')
if session_pid and int(session_pid) in ancestor_pids:
return session.session_id
except Exception:
continue
return None
try:
return iterm2.run_until_complete(find_session)
except Exception:
return None
def get_session_id():
"""Get the iTerm session ID from env or process tree."""
session_id = os.environ.get('ITERM_SESSION_ID', '')
if session_id and ':' in session_id:
session_id = session_id.split(':', 1)[1]
if not session_id:
session_id = os.environ.get('ANIMATE_TITLE_SESSION_ID', '')
if not session_id:
session_id = find_session_id_by_process_tree()
return session_id
def get_pid_file(session_id=None):
"""Get session-specific PID file."""
if not session_id:
session_id = get_session_id() or 'default'
safe_id = session_id.replace(':', '_').replace('/', '_')
return f'/tmp/iterm_animation_{safe_id}.pid'
def get_title_file(session_id=None):
"""Get session-specific title file."""
if not session_id:
session_id = get_session_id() or 'default'
safe_id = session_id.replace(':', '_').replace('/', '_')
return f'/tmp/iterm_original_title_{safe_id}.txt'
REFRESH_RATE = 0.1 # 100ms (faster spin)
def run_animation():
"""Actually run the animation loop (called in background process)."""
import asyncio
import iterm2
# Get session ID from environment (passed by start())
session_id = os.environ.get('ANIMATE_TITLE_SESSION_ID', '')
title_file = get_title_file(session_id)
async def animate_loop(connection):
app = await iterm2.async_get_app(connection)
# Find the target session
session = app.get_session_by_id(session_id) if session_id else None
if not session:
# Fallback to current session
window = app.current_window
if window and window.current_tab:
session = window.current_tab.current_session
if not session:
return
# Save original session name
original_name = session.name or 'Terminal'
with open(title_file, 'w') as f:
f.write(original_name)
idx = 0
while True:
moon = MOON_PHASES[idx % len(MOON_PHASES)]
# Build end animations
end_section = ''.join(frames[idx % len(frames)] for frames in END_ANIMATIONS)
title = f'{moon} {original_name} {end_section}'
try:
await session.async_set_name(title)
except Exception:
pass
await asyncio.sleep(REFRESH_RATE)
idx += 1
async def main(connection):
await animate_loop(connection)
try:
iterm2.run_until_complete(main, retry=True)
except Exception:
pass
def run_restore():
"""Actually restore the session name (called in background process)."""
import iterm2
session_id = os.environ.get('ANIMATE_TITLE_SESSION_ID', '')
title_file = get_title_file(session_id)
if not os.path.exists(title_file):
return
with open(title_file) as f:
original = f.read().strip()
async def restore(connection):
app = await iterm2.async_get_app(connection)
session = app.get_session_by_id(session_id) if session_id else None
if not session:
window = app.current_window
if window and window.current_tab:
session = window.current_tab.current_session
if session:
await session.async_set_name(original)
try:
iterm2.run_until_complete(restore)
except Exception:
pass
try:
os.remove(title_file)
except FileNotFoundError:
pass
def run_burst():
"""Play fire burst animation on both sides, then restore name."""
import asyncio
import iterm2
session_id = os.environ.get('ANIMATE_TITLE_SESSION_ID', '')
title_file = get_title_file(session_id)
# Read original name
if os.path.exists(title_file):
with open(title_file) as f:
base_title = f.read().strip()
else:
base_title = 'Terminal'
async def burst_animation(connection):
app = await iterm2.async_get_app(connection)
session = app.get_session_by_id(session_id) if session_id else None
if not session:
window = app.current_window
if window and window.current_tab:
session = window.current_tab.current_session
if not session:
return
# Play the burst on both sides
for fire in FIRE_BURST:
title = f'{fire} {base_title} {fire}'
try:
await session.async_set_name(title)
except Exception:
pass
await asyncio.sleep(0.1) # 100ms per frame
# Restore to just the base title
await session.async_set_name(base_title)
try:
iterm2.run_until_complete(burst_animation)
except Exception:
pass
def stop_process(session_id=None):
"""Stop any running animation process."""
pid_file = get_pid_file(session_id)
if os.path.exists(pid_file):
try:
with open(pid_file) as f:
pid = int(f.read().strip())
os.kill(pid, signal.SIGTERM)
except (ProcessLookupError, ValueError, FileNotFoundError, PermissionError):
pass
try:
os.remove(pid_file)
except FileNotFoundError:
pass
def start():
"""Start animation as detached background process."""
# Detect session ID BEFORE detaching
session_id = get_session_id()
stop_process(session_id)
# Pass session ID to daemon via environment variable
env = os.environ.copy()
if session_id:
env['ANIMATE_TITLE_SESSION_ID'] = session_id
proc = subprocess.Popen(
[sys.executable, __file__, 'run'],
env=env,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
stdin=subprocess.DEVNULL,
start_new_session=True,
)
pid_file = get_pid_file(session_id)
with open(pid_file, 'w') as f:
f.write(str(proc.pid))
def stop():
"""Stop animation and restore name (non-blocking)."""
session_id = get_session_id()
stop_process(session_id)
# Pass session ID to restore process
env = os.environ.copy()
if session_id:
env['ANIMATE_TITLE_SESSION_ID'] = session_id
subprocess.Popen(
[sys.executable, __file__, 'restore'],
env=env,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
stdin=subprocess.DEVNULL,
start_new_session=True,
)
def burst():
"""Play fire burst animation (non-blocking)."""
session_id = get_session_id()
stop_process(session_id)
# Pass session ID to burst process
env = os.environ.copy()
if session_id:
env['ANIMATE_TITLE_SESSION_ID'] = session_id
subprocess.Popen(
[sys.executable, __file__, 'run_burst'],
env=env,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
stdin=subprocess.DEVNULL,
start_new_session=True,
)
if __name__ == '__main__':
if len(sys.argv) < 2:
print('Usage: animate_title.py start|stop|burst')
sys.exit(1)
cmd = sys.argv[1].lower()
if cmd == 'start':
start()
elif cmd == 'stop':
stop()
elif cmd == 'burst':
burst()
elif cmd == 'run':
run_animation()
elif cmd == 'restore':
run_restore()
elif cmd == 'run_burst':
run_burst()