From b0be7c2d1eb9766672d8e4670cbff1d010268020 Mon Sep 17 00:00:00 2001 From: yuh Date: Sat, 2 Nov 2024 02:41:01 +0800 Subject: [PATCH] fix: decode unicode escape sequences (fixes emojis, Chinese, etc) (#6) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Item name Chinese encoding * Apply suggestions from code review * Apply suggestions from code review --------- Co-authored-by: Erik Bjäreholt --- aw_notify/main.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/aw_notify/main.py b/aw_notify/main.py index 57a3f85..5978daf 100644 --- a/aw_notify/main.py +++ b/aw_notify/main.py @@ -331,6 +331,16 @@ def checkin(testing=False): send_checkin() +def decode_unicode_escapes(s: str) -> str: + """ + Decodes any Unicode escape sequences present in the input string + and returns the decoded result. + """ + # see https://github.com/ActivityWatch/aw-notify/pull/6 + # assert "工作" == decode_unicode_escapes("\\u5de5\\u4f5c") + return s.encode('utf-8').decode('unicode_escape') + + def send_checkin(title="Time today", date=None): """ Sends a summary notification of the day. @@ -346,7 +356,7 @@ def send_checkin(title="Time today", date=None): ][:4] msg = "" - msg += "\n".join(f"- {c}: {t}" for c, t in top_categories) + msg += "\n".join(f"- {decode_unicode_escapes(c)}: {t}" for c, t in top_categories) if msg: notify(title, msg) else: