From c2f6e81215b14bf7a8e6625513e1bb6201ac01a9 Mon Sep 17 00:00:00 2001 From: Ezri Brimhall Date: Tue, 19 Mar 2024 13:32:42 -0600 Subject: [PATCH] eww clock no longer spawns a new process every 0.5 seconds --- .config/eww/modules/clock.yuck | 4 ++-- .config/eww/scripts/date.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100755 .config/eww/scripts/date.py diff --git a/.config/eww/modules/clock.yuck b/.config/eww/modules/clock.yuck index ad14c7a..cb457cf 100644 --- a/.config/eww/modules/clock.yuck +++ b/.config/eww/modules/clock.yuck @@ -1,6 +1,6 @@ ;; -*-lisp-*- -(defpoll clock--data :interval "500ms" - `date +'{"hour": "%H", "minute": "%M", "second": "%S", "year": "%Y", "day": "%d", "month": "%m", "dow": "%A", "month_name": "%B", "unix": %s}'`) +(deflisten clock--data :initial "{}" + `~/.config/eww/scripts/date.py`) (defvar clock--show "clock") diff --git a/.config/eww/scripts/date.py b/.config/eww/scripts/date.py new file mode 100755 index 0000000..8203922 --- /dev/null +++ b/.config/eww/scripts/date.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +"""A simple Python script to output the current date and time as a JSON object string every 0.5 seconds. + +Used instead of the `date` command in order to avoid spawning a new process every 0.5 seconds. +""" + +import datetime +import sys +import time + + +def get_date(): + """Return the current date as a JSON object string.""" + return datetime.datetime.now().strftime( + '{"hour": "%H", "minute": "%M", "second": "%S", "day": "%d", "month": "%m", "year": "%Y", "dow": "%w","month_name": "%B", "unix": %s}' + ) + + +if __name__ == "__main__": + while True: + print(get_date()) + sys.stdout.flush() + time.sleep(0.5)