machine_setup/ansible/roles/sway/files/waybar_custom_clock.py

54 lines
1.4 KiB
Python
Raw Normal View History

2022-10-19 00:04:46 +00:00
#!/usr/bin/env python
#
# Custom waybar module for clocks. Implemented because the official clock module was downloading timezone definitions on every boot.
import json
import sys
import time
from dataclasses import dataclass
from typing import Final, List, Optional
INTERVAL: Final[int] = 10
@dataclass
class Update:
text: Optional[str]
alt: Optional[str]
tooltip: Optional[str]
css_class: Optional[List[str]]
percentage: Optional[str]
def dump(self) -> str:
# Dump a dict because we can't name our member variable "class"
return json.dumps(
{
k: v
for k, v in {
"text": self.text,
"alt": self.alt,
"tooltip": self.tooltip,
"class": self.css_class,
"percentage": self.percentage,
}.items()
if v is not None
}
)
def main():
next_update = time.time()
while True:
time_before_next_update = next_update - time.time()
if time_before_next_update > 0:
time.sleep(time_before_next_update)
next_update = time.time() + INTERVAL
argv = json.dumps(sys.argv)
out = Update(
text="foo", alt=argv, tooltip=argv, css_class=["foo"], percentage="100"
)
print(out.dump(), flush=True)
if __name__ == "__main__":
main()