i3-companion: organize code a bit differently to ease browsing

This is not ideal, but I don't want to split into several files yet.
This commit is contained in:
Vincent Bernat 2021-07-07 14:14:12 +02:00
parent ec86d91c91
commit e089c59de5

View file

@ -41,35 +41,6 @@ def on(*events):
return decorator
def parse_args(args=sys.argv[1:]):
"""Parse arguments."""
parser = argparse.ArgumentParser(
description=sys.modules[__name__].__doc__,
formatter_class=CustomFormatter)
g = parser.add_mutually_exclusive_group()
g.add_argument("--debug", "-d", action="store_true",
default=False,
help="enable debugging")
g.add_argument("--silent", "-s", action="store_true",
default=False,
help="don't log")
return parser.parse_args(args)
def setup_logging(options):
"""Configure logging."""
root = logging.getLogger("")
root.setLevel(logging.WARNING)
logger.setLevel(options.debug and logging.DEBUG or logging.INFO)
if not options.silent:
ch = logging.StreamHandler()
ch.setFormatter(logging.Formatter(
"%(levelname)s[%(name)s] %(message)s"))
root.addHandler(ch)
# See https://fontawesome.com/v5.15/icons
application_icons = {
"chromium": "",
@ -95,6 +66,13 @@ application_icons_alone = {
}
@on(Event.WINDOW_MOVE, Event.WINDOW_NEW, Event.WINDOW_CLOSE)
async def workspace_rename(i3, event):
"""Rename workspaces using icons to match what's inside it."""
tree = await i3.get_tree()
workspaces = tree.workspaces()
commands = []
def application_icon(window):
"""Get application icon for a window."""
for attr in ('name',
@ -109,13 +87,6 @@ def application_icon(window):
return v
return application_icons["NOMATCH"]
@on(Event.WINDOW_MOVE, Event.WINDOW_NEW, Event.WINDOW_CLOSE)
async def workspace_rename(i3, event):
"""Rename workspaces using icons to match what's inside it."""
tree = await i3.get_tree()
workspaces = tree.workspaces()
commands = []
for workspace in workspaces:
icons = set()
for window in workspace.leaves():
@ -239,11 +210,6 @@ async def output_update(i3, event):
if output_update_running is not None:
output_update_running.cancel()
logger.debug("schedule XRandR change")
output_update_running = asyncio.get_event_loop().call_later(
1, output_update_now)
def output_update_now():
"""Execute actions to react to XRandR change."""
global output_update_running
@ -259,7 +225,21 @@ def output_update_now():
if proc.returncode != 0:
logger.warning(f"{cmd} exited with {proc.returncode}")
logger.debug("schedule XRandR change")
output_update_running = asyncio.get_event_loop().call_later(
1, output_update_now)
async def main(options):
i3 = await Connection().connect()
# Regular events
for fn, events in on.functions.items():
for event in events:
if isinstance(event, Event):
i3.on(event, fn)
# Ticks
async def tick_event(i3, event):
"""Process a TICK event."""
if type(event.payload) is not str:
@ -269,23 +249,28 @@ async def tick_event(i3, event):
for e in events:
if e == kind:
await fn(i3, event)
async def main(options):
i3 = await Connection().connect()
for fn, events in on.functions.items():
for event in events:
if isinstance(event, Event):
i3.on(event, fn)
i3.on(Event.TICK, tick_event)
await i3.main()
if __name__ == "__main__":
options = parse_args()
setup_logging(options)
# Parse
parser = argparse.ArgumentParser(
description=sys.modules[__name__].__doc__)
parser.add_argument("--debug", "-d", action="store_true",
default=False,
help="enable debugging")
options = parser.parse_args()
# Logging
root = logging.getLogger("")
root.setLevel(logging.WARNING)
logger.setLevel(options.debug and logging.DEBUG or logging.INFO)
ch = logging.StreamHandler()
ch.setFormatter(logging.Formatter(
"%(levelname)s[%(name)s] %(message)s"))
root.addHandler(ch)
try:
asyncio.get_event_loop().run_until_complete(main(options))