diff --git a/README.md b/README.md index 1f429d0..27aae75 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ bootDataspace("main") do (dataspace: Ref; turn: var Turn): ## Examples -- [simpleChatProtocol](tests/chat.nim) +- [src/syndicate/unix](./src/syndicate/unix) - [erisresolver](https://codeberg.org/eris/nim-eris_utils#erisresolver) - dynamic configuration - [xdg_open_ng](https://git.syndicate-lang.org/ehmry/xdg_open_ng) - messaging, UNIX sockets, dynamic configuration, [Syndicate server](https://synit.org/book/operation/system-bus.html) interaction. diff --git a/src/syndicate/unix/swaybar_status_actor.nim b/src/syndicate/unix/swaybar_status_actor.nim new file mode 100644 index 0000000..9f68636 --- /dev/null +++ b/src/syndicate/unix/swaybar_status_actor.nim @@ -0,0 +1,50 @@ +# SPDX-FileCopyrightText: ☭ 2022 Emery Hemingway +# SPDX-License-Identifier: Unlicense + +## See the swaybar-protocol(7) manpage. + +import std/[asyncdispatch, json, os, strutils] +import preserves, preserves/jsonhooks +import syndicate, syndicate/capabilities + +proc unixSocketPath: string = + let args = commandLineParams() + case args.len + of 1: result = args[0] + of 0: + result = getEnv("SYNDICATE_SOCK") + if result == "": + result = getEnv("XDG_RUNTIME_DIR", "/run/user/1000") / "dataspace" + else: quit "must pass Syndicate socket location as the only argument" + +proc mintCap: SturdyRef = + var key: array[16, byte] + mint(key, "syndicate") + +type SwaybarStatus {.preservesRecord: "swaybar-status".} = object + elem: JsonNode + +bootDataspace("main") do (root: Ref; turn: var Turn): + let header = %* { "version": 1 } + stdout.write(header, "\x0a") + stdout.flushFile() + stdout.write("[") + connectUnix(turn, unixSocketPath(), mintCap()) do (turn: var Turn; ds: Ref): + + var lineElements = initSet[Ref]() + proc sendLine(turn: var Turn) = + var line: JsonNode + if fromPreserve(line, lineElements): + stdout.writeLine(line, ",") + stdout.flushFile() + else: + stderr.writeLine "could not convert ", lineElements + + during(turn, ds, ?SwaybarStatus) do (a: Assertion): + lineElements.incl a + sendLine(turn) + do: + lineElements.excl a + sendLine(turn) + +runForever() diff --git a/syndicate.nimble b/syndicate.nimble index f01ce52..c30e823 100644 --- a/syndicate.nimble +++ b/syndicate.nimble @@ -1,6 +1,6 @@ # Package -version = "20220904" +version = "20221013" author = "Emery Hemingway" description = "Syndicated actors for conversational concurrency" license = "Unlicense" @@ -9,4 +9,4 @@ srcDir = "src" # Dependencies -requires "nim >= 1.4.8", "nimSHA2 >= 0.1.1", "preserves >= 20220709" +requires "nim >= 1.4.8", "nimSHA2 >= 0.1.1", "preserves >= 20221013"