Skip to content

Instantly share code, notes, and snippets.

@jo-makar
Last active December 24, 2019 01:32
Show Gist options
  • Save jo-makar/c28c63d3c7bd9142ccf3e5a708ce3f74 to your computer and use it in GitHub Desktop.
Save jo-makar/c28c63d3c7bd9142ccf3e5a708ce3f74 to your computer and use it in GitHub Desktop.

Revisions

  1. jo-makar revised this gist Dec 24, 2019. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions i3-session.py
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    def i3msg(args):
    cmd = ['i3-msg'] + args
    logging.info('cmd = %r', cmd)
    out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, encoding='utf-8')
    subprocess.check_call(cmd, stderr=subprocess.STDOUT, encoding='utf-8')

    class IpcThread(threading.Thread):
    def __init__(self):
    @@ -40,8 +40,7 @@ def launch(cmd, timeout=30):

    def window_type(wid, text):
    cmd = ['xdotool', 'type', '--window', str(wid), text]
    out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, encoding='utf-8')
    logging.debug('%r => %r', cmd, out)
    subprocess.check_call(cmd, stderr=subprocess.STDOUT, encoding='utf-8')

    logging.basicConfig(format='%(asctime)s:%(threadName)s:%(levelname)s:%(message)s', level=logging.INFO)

  2. jo-makar created this gist Dec 24, 2019.
    65 changes: 65 additions & 0 deletions i3-session.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    #!/usr/bin/env python3
    # Restore/build an i3 session

    import argparse, json, logging, queue, subprocess, threading

    if __name__ == '__main__':
    def i3msg(args):
    cmd = ['i3-msg'] + args
    logging.info('cmd = %r', cmd)
    out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, encoding='utf-8')

    class IpcThread(threading.Thread):
    def __init__(self):
    threading.Thread.__init__(self)
    self.daemon = True
    self.queue = queue.Queue()

    def run(self):
    with subprocess.Popen(['i3-msg', '-t', 'subscribe', '-m', '["window"]'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding='utf-8') as proc:
    for line in proc.stdout:
    logging.debug('ipc: %s', line)
    if json.loads(line)['change'] == 'new':
    self.queue.put(json.loads(line)['container']['window'])

    def launch(cmd, timeout=30):
    # One effective way to know when a command has successfully launched is by tracking the creation of its window.
    # A separate thread is used to monitor "window" events for this, with a queue indicating windows creation with their ids.

    while not ipc.queue.empty():
    ipc.queue.get()

    i3msg(['exec --no-startup-id ' + cmd])

    return ipc.queue.get(timeout=timeout)

    focus = lambda x: i3msg(['focus', x])
    layout = lambda x: i3msg(['layout', x])
    splitvert = lambda: i3msg(['split', 'v'])
    workspace = lambda x: i3msg(['workspace', str(x)])

    def window_type(wid, text):
    cmd = ['xdotool', 'type', '--window', str(wid), text]
    out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, encoding='utf-8')
    logging.debug('%r => %r', cmd, out)

    logging.basicConfig(format='%(asctime)s:%(threadName)s:%(levelname)s:%(message)s', level=logging.INFO)

    parser = argparse.ArgumentParser()
    parser.add_argument('--debug', '-d', action='store_true')
    args = parser.parse_args()

    if args.debug:
    logging.getLogger().setLevel(logging.DEBUG)

    ipc = IpcThread()
    ipc.start()

    workspace(0)
    wid = launch('xterm')
    window_type(wid, 'cd ~/todo\n')

    workspace(1)
    launch('google-chrome --new-window "https://mail.google.com/mail/u/0/#inbox"')

    # ...