Use runActor proc

This commit is contained in:
Emery Hemingway 2023-05-06 20:25:02 +01:00
parent 80b7bf7277
commit 1b62a1e571
2 changed files with 32 additions and 25 deletions

View File

@ -1,6 +1,6 @@
# Package
version = "20230405"
version = "20230506"
author = "Emery Hemingway"
description = "Actor for sending notifications with Libnotify"
license = "Unlicense"

View File

@ -21,7 +21,7 @@ type
message: cstring
proc init(appName: cstring): bool {.notify.}
# proc uninit() {.notify.}
proc uninit() {.notify.}
proc notification_new(summary, body, icon: cstring = nil): Notification {.notify.}
proc show(n: Notification; err: ptr GError): bool {.notification.}
@ -41,31 +41,38 @@ const appName = "libnotify_actor"
if not init(appName):
quit "failed to initialize libnotify"
bootDataspace(appName) do (ds: Ref; turn: var Turn):
connectStdio(ds, turn)
#during(turn, ds, grab()) do (config: Config):
# discard init(config.app_name)
onMessage(turn, ds, ?Notify) do (summary: string, attrs: Attrs):
let
body = getOrDefault(attrs, Symbol"body")
icon = getOrDefault(attrs, Symbol"icon")
n = notification_new(summary, body, icon)
case getOrDefault(attrs, Symbol"urgency").normalize
of "high", "critical":
set_urgency(n, Critical)
of "low":
set_urgency(n, Low)
else: discard
type Args {.preservesDictionary.} = object
dataspace: Ref
let app_name = getOrDefault(attrs, Symbol"app_name")
if app_name != "":
set_app_name(n, app_name)
runActor("main") do (root: Ref; turn: var Turn):
connectStdio(root, turn)
during(turn, root, ?Args) do (ds: Ref):
if not init(appName):
stderr.writeLine "failed to initialize libnotify"
else:
onMessage(turn, ds, ?Notify) do (summary: string, attrs: Attrs):
let
body = getOrDefault(attrs, Symbol"body")
icon = getOrDefault(attrs, Symbol"icon")
n = notification_new(summary, body, icon)
let category = getOrDefault(attrs, Symbol"category")
if category != "":
set_category(n, category)
case getOrDefault(attrs, Symbol"urgency").normalize
of "high", "critical":
set_urgency(n, Critical)
of "low":
set_urgency(n, Low)
else: discard
show(n)
let app_name = getOrDefault(attrs, Symbol"app_name")
if app_name != "":
set_app_name(n, app_name)
runForever()
let category = getOrDefault(attrs, Symbol"category")
if category != "":
set_category(n, category)
show(n)
do:
uninit()