Starting a custom clock in python.

This commit is contained in:
Tom Alexander 2022-10-18 20:04:46 -04:00
parent 637e9b5fed
commit 7e84f7980e
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 66 additions and 1 deletions

View File

@ -51,6 +51,7 @@
#clock,
#cpu,
#custom-keyboard-layout,
#custom-clock,
#memory,
#mode,
#network,

View File

@ -20,7 +20,7 @@
"spacing": 10
},
"custom/clock": {
"exec": "$HOME/.bin/.waybar_date",
"exec": "waybar_custom_clock",
"return-type": "json",
"restart-interval": 30
}

View File

@ -0,0 +1,53 @@
#!/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()

View File

@ -1,3 +1,14 @@
- name: Install scripts
copy:
src: "files/{{ item.src }}"
dest: "{{ item.dest }}"
mode: 0755
owner: root
group: wheel
loop:
- src: waybar_custom_clock.py
dest: /usr/local/bin/waybar_custom_clock
- import_tasks: tasks/freebsd.yaml
when: 'os_flavor == "freebsd"'