Write the package of user commands on the first interactive start

When $XDG_CONFIG_HOME/bjosh/commands is not there, an interactive bjosh
writes it with the example, then builds and loads it. An empty directory
turns this off. --new-commands stays, for a start that is not interactive.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 10:57:19 +01:00
parent aa5bae96e0
commit bf961b12f9
4 changed files with 38 additions and 10 deletions

View file

@ -121,13 +121,14 @@
(go (+ j 1) out))))
(else (stringbuilder-add! out (vec-ref chars i)) (go (+ i 1) out)))))
;; Types lines into an interactive bjosh under `script`. Returns what it
;; writes, without escape sequences.
(: (-> (Vec string) string))
(defun (interactive lines)
(def keys (str "sleep 2.5; "
;; Types lines into an interactive bjosh under `script`, with `dir` as
;; XDG_CONFIG_HOME, after `wait` seconds. Returns what it writes, without
;; escape sequences.
(: (-> string string (Vec string) string))
(defun (interactive dir wait lines)
(def keys (str "sleep " wait "; "
(string-join (vec-map (fun (l) (str "printf '" l "\\r'; sleep 0.7")) lines) "; ")))
(def command (str "(" keys ") | XDG_STATE_HOME=" config " XDG_CONFIG_HOME=" config
(def command (str "(" keys ") | XDG_STATE_HOME=" dir " XDG_CONFIG_HOME=" dir
" script -qec 'dotnet src/main.exe' /dev/null"))
(match (run/output '(sh "-c" ,command))
((Ok (Tuple _ text)) (plain text))
@ -175,13 +176,21 @@
(if (not (file-exists? "/usr/bin/script"))
(println "skipped: the prompt (no script command)")
(let ((out (interactive ["true" "false" "true" "exit"])))
(let ((out (interactive config "2.5" ["true" "false" "true" "exit"])))
(expect-true "the prompt of the commands" (string-contains? out "[ext 0]> true"))
(expect-true "a prompt that raises writes its message"
(string-contains? out "bjosh: the prompt of your commands raised: the prompt failed"))
(expect "once" (vec-length (string-split out "the prompt failed")) 2)
(expect-true "and the default prompt is used after it" (not (string-contains? out "[ext 0]> exit")))))
(if (file-exists? "/usr/bin/script")
(let* ((fresh (path-combine config "fresh"))
(out (interactive fresh "12" ["echo a | twice" "exit"])))
(expect-true "the first interactive start writes the package"
(string-contains? out "bjosh: wrote your commands to "))
(expect-true "and builds and loads it" (string-contains? out "twice\na\na\n")))
unit)
(ignore (directory-delete-tree config))
(println "extension ok")
0)