Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example: focus on vim session #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions examples/run_vim_session.py
Original file line number Diff line number Diff line change
@@ -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()