Compare commits
7 Commits
Author | SHA1 | Date |
---|---|---|
|
74f267740a | |
|
bb773e10b9 | |
|
c77c1cb8ac | |
|
509970a2d2 | |
|
bcc1870176 | |
|
1717abb47a | |
|
4db9037153 |
2
.envrc
2
.envrc
|
@ -1,2 +1,2 @@
|
||||||
source_env ..
|
source_env ..
|
||||||
use flake syndicate#xdg_open_ng
|
use nix
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
/.direnv
|
/nim.cfg
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
# xdg-open-ng
|
# Syndicated Open
|
||||||
|
|
||||||
An [xdg-open](https://portland.freedesktop.org/doc/xdg-open.html) replacement that uses [Syndicate](http://syndicate-lang.org/) and PCRE pattern matching to open URIs.
|
An [open command](https://en.wikipedia.org/wiki/Open_(process)) implementation that uses [Syndicate](http://syndicate-lang.org/) and PCRE pattern matching to open URIs.
|
||||||
|
|
||||||
There are two utilites, `xdg-open` and `uri_runner`. The former connects to a shared Syndicate dataspace via a UNIX socket at `$SYNDICATE_SOCK` otherwise `$XDG_RUNTIME_DIR/dataspace` and has no other configuration. The `uri_runner` component is intended to be managed by the [Syndicate server](https://git.syndicate-lang.org/syndicate-lang/syndicate-rs) thru which it receives configuration, see [uri_runner.pr](./uri_runner.pr) as an example.
|
There are two utilites, `open` and `uri_runner`. The former connects to a shared Syndicate dataspace using a route at `$SYNDICATE_ROUTE` and has no other configuration. The `uri_runner` component is intended to be managed by the [Syndicate server](https://git.syndicate-lang.org/syndicate-lang/syndicate-rs) thru which it receives configuration, see [uri_runner.pr](./uri_runner.pr) as an example.
|
||||||
|
|
||||||
Matching patterns to actions is done with `action-handler` records:
|
Matching patterns to actions is done with `action-handler` records:
|
||||||
```preserves
|
```preserves
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
include ../syndicate-nim/depends.tup
|
include ../syndicate-nim/depends.tup
|
||||||
NIM_FLAGS += --path:$(TUP_CWD)/../syndicate-nim/src
|
NIM_FLAGS += --path:$(TUP_CWD)/../syndicate-nim/src
|
||||||
NIM_GROUPS += $(TUP_CWD)/../nimble/<nimsha2>
|
NIM_GROUPS += $(TUP_CWD)/<lock>
|
||||||
|
|
|
@ -16,9 +16,11 @@
|
||||||
<action-handler "https://twitter.com/(.*)" $execspace
|
<action-handler "https://twitter.com/(.*)" $execspace
|
||||||
<exec ["/run/current-system/sw/bin/firefox" "--new-tab" "https://nitter.net/$1"]>>
|
<exec ["/run/current-system/sw/bin/firefox" "--new-tab" "https://nitter.net/$1"]>>
|
||||||
|
|
||||||
<action-handler "(http://.*)|(https://.*)" $execspace
|
<action-handler "(file:///.*.html|http://.*|https://.*)" $execspace
|
||||||
<exec ["/run/current-system/sw/bin/firefox" "$1"]>>
|
<exec ["/run/current-system/sw/bin/firefox" "$#"]>>
|
||||||
|
|
||||||
|
<action-handler "mailto:(.*)" $execspace
|
||||||
|
<exec ["/run/current-system/sw/bin/astroid" "--mailto" "$#"]>>
|
||||||
]
|
]
|
||||||
|
|
||||||
; when mpv is available send it commands directly
|
; when mpv is available send it commands directly
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
{"depends":[{"method":"fetchzip","packages":["hashlib"],"path":"/nix/store/v03nzlpdgbfxd2zhcnkfbkq01d5kqxcl-source","rev":"84e0247555e4488594975900401baaf5bbbfb53","sha256":"1pfczsv8kl36qpv543f93d2y2vgz2acckssfap7l51s2x62m6qwx","srcDir":"","url":"https://github.com/khchen/hashlib/archive/84e0247555e4488594975900401baaf5bbbfb53.tar.gz"},{"method":"fetchzip","packages":["nimcrypto"],"path":"/nix/store/zyr8zwh7vaiycn1s4r8cxwc71f2k5l0h-source","ref":"traditional-api","rev":"602c5d20c69c76137201b5d41f788f72afb95aa8","sha256":"1dmdmgb6b9m5f8dyxk781nnd61dsk3hdxqks7idk9ncnpj9fng65","srcDir":"","url":"https://github.com/cheatfate/nimcrypto/archive/602c5d20c69c76137201b5d41f788f72afb95aa8.tar.gz"},{"method":"fetchzip","packages":["npeg"],"path":"/nix/store/ffkxmjmigfs7zhhiiqm0iw2c34smyciy-source","ref":"1.2.1","rev":"26d62fdc40feb84c6533956dc11d5ee9ea9b6c09","sha256":"0xpzifjkfp49w76qmaylan8q181bs45anmp46l4bwr3lkrr7bpwh","srcDir":"src","url":"https://github.com/zevv/npeg/archive/26d62fdc40feb84c6533956dc11d5ee9ea9b6c09.tar.gz"},{"method":"fetchzip","packages":["preserves"],"path":"/nix/store/nrcpzf9hx70kry3gwhrdzcs3qicjncjh-source","ref":"20231021","rev":"edece399be70818208bf2263c30cb2bcf435bbff","sha256":"0xmw35wmw3a4lja9q4qvlvpxv3xk0hnkjg4fwfw6f3inh6zfiqki","srcDir":"src","url":"https://git.syndicate-lang.org/ehmry/preserves-nim/archive/edece399be70818208bf2263c30cb2bcf435bbff.tar.gz"},{"method":"fetchzip","packages":["syndicate"],"path":"/nix/store/1y3nnpp2mhxqmdb3xh4c4k5k5l9hhqk3-source","ref":"20231019","rev":"57b99b20e7db1b97b1cb9c6df574bd13983c26fc","sha256":"1kgb3a78igs37xkmv8cbaxa17qdjf2h43vdmpda517c9086ggsn5","srcDir":"src","url":"https://git.syndicate-lang.org/ehmry/syndicate-nim/archive/57b99b20e7db1b97b1cb9c6df574bd13983c26fc.tar.gz"}]}
|
|
@ -1,6 +1,12 @@
|
||||||
version 1 .
|
version 1 .
|
||||||
|
|
||||||
XdgOpen = <xdg-open @uri string> .
|
Authority = [ @username any @password any @host any @port any ] .
|
||||||
|
|
||||||
|
; Uniform Resource Identifier https://datatracker.ietf.org/doc/html/rfc3986
|
||||||
|
Uri = <uri @scheme string @auth Authority @path any @query any @fragment any> .
|
||||||
|
|
||||||
|
; Broadcast this to open a URI.
|
||||||
|
Open = <open @uri Uri>.
|
||||||
|
|
||||||
UriRunnerConfig = {
|
UriRunnerConfig = {
|
||||||
handlerspace: #!any
|
handlerspace: #!any
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
{ pkgs ? import <nixpkgs> { } }:
|
||||||
|
|
||||||
|
pkgs.nim2Packages.buildNimPackage {
|
||||||
|
name = "dummy";
|
||||||
|
nativeBuildInputs = [ pkgs.pkg-config ];
|
||||||
|
buildInputs = [ pkgs.pcre ];
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
include_rules
|
include_rules
|
||||||
: foreach ../*.prs |> !preserves_schema_nim |> | {schema}
|
: foreach ../*.prs |> !preserves_schema_nim |> | {common}
|
||||||
: uri_runner.nim | $(SYNDICATE_PROTOCOL) {schema} |> !nim_bin |> {bin}
|
: common.nim |> !nim_check |> {common}
|
||||||
: xdg_open.nim | $(SYNDICATE_PROTOCOL) {schema} |> !nim_bin |> $(BIN_DIR)/xdg-open {bin}
|
: uri_runner.nim | $(SYNDICATE_PROTOCOL) {common} |> !nim_bin |> {bin}
|
||||||
|
: open.nim | $(SYNDICATE_PROTOCOL) {common} |> !nim_bin |> {bin}
|
||||||
: foreach {bin} |> !assert_built |>
|
: foreach {bin} |> !assert_built |>
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||||
|
# SPDX-License-Identifier: Unlicense
|
||||||
|
|
||||||
|
import std/uri
|
||||||
|
import preserves
|
||||||
|
|
||||||
|
proc toPreserveOrNull(s: string; E: typedesc): Preserve[E] =
|
||||||
|
if s != "": result = toPreserve(s, E)
|
||||||
|
|
||||||
|
proc toPreserveHook*(u: Uri; E: typedesc): Preserve[E] =
|
||||||
|
## Best-effort preservation of the `Uri` type.
|
||||||
|
runnableExamples:
|
||||||
|
from std/unittest import check
|
||||||
|
import std/uri
|
||||||
|
import preserves
|
||||||
|
var u = parseUri"https://bidstonobservatory.org"
|
||||||
|
check $(u.toPreserve) == """<uri "https" [#f #f "bidstonobservatory.org" #f] #f #f #f>"""
|
||||||
|
initRecord("uri",
|
||||||
|
u.scheme.toPreserve(E),
|
||||||
|
toPreserve([
|
||||||
|
u.username.toPreserveOrNull(E),
|
||||||
|
u.password.toPreserveOrNull(E),
|
||||||
|
u.hostname.toPreserveOrNull(E),
|
||||||
|
u.port.toPreserveOrNull(E),
|
||||||
|
]),
|
||||||
|
u.path.toPreserveOrNull(E),
|
||||||
|
u.query.toPreserveOrNull(E),
|
||||||
|
u.anchor.toPreserveOrNull(E),
|
||||||
|
)
|
||||||
|
|
||||||
|
proc fromPreserveHook*[E](u: var Uri; pr: Preserve[E]): bool =
|
||||||
|
## Best-effort preservation of the `Uri` type.
|
||||||
|
runnableExamples:
|
||||||
|
import std/uri
|
||||||
|
import preserves
|
||||||
|
var u: Uri
|
||||||
|
doAssert fromPreserveHook(u, parsePreserves"""<uri "https" [#f #f "bidstonobservatory.org" #f] #f #f #f>""")
|
||||||
|
if pr.isRecord("uri", 5):
|
||||||
|
result = fromPreserve(u.scheme, pr[0])
|
||||||
|
if result:
|
||||||
|
if pr[1].isSequence and pr[1].sequence.len == 4:
|
||||||
|
discard fromPreserve(u.username, pr[1][0])
|
||||||
|
discard fromPreserve(u.password, pr[1][1])
|
||||||
|
discard fromPreserve(u.hostname, pr[1][2])
|
||||||
|
discard fromPreserve(u.port, pr[1][3])
|
||||||
|
discard fromPreserve(u.path, pr[2])
|
||||||
|
discard fromPreserve(u.query, pr[3])
|
||||||
|
discard fromPreserve(u.anchor, pr[4])
|
|
@ -0,0 +1,25 @@
|
||||||
|
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||||
|
# SPDX-License-Identifier: Unlicense
|
||||||
|
|
||||||
|
import std/[asyncdispatch, os, uri]
|
||||||
|
import preserves, syndicate, syndicate/relays, syndicate/capabilities
|
||||||
|
import ./protocol, ./common
|
||||||
|
|
||||||
|
proc publishUri(turn: var Turn; ds: Cap) =
|
||||||
|
for arg in commandLineParams():
|
||||||
|
if fileExists(arg):
|
||||||
|
message(turn, ds, Open(uri: protocol.Uri(
|
||||||
|
scheme: "file", path: arg.absolutePath.toPreserve)))
|
||||||
|
else:
|
||||||
|
message(turn, ds, initRecord("open", arg.parseUri.toPreserve))
|
||||||
|
stop(turn)
|
||||||
|
|
||||||
|
proc main =
|
||||||
|
let route = envRoute()
|
||||||
|
discard bootDataspace("open") do (root: Cap; turn: var Turn):
|
||||||
|
resolve(turn, root, route, publishUri)
|
||||||
|
|
||||||
|
for i in 0..4: poll(20)
|
||||||
|
# A hack to exit
|
||||||
|
|
||||||
|
main()
|
|
@ -3,20 +3,34 @@ import
|
||||||
preserves
|
preserves
|
||||||
|
|
||||||
type
|
type
|
||||||
XdgOpen* {.preservesRecord: "xdg-open".} = object
|
Open* {.preservesRecord: "open".} = object
|
||||||
`uri`*: string
|
`uri`*: Uri
|
||||||
|
|
||||||
UriRunnerConfig* {.preservesDictionary.} = object
|
UriRunnerConfig* {.preservesDictionary.} = object
|
||||||
`handlerspace`* {.preservesEmbedded.}: Preserve[void]
|
`handlerspace`* {.preservesEmbedded.}: Preserve[void]
|
||||||
`urispace`* {.preservesEmbedded.}: Preserve[void]
|
`urispace`* {.preservesEmbedded.}: Preserve[void]
|
||||||
|
|
||||||
|
Authority* {.preservesTuple.} = object
|
||||||
|
`username`*: Preserve[void]
|
||||||
|
`password`*: Preserve[void]
|
||||||
|
`host`*: Preserve[void]
|
||||||
|
`port`*: Preserve[void]
|
||||||
|
|
||||||
|
Uri* {.preservesRecord: "uri".} = object
|
||||||
|
`scheme`*: string
|
||||||
|
`auth`*: Authority
|
||||||
|
`path`*: Preserve[void]
|
||||||
|
`query`*: Preserve[void]
|
||||||
|
`fragment`*: Preserve[void]
|
||||||
|
|
||||||
ActionHandler* {.preservesRecord: "action-handler".} = object
|
ActionHandler* {.preservesRecord: "action-handler".} = object
|
||||||
`pat`*: string
|
`pat`*: string
|
||||||
`entity`* {.preservesEmbedded.}: Preserve[void]
|
`entity`* {.preservesEmbedded.}: Preserve[void]
|
||||||
`action`*: Preserve[void]
|
`action`*: Preserve[void]
|
||||||
|
|
||||||
proc `$`*(x: XdgOpen | UriRunnerConfig | ActionHandler): string =
|
proc `$`*(x: Open | UriRunnerConfig | Authority | Uri | ActionHandler): string =
|
||||||
`$`(toPreserve(x))
|
`$`(toPreserve(x))
|
||||||
|
|
||||||
proc encode*(x: XdgOpen | UriRunnerConfig | ActionHandler): seq[byte] =
|
proc encode*(x: Open | UriRunnerConfig | Authority | Uri | ActionHandler): seq[
|
||||||
|
byte] =
|
||||||
encode(toPreserve(x))
|
encode(toPreserve(x))
|
||||||
|
|
|
@ -1,42 +1,37 @@
|
||||||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||||
# SPDX-License-Identifier: Unlicense
|
# SPDX-License-Identifier: Unlicense
|
||||||
|
|
||||||
import std/[re, tables]
|
import std/[re, tables, uri]
|
||||||
import preserves, syndicate, syndicate/patterns
|
import preserves, syndicate, syndicate/relays, syndicate/patterns
|
||||||
import ./protocol
|
import ./protocol, ./common
|
||||||
|
|
||||||
# importing std/re doesn't actually link against PCRE
|
# importing std/re doesn't actually link against PCRE
|
||||||
{.passC: staticExec("pkg-config --cflags libpcre").}
|
{.passC: staticExec("pkg-config --cflags libpcre").}
|
||||||
{.passL: staticExec("pkg-config --libs libpcre").}
|
{.passL: staticExec("pkg-config --libs libpcre").}
|
||||||
|
|
||||||
type RegexAction = tuple[regex: Regex, entity: Ref, action: Assertion]
|
type RegexAction = tuple[regex: Regex, entity: Cap, action: Assertion]
|
||||||
|
|
||||||
proc rewrite(result: var Assertion; uri: string; regex: Regex) =
|
proc rewrite(result: var Assertion; uri: string; regex: Regex) {.gcsafe.} =
|
||||||
proc op(pr: var Assertion) =
|
proc op(pr: var Assertion) {.gcsafe.} =
|
||||||
if pr.isString:
|
if pr.isString:
|
||||||
pr.string = replacef(uri, regex, pr.string)
|
pr.string = replacef(uri, regex, pr.string)
|
||||||
apply(result, op)
|
apply(result, op)
|
||||||
|
|
||||||
runActor("main") do (root: Ref; turn: var Turn):
|
runActor("main") do (root: Cap; turn: var Turn):
|
||||||
connectStdio(root, turn)
|
connectStdio(turn, root)
|
||||||
during(turn, root, ?UriRunnerConfig) do (handlerspace: Ref, urispace: Ref):
|
let handlers = newTable[Handle, RegexAction]()
|
||||||
|
during(turn, root, ?UriRunnerConfig) do (handlerspace: Cap, urispace: Cap):
|
||||||
# sanity chek
|
|
||||||
onMessage(turn, handlerspace, ?XdgOpen) do (uri: string):
|
|
||||||
raiseAssert "got a message in the wrong dataspace!"
|
|
||||||
onPublish(turn, urispace, ?ActionHandler) do (pat: string; entity: Ref; act: Assertion):
|
|
||||||
raiseAssert "got an assertion in the wrong dataspace!"
|
|
||||||
|
|
||||||
during(turn, handlerspace, dropType(ActionHandler)) do:
|
during(turn, handlerspace, dropType(ActionHandler)) do:
|
||||||
|
|
||||||
var handlers: Table[Handle, RegexAction]
|
during(turn, handlerspace, ?ActionHandler) do (pat: string; entity: Cap; act: Assertion):
|
||||||
during(turn, handlerspace, ?ActionHandler) do (pat: string; entity: Ref; act: Assertion):
|
|
||||||
# `duringHandle` is a symbol exposed by the `during` macro
|
# `duringHandle` is a symbol exposed by the `during` macro
|
||||||
handlers[duringHandle] = (re(pat, {reIgnoreCase, reStudy}), entity, act,)
|
handlers[duringHandle] = (re(pat, {reIgnoreCase, reStudy}), entity, act,)
|
||||||
do:
|
do:
|
||||||
del(handlers, duringHandle)
|
del(handlers, duringHandle)
|
||||||
|
|
||||||
onMessage(turn, urispace, ?XdgOpen) do (uri: string):
|
onMessage(turn, urispace, Open ? {0:grab()}) do (u: uri.Uri):
|
||||||
|
let uri = $u
|
||||||
assert len(handlers) > 0
|
assert len(handlers) > 0
|
||||||
var matched = false
|
var matched = false
|
||||||
for handler in handlers.values:
|
for handler in handlers.values:
|
||||||
|
@ -50,5 +45,5 @@ runActor("main") do (root: Ref; turn: var Turn):
|
||||||
stderr.writeLine "rewrite failed on ", action
|
stderr.writeLine "rewrite failed on ", action
|
||||||
if not matched:
|
if not matched:
|
||||||
stderr.writeLine "no actions matched for ", uri
|
stderr.writeLine "no actions matched for ", uri
|
||||||
|
do:
|
||||||
|
clear(handlers)
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
|
||||||
# SPDX-License-Identifier: Unlicense
|
|
||||||
|
|
||||||
## Facsimile of xdg-open from xdg-utils.
|
|
||||||
## https://portland.freedesktop.org/doc/xdg-open.html
|
|
||||||
|
|
||||||
import std/[asyncdispatch, os]
|
|
||||||
import preserves, syndicate, syndicate/capabilities
|
|
||||||
import ./protocol
|
|
||||||
|
|
||||||
proc publishUri(turn: var Turn; ds: Ref) =
|
|
||||||
for arg in commandLineParams():
|
|
||||||
var xo = XdgOpen(uri: arg)
|
|
||||||
if fileExists(xo.uri):
|
|
||||||
xo.uri = "file://" & absolutePath(arg)
|
|
||||||
message(turn, ds, xo)
|
|
||||||
stop(turn)
|
|
||||||
|
|
||||||
proc unixSocketPath: Unix =
|
|
||||||
result.path = getEnv("SYNDICATE_SOCK")
|
|
||||||
if result.path == "":
|
|
||||||
result.path = getEnv("XDG_RUNTIME_DIR", "/run/user/1000") / "dataspace"
|
|
||||||
|
|
||||||
bootDataspace("xdg-open") do (root: Ref; turn: var Turn):
|
|
||||||
let cap = mint().toPreserve(Ref)
|
|
||||||
connect(turn, unixSocketPath(), cap, publishUri)
|
|
||||||
|
|
||||||
for i in 0..4: poll(20)
|
|
||||||
# A hack to exit
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
# Package
|
||||||
|
|
||||||
|
version = "20231021"
|
||||||
|
author = "Emery"
|
||||||
|
description = "Syndicated open command"
|
||||||
|
license = "Unlicense"
|
||||||
|
srcDir = "src"
|
||||||
|
bin = @[ "open", "uri_runner"]
|
||||||
|
|
||||||
|
|
||||||
|
# Dependencies
|
||||||
|
|
||||||
|
requires "nim >= 1.6.4", "syndicate >= 20231021"
|
|
@ -1,5 +1,3 @@
|
||||||
<require-service <daemon uri_runner>>
|
|
||||||
|
|
||||||
; configure the daemon when it is built
|
; configure the daemon when it is built
|
||||||
; (this is an artifact of the author's build system)
|
; (this is an artifact of the author's build system)
|
||||||
? <built uri_runner ?path ?hash> [
|
? <built uri_runner ?path ?hash> [
|
||||||
|
@ -10,12 +8,12 @@
|
||||||
}>
|
}>
|
||||||
]
|
]
|
||||||
|
|
||||||
; grab a dataspace for observing <xdg-open …> messages
|
; grab a dataspace for observing <open …> messages
|
||||||
? <socketspace ?socketspace> [
|
? <socketspace ?socketspace> [
|
||||||
|
|
||||||
; log xdg-open messages
|
; log open messages
|
||||||
$socketspace ?? <xdg-open ?stuff> [
|
$socketspace ?? <open ?stuff> [
|
||||||
$log ! <log "-" { line: <xdg-open $stuff> }>
|
$log ! <log "-" { line: <open $stuff> }>
|
||||||
]
|
]
|
||||||
|
|
||||||
; configure the uri_runner
|
; configure the uri_runner
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
# Package
|
|
||||||
|
|
||||||
version = "20230518"
|
|
||||||
author = "Emery"
|
|
||||||
description = "A better xdg-open"
|
|
||||||
license = "Unlicense"
|
|
||||||
srcDir = "src"
|
|
||||||
bin = @[ "uri_runner", "xdg_open"]
|
|
||||||
|
|
||||||
|
|
||||||
# Dependencies
|
|
||||||
|
|
||||||
requires "nim >= 1.6.4", "syndicate >= 20230518"
|
|
Loading…
Reference in New Issue