aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/i3blocks
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-08-30 20:36:12 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-08-30 20:36:12 -0500
commit3e13ef897a771be178a40b0b625bbe0994f0150c (patch)
tree47994afcdaeec9a898a302459d9f7bb440b483b4 /i3blocks
parent39ba89e0eaee634e8d69b54d14efa0040b4c1c42 (diff)
fix(i3blocks): simplify title block
Diffstat (limited to 'i3blocks')
-rw-r--r--i3blocks/.config/i3blocks/config1
-rwxr-xr-xi3blocks/.local/lib/i3blocks/i3blocks-title114
2 files changed, 24 insertions, 91 deletions
diff --git a/i3blocks/.config/i3blocks/config b/i3blocks/.config/i3blocks/config
index c2207ed..6dfacda 100644
--- a/i3blocks/.config/i3blocks/config
+++ b/i3blocks/.config/i3blocks/config
@@ -2,6 +2,7 @@
command=$SCRIPT_DIR/i3blocks-title
max_length=30
interval=persist
+format=json
[weather]
command=$SCRIPT_DIR/i3blocks-wttr
diff --git a/i3blocks/.local/lib/i3blocks/i3blocks-title b/i3blocks/.local/lib/i3blocks/i3blocks-title
index a2f8ca5..52ffd47 100755
--- a/i3blocks/.local/lib/i3blocks/i3blocks-title
+++ b/i3blocks/.local/lib/i3blocks/i3blocks-title
@@ -1,91 +1,23 @@
-#!/usr/bin/env python3
-
-import asyncio
-from asyncio import TaskGroup, CancelledError
-from collections import deque
-from itertools import islice
-
-import i3ipc
-from i3ipc.aio import Connection
-
-
-class Printer:
- def __init__(self, title: str | None = None, width: int = 30):
- self.width = width
- match title:
- case str("") | None:
- self.iter = [""]
- case str(s):
- self.iter = deque(list(s + " "))
-
- def __iter__(self):
- while self.iter:
- yield "".join(islice(self.iter, 0, self.width))
- if len(self.iter) > self.width:
- self.iter.rotate(-1)
- else:
- break
-
- async def print(self):
- for status in iter(self):
- print(status, flush=True)
- await asyncio.sleep(0.5)
-
-
-class Blocklet:
- SET_EVENTS = [
- i3ipc.Event.WINDOW_FOCUS,
- i3ipc.Event.WINDOW_TITLE,
- i3ipc.Event.WINDOW_NEW,
- ]
-
- CLEAR_EVENTS = [
- i3ipc.Event.WINDOW_CLOSE,
- i3ipc.Event.WORKSPACE_FOCUS,
- ]
-
- def __init__(self, task_group: TaskGroup):
- self.i3 = None
- self.task_group = task_group
- self.printer_task = None
-
- @classmethod
- async def connect(cls, task_group: TaskGroup):
- self = cls(task_group)
- self.i3 = await Connection().connect()
-
- for event in Blocklet.SET_EVENTS:
- self.i3.on(event, lambda _, e: self.set(e.container.name))
-
- for event in Blocklet.CLEAR_EVENTS:
- self.i3.on(event, lambda _, e: self.clear())
-
- return self
-
- async def main(self):
- if focused := (await self.i3.get_tree()).find_focused():
- self.set(focused.name)
- await self.i3.main()
-
- def set(self, title: str = None):
- if self.printer_task is not None:
- self.printer_task.cancel()
- self.printer_task = self.task_group.create_task(Printer(title).print())
-
- def clear(self):
- if self.printer_task is not None:
- self.printer_task.cancel()
- print(flush=True)
-
-
-async def main():
- try:
- async with TaskGroup() as task_group:
- blocklet = await Blocklet.connect(task_group)
- task_group.create_task(blocklet.main())
- except CancelledError:
- return
-
-
-if __name__ == "__main__":
- asyncio.run(main())
+#!/bin/sh
+
+swaymsg -t get_tree |
+ jq --compact-output '.. | select(.focused? == true and .type == "con") |
+ {
+ "full_text": (.name // ""),
+ "short_text": ([.app_id, .window_title] | map(select(. != null and . != "")) | (first // "")),
+ }
+'
+
+swaymsg --monitor -t subscribe '["window", "workspace"]' |
+ jq --compact-output --unbuffered '
+ if .change == "close" and .container.focused == true then
+ { "full_text": "" }
+ elif .change == "focus" or (.change == "title" and .container.focused == true) then
+ .container | {
+ "full_text": (.name // ""),
+ "short_text": ([.app_id, .window_title] | map(select(. != null and . != "")) | (first // "")),
+ }
+ else
+ empty
+ end
+'