Proper capture references

This commit is contained in:
Emery Hemingway 2022-04-15 18:04:37 -05:00
parent a959565fb5
commit 1c22d61d0f
4 changed files with 27 additions and 11 deletions

View File

@ -8,18 +8,21 @@ import ./protocol
bootDataspace("main") do (root: Ref; turn: var Turn): bootDataspace("main") do (root: Ref; turn: var Turn):
var var
actions: seq[tuple[regex: Regex; cmd: string; args: seq[string]]] actions: seq[tuple[regex: Regex; cmd: string; args: seq[Assertion]]]
children: Deque[Process] children: Deque[Process]
connectStdio(root, turn) connectStdio(root, turn)
onPublish(turn, root, ?ActionHandler) do (pat: string; cmd: seq[string]): onPublish(turn, root, ?ActionHandler) do (pat: string; cmd: seq[Assertion]):
# TODO: during # TODO: during
if cmd.len < 2: if cmd.len < 2:
stderr.writeLine "ignoring ", $cmd, " for ", pat stderr.writeLine "ignoring ", $cmd, " for ", pat
else: else:
var act = (re(pat, {reIgnoreCase, reStudy}), cmd[0], cmd[1..cmd.high],) if cmd[0].isString:
actions.add act var act = (re(pat, {reIgnoreCase, reStudy}), cmd[0].string, cmd[1..cmd.high],)
actions.add act
else:
stderr.writeLine "not a valid program specification: ", cmd[0]
onPublish(turn, root, ?ListenOn[Ref]) do (a: Assertion): onPublish(turn, root, ?ListenOn[Ref]) do (a: Assertion):
let ds = unembed a let ds = unembed a
@ -37,7 +40,13 @@ bootDataspace("main") do (root: Ref; turn: var Turn):
matched = true matched = true
var args = newSeq[string](act.args.len) var args = newSeq[string](act.args.len)
for i, arg in act.args: for i, arg in act.args:
args[i] = replace(arg, "\\1", uri) if arg.isString:
args[i] = replacef(uri, act.regex, arg.string)
elif arg.isInteger:
if arg.int == 0:
args[i] = uri
else:
args[i] = replacef(uri, act.regex, "$" & $arg.int)
var child = startProcess( var child = startProcess(
command = act.cmd, args = args, options = {}) command = act.cmd, args = args, options = {})
children.addLast child children.addLast child

View File

@ -20,7 +20,6 @@ bootDataspace("main") do (ds: Ref; turn: var Turn):
connectUnix(turn, unixSocketPath(), mintCap()) do (turn: var Turn; a: Assertion) -> TurnAction: connectUnix(turn, unixSocketPath(), mintCap()) do (turn: var Turn; a: Assertion) -> TurnAction:
let ds = unembed a let ds = unembed a
message(turn, ds, XdgOpen(uris: commandLineParams())) message(turn, ds, XdgOpen(uris: commandLineParams()))
stop(turn, mainFacet)
for i in 0..7: poll(20) for i in 0..7: poll(20)
# A hack to exit # A hack to exit

View File

@ -13,9 +13,17 @@ let ?root_ds = dataspace
? <service-object <daemon uri_runner> ?cap> [ ? <service-object <daemon uri_runner> ?cap> [
$cap [ $cap [
<listen-on $root_ds> <listen-on $root_ds>
<action-handler "gemini://.*|file:///.*.gmi" ["/run/current-system/sw/bin/kristall" "\\1"]>
<action-handler "http://.*|https://.*|.*html", ["/run/current-system/sw/bin/librewolf" "\\1"]> ; Here the "0" argument is replaced with the whole URI asserted by xdg-open.
<action-handler "tox:.*|uri:tox:.*", ["/run/current-system/sw/bin/qtox" "\\1"]> <action-handler "http://.*|https://.*|.*html", ["/run/current-system/sw/bin/librewolf" 0]>
<action-handler ".*\\.avi|.*\\.mkv|.*\\.mp4", ["/run/current-system/sw/bin/mpv" "\\1"]>
; An argument can be a reference to a capture.
<action-handler "(tox:.*)|uri:(tox:.*)", ["/run/current-system/sw/bin/qtox" 1]>
; An argument can contain a reference to a capture using the $i notation.
<action-handler "https://twitter.com/(.*)" ["/run/current-system/sw/bin/librewolf" "https://nitter.net/$1"]>
<action-handler "gemini://.*|file:///.*.gmi" ["/run/current-system/sw/bin/kristall" 0]>
<action-handler ".*\\.avi|.*\\.mkv|.*\\.mp4|.*\\.ogg|.*\\.opus", ["/run/current-system/sw/bin/mpv" 0]>
] ]
] ]

View File

@ -1,6 +1,6 @@
# Package # Package
version = "0.2.0" version = "0.3.0"
author = "Emery" author = "Emery"
description = "A better xdg-open" description = "A better xdg-open"
license = "Unlicense" license = "Unlicense"