From 49a06704bf091ecc768f1a4c8a93af102886d8d1 Mon Sep 17 00:00:00 2001 From: Egor Zvorykin Date: Tue, 10 Apr 2018 15:53:49 +0300 Subject: [PATCH] add example: focus on vim session --- examples/run_vim_session.py | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 examples/run_vim_session.py diff --git a/examples/run_vim_session.py b/examples/run_vim_session.py new file mode 100755 index 0000000..2b23276 --- /dev/null +++ b/examples/run_vim_session.py @@ -0,0 +1,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()