-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathrun_vim_session.py
More file actions
executable file
·44 lines (35 loc) · 1.02 KB
/
run_vim_session.py
File metadata and controls
executable file
·44 lines (35 loc) · 1.02 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
#!/usr/bin/python
import re
import sys
import psutil
import i3ipc
def get_window_id_by_pid(pid):
with open('/proc/{}/environ'.format(pid), 'r') as f:
pid_env = f.read()
match = re.search(r'WINDOWID=(\d+)', pid_env)
wid = int(match.group(1))
return wid
def get_session_pid(session_name):
for process in psutil.process_iter():
if '--servername {}'.format(session_name.lower()) in ' '.join(process.cmdline()).lower():
yield process.pid
def get_vim_window_id(session_name):
while True:
try:
pid = next(get_session_pid(session_name))
except StopIteration:
break
if pid is None:
continue
else:
break
return get_window_id_by_pid(pid)
def main():
for line in sys.stdin:
vim_window_id = get_vim_window_id(line.strip('\n'))
break
if vim_window_id is not None:
con = i3ipc.Connection()
con.command('[id=%s] focus' % vim_window_id)
if __name__ == "__main__":
main()