commit 481b92bf163536c12074a5bf08417b929173d375
parent 74a0533784c66a32e4ee3c26a4e075a52475246a
Author: lou woell <lou@repetitions.de>
Date: Sun, 22 Mar 2026 01:43:03 +0100
add simple dmenu interface
Diffstat:
| M | linkhut-guile.scm | | | 64 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- |
1 file changed, 61 insertions(+), 3 deletions(-)
diff --git a/linkhut-guile.scm b/linkhut-guile.scm
@@ -34,6 +34,8 @@
;; readline
(ice-9 rdelim)
+ (rnrs io ports)
+
(web uri)
(web response)
(web client)
@@ -43,17 +45,26 @@
(sxml xpath))
;; TODO: make this a bit more user friendly
-(define token (call-with-input-file "./token" read-line))
+(define token (call-with-input-file "./token" read-line))
+(define prompt "Bookmark: ")
+
(define host "api.ln.ht")
(define api-version "v1")
(define post-url "posts")
(define tags-url "tags")
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;; utils
+;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
(define (encode-query-val val)
(cond ((string? val) (uri-encode val))
((list? val) (string-join (map uri-encode val) "+"))))
-(define (query-part parts)
+(define (make-query-part parts)
(let ((key (symbol->string (car parts)))
(val (cdr parts)))
(if val
@@ -63,7 +74,7 @@
"")))
(define (build-query parts)
- (string-join (cons "?" (map query-part parts)) "&"))
+ (string-join (cons "?" (map make-query-part parts)) "&"))
(define* (call-linkhut path #:optional (query #f))
(let-values
@@ -126,6 +137,18 @@
(list 'update (timestamp t) (string= "done" c) i))))
+
+;; Gets value of property PROP of POST.
+(define (get-post-value prop post)
+ (cdr (assoc prop (cdr post))))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;; linkhut api
+;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
;; posts update
;;
;; Returns the most recent time a bookmark was added, updated or deleted.
@@ -365,3 +388,38 @@
(call-linkhut (list tags-url "delete")
`((old . ,old)
(new . ,new))))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;; menu
+;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; (post ...) -> ((title . link) ...)
+(define (show posts)
+ (map (lambda (post) (cons (get-post-value 'title post)
+ (get-post-value 'link post)))
+ posts))
+
+;; Spawn a dmenu let the user select an entry and return it. ENTRIES is assumed
+;; to be an alist with string keys, which will be shown to the user.
+(define (menu entries)
+ (let* ((in-pipe (pipe))
+ (out-pipe (pipe))
+ (pid (spawn "dmenu" `("dmenu" "-p" ,prompt)
+ #:input (car in-pipe)
+ #:output (cdr out-pipe))))
+ (close-port (cdr out-pipe))
+
+ (map (lambda (entry) (write-line (car entry) (cdr in-pipe)))
+ entries)
+ (close-port (cdr in-pipe))
+
+ (let ((result (read-line (car out-pipe))))
+ (close-port (car out-pipe))
+ (display (/ (cdr (waitpid pid))
+ 256))
+ (newline)
+
+ (assoc result entries))))