Last active
July 15, 2024 21:02
-
-
Save un-def/8362f2f95d4ece184d94453d8ceaada5 to your computer and use it in GitHub Desktop.
i3 persistent workspaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
import argparse | |
import json | |
import subprocess | |
from typing import Callable, Iterable, Iterator, TypedDict | |
class Workspace(TypedDict): | |
name: str | |
def print_workspaces(names: Iterable[str]) -> None: | |
workspace_iter: Iterator[Workspace] = iter(json.loads( | |
subprocess.check_output(['i3-msg', '-t', 'get_workspaces']))) | |
workspaces: list[Workspace] = [] | |
workspace = next(workspace_iter, None) | |
for name in names: | |
if workspace and workspace['name'] == name: | |
workspaces.append(workspace) | |
workspace = next(workspace_iter, None) | |
else: | |
workspaces.append({'name': name}) | |
print(json.dumps(workspaces), flush=True) | |
def subscribe(callback: Callable[[bytes], None]) -> None: | |
listener = subprocess.Popen( | |
['i3-msg', '-t', 'subscribe', '-m', '["workspace", "output"]'], | |
stdout=subprocess.PIPE, | |
) | |
assert listener.stdout is not None, 'makes typechecker happy' | |
for line in listener.stdout: | |
callback(line) | |
def main() -> None: | |
parser = argparse.ArgumentParser() | |
parser.add_argument('workspaces', nargs='+', metavar='WORKSPACE') | |
args = parser.parse_args() | |
workspaces: list[str] = args.workspaces | |
print_workspaces(workspaces) | |
subscribe(lambda _: print_workspaces(workspaces)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i3 version 4.23+ is required: https://i3wm.org/docs/i3bar-workspace-protocol.html
i3 config: