Cleanup imports, add exit code.

This commit is contained in:
redxef 2022-10-20 12:37:07 +02:00
parent 459de18a9c
commit ec8fd5171f
Signed by: redxef
GPG key ID: 7DAC3AA211CBD921

View file

@ -1,10 +1,10 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import enum
import functools
import itertools
import json
import time
import click
import i3ipc
@ -269,27 +269,38 @@ def window_new(filter, *, workspace, debug):
if debug:
print(json.dumps(e.ipc_data))
if filter.reduce(e.ipc_data):
ipc.command(f'move container to workspace {workspace}')
container_id = e.ipc_data['container']['id']
ipc.command(f'for_window [con_id="{container_id}"] move container to workspace {workspace}')
ipc.main_quit()
return callback
@click.command()
@click.pass_context
@click.option('--filter', '-f', default='True', help="A filter expression for the raw ipc dictionary.")
@click.option('--debug', '-d', default=False, is_flag=True, help="Enable debug mode, will log ipc dictionary.")
@click.option('--timeout', '-t', default=3000, help="Wait time for a window to appear (and match) in milliseconds.")
@click.option('--workspace', '-w', required=True, help="The workspace to move to.")
@click.argument('program', nargs=-1)
def main(filter, debug, workspace, program):
def main(ctx, filter, debug, timeout, workspace, program):
"""
Start a program and move it's created window to the desired i3 workspace.
\b
Exist status:
0 on success,
1 when no window has been found.
"""
filter = parse(filter)
program = ' '.join(program)
ipc = i3ipc.Connection()
ipc.on('window::new', window_new(filter, workspace=workspace, debug=debug))
ipc.command(f'exec {program}')
ipc.main(timeout=10)
started_at = time.monotonic_ns() // (1000*1000)
ipc.main(timeout=timeout / 1000)
total_time = time.monotonic_ns() // (1000*1000) - started_at
ctx.exit(int(total_time >= timeout))
if __name__ == '__main__':
main()