From bf961b12f9c1571312862fbf900eb77be94a3403d39e74f743d39109d91a4bfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Bj=C3=B6rnstam?= Date: Tue, 1 Dec 2026 10:57:19 +0100 Subject: [PATCH] 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 --- plan.org | 5 ++++- src/main.bjo | 17 ++++++++++++++++- tests/extension.bjo | 23 ++++++++++++++++------- tests/interactive.bjo | 3 ++- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/plan.org b/plan.org index ec90257..caa8d86 100644 --- a/plan.org +++ b/plan.org @@ -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 diff --git a/src/main.bjo b/src/main.bjo index 5d65fa5..6162f5b 100644 --- a/src/main.bjo +++ b/src/main.bjo @@ -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) diff --git a/tests/extension.bjo b/tests/extension.bjo index 2adc77c..a02b606 100644 --- a/tests/extension.bjo +++ b/tests/extension.bjo @@ -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) diff --git a/tests/interactive.bjo b/tests/interactive.bjo index 2400aac..77f8feb 100644 --- a/tests/interactive.bjo +++ b/tests/interactive.bjo @@ -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"