From f74a0bff5dea6cc672db62ac07d7e6608c1bd5a5 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Sat, 31 Jul 2021 18:40:16 +0900 Subject: [PATCH] toast: Quick Kuroko toast client --- apps/toast.krk | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 apps/toast.krk diff --git a/apps/toast.krk b/apps/toast.krk new file mode 100644 index 00000000..81f1f310 --- /dev/null +++ b/apps/toast.krk @@ -0,0 +1,50 @@ +#!/bin/kuroko +import kuroko +import os + +def jsonFilter(c): + let mapping = { + '"': '\\"', + '\\': '\\\\', + '\b': '\\b', + '\f': '\\f', + '\n': '\\n', + '\r': '\\r', + '\t': '\\t', + } + return mapping[c] if c in mapping else c + +def dumpJson(o): + if isinstance(o,list): + return '[' + ','.join([dumpJson(i) for i in o]) + ']' + else if isinstance(o,int): + return repr(o) + else if isinstance(o,str): + return '"' + ''.join([jsonFilter(c) for c in o]) + '"' + else if isinstance(o,dict): + return '{' + ','.join([dumpJson(k) + ':' + dumpJson(v) for k, v in o.items()]) + '}' + else: + return dumpJson(str(o)) + +let msg = {} +let args = kuroko.argv[1:] +let sock = os.open("/dev/pex/toast",os.O_WRONLY) + +if not sock: + print("No toast daemon.") + return 1 + +while args: + let arg = args.pop(0) + if arg == '--icon': + msg['icon'] = args.pop(0) + else if arg == '--duration': + msg['duration'] = int(args.pop(0)) + else if arg.startswith('--'): + print("Unrecognized option:",arg) + else: + msg['body'] = arg + print("Sending toast:", dumpJson(msg),"to",sock) + os.write(sock, dumpJson(msg).encode()) + +