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

@ -433,8 +433,11 @@ User commands, compiled, loaded into the shell at start-up. Needs
type checker could not tell the records apart in a lambda.
- A package that does not build is stale at each start, so each start
builds it again and shows the error, until it is fixed.
- The first interactive start writes the package with its example, when
its directory is not there; an empty directory turns this off.
~--new-commands~ stays, for a start that is not interactive.
- Measured: 132 ms for ~bjosh -c true~ with the example package loaded.
- Tests: ~tests/extension.bjo~, 23 checks. The other tests: syntax 72,
- Tests: ~tests/extension.bjo~, 25 checks. The other tests: syntax 72,
expand 26, values 59, editor 24, run 60, interactive 16.
** Phase 5: later

View file

@ -10,13 +10,17 @@
;; bjosh FILE ARG... runs the lines of FILE, with $0 FILE, $1 ARG...
;; bjosh --new-commands
;; writes a package of user commands, with an
;; example; see (bjosh user)
;; example; see (bjosh user). The first interactive
;; start does this too.
;;
;; The exit code is the status of the last line, or the code of `exit`.
;;
;; - An interactive shell first runs the lines of the start-up file,
;; $XDG_CONFIG_HOME/bjosh/rc or ~/.config/bjosh/rc.
;; - Each bjosh loads the user's commands first, when there are any.
;; - An interactive bjosh writes the package of user commands, with an
;; example, when its directory is not there. An empty directory there
;; turns this off.
;; - The prompt is $PROMPT when it is set (see `custom-prompt`), else the
;; user's prompt function, else the directory and $.
@ -85,6 +89,16 @@
(def color (if (= (record-ref sh status) 0) "32" "31"))
(str "\u{1B}[1;34m" (shown-directory) "\u{1B}[0m \u{1B}[" color "m$\u{1B}[0m "))
;; Writes the package of user commands, with its example, when its
;; directory is not there. For an interactive start.
(: (-> void))
(defun (set-up-commands)
(unless (directory-exists? (commands-dir))
(match (write-example)
(None (println (str "bjosh: wrote your commands to " (commands-dir) "/src/commands.bjo."
" Edit them there, then run reload. Building them the first time...")))
((Some message) (shell-error message)))))
;; Loads the user's commands into `sh`. A failure writes its message.
(: (-> Shell Shell))
(defun (with-extension sh)
@ -156,6 +170,7 @@
(def path (history-path))
(ignore (line-history-load! ed path))
(ignore (try (directory-create (path-combine path "..")) #:catch (System.Exception)))
(set-up-commands)
(def sh (with-extension (make-shell jc (Some ed) ["bjosh"])))
(def rc (rc-path))
(when (file-exists? rc)

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)

View file

@ -39,7 +39,8 @@
((not (file-exists? "src/main.exe")) (println "FAILURE: src/main.exe is missing: run bjo build first"))
(else
(def state (path-combine (temp-dir) (str "bjosh-interactive-" (int->string process-id))))
(ignore (directory-create (path-combine state "bjosh")))
;; An empty commands directory: no package of user commands is written.
(ignore (directory-create (path-combine state "bjosh/commands")))
(ignore (file-write-text (path-combine state "bjosh/rc") "alias fromrc='echo set in rc'\n"))
(def keys (string-join ["sleep 2.5"
"printf 'sleep 30\\r'" "sleep 1" "printf '\\032'" "sleep 0.5"