Phase 4: commands in Bjolang

- A package of user commands in $XDG_CONFIG_HOME/bjosh/commands. Its
  module commands.bjo exports bjosh-extension: value commands, shell
  commands and a prompt function.
- (bjosh api): the types and functions a package imports.
- (bjosh user): builds the package when it is stale, and loads it with
  (std plugin) at each start. `reload` builds and loads it again.
- bjosh --new-commands writes a package with an example.
- No Native AOT, because it cannot load modules. bin/bjosh starts
  src/main.exe.
- tests/extension.bjo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 10:38:40 +01:00
parent f8c4ad64bc
commit 84bd2f3382
11 changed files with 686 additions and 29 deletions

View file

@ -8,12 +8,17 @@
;; terminal; else runs the lines of stdin
;; bjosh -c LINE runs LINE
;; 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)
;;
;; 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.
;; - The prompt is $PROMPT when it is set; see `custom-prompt`.
;; - Each bjosh loads the user's commands first, when there are any.
;; - The prompt is $PROMPT when it is set (see `custom-prompt`), else the
;; user's prompt function, else the directory and $.
(import (std prelude))
(import (std run))
@ -24,6 +29,8 @@
(import (bjosh builtins))
(import (bjosh exec))
(import (bjosh editor))
(import (bjosh api))
(import (bjosh user))
;; ===========================================================================
;; The loop
@ -53,15 +60,39 @@
(str "~" (string-drop dir (string-length home)))
dir))
;; The prompt: `custom-prompt` of $PROMPT, else the directory, and $ green
;; after status 0, else red.
;; The prompt: `custom-prompt` of $PROMPT, else the user's prompt function,
;; else the directory, and $ green after status 0, else red. A prompt
;; function that raises writes its message once, and is not used again
;; until `reload`.
(: (-> Shell string))
(defun (prompt sh)
(def user (record-ref (record-ref sh extension) prompt))
(match (var-ref sh "PROMPT")
((Some template) (custom-prompt sh template))
(None
(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 "))))
(match (if (record-ref sh prompt-failed?) None user)
((Some f)
(match (try (f (shell-view sh)) #:catch (System.Exception))
((Ok text) text)
((Err e)
(shell-error (str "the prompt of your commands raised: " (.-Message e)))
(prompt-failed! sh)
(default-prompt sh))))
(None (default-prompt sh))))))
(: (-> Shell string))
(defun (default-prompt sh)
(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 "))
;; Loads the user's commands into `sh`. A failure writes its message.
(: (-> Shell Shell))
(defun (with-extension sh)
(match (load-extension #f)
(None unit)
((Some (Ok ext)) (set-extension! sh ext))
((Some (Err message)) (shell-error message)))
sh)
(import/extern
(user-name (: System.Environment.UserName string #:get #:no-raise))
@ -125,7 +156,7 @@
(def path (history-path))
(ignore (line-history-load! ed path))
(ignore (try (directory-create (path-combine path "..")) #:catch (System.Exception)))
(def sh (make-shell jc (Some ed) ["bjosh"]))
(def sh (with-extension (make-shell jc (Some ed) ["bjosh"])))
(def rc (rc-path))
(when (file-exists? rc)
(match (file-read-text rc)
@ -153,17 +184,23 @@
(: (-> (Vec string) int))
(defun (run-file args)
(match (file-read-text (vec-ref args 0))
((Ok text) (run-script (make-shell None None args) text))
((Ok text) (run-script (with-extension (make-shell None None args)) text))
((Err e) (shell-error (fs-error-message e)) 127)))
(defun (main args)
(cond
((= args ["--new-commands"])
(match (write-example)
(None
(println (str "Wrote " (commands-dir) ". Edit src/commands.bjo there, and run reload in bjosh."))
0)
((Some message) (shell-error message) 1)))
((and (>= (vec-length args) 2) (= (vec-ref args 0) "-c"))
(run-script (make-shell None None (vec-add-first (vec-slice args 2 (- (vec-length args) 2)) "bjosh"))
(run-script (with-extension (make-shell None None (vec-add-first (vec-slice args 2 (- (vec-length args) 2)) "bjosh")))
(vec-ref args 1)))
((vec-empty? args)
(match (terminal-open)
((Ok term) (interactive term))
((Err _)
(run-script (make-shell None None ["bjosh"]) (read-all (parameter-ref current-input-port))))))
(run-script (with-extension (make-shell None None ["bjosh"])) (read-all (parameter-ref current-input-port))))))
(else (run-file args))))