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:
parent
f8c4ad64bc
commit
84bd2f3382
11 changed files with 686 additions and 29 deletions
91
src/api.bjo
Normal file
91
src/api.bjo
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
;; (bjosh api): what a package of user commands imports. See plan.org,
|
||||
;; phase 4.
|
||||
;;
|
||||
;; - The package's module commands.bjo exports `bjosh-extension`, an
|
||||
;; `Extension`: value commands, shell commands and a prompt.
|
||||
;; - Value commands are as the built-in ones: a `ValueCommand` with a long
|
||||
;; and a short name, and a `Body` that reads bytes, values or nothing.
|
||||
;; They read with `next!`, write with `emit!`, and raise with
|
||||
;; `command-error`.
|
||||
;; - A shell command runs in the shell, alone on a line, as cd does. It gets
|
||||
;; a `ShellView` and its arguments, and returns a status.
|
||||
;; - The prompt function gets a `ShellView`, and returns the prompt. Escape
|
||||
;; sequences in it take no columns.
|
||||
|
||||
(import (std prelude))
|
||||
(import (text json))
|
||||
(import (bjosh values))
|
||||
(import (bjosh commands))
|
||||
|
||||
(export
|
||||
Extension ShellCommand ShellView JobInfo
|
||||
extension no-extension
|
||||
shell-status shell-var shell-set-var! shell-run shell-jobs)
|
||||
|
||||
(re-export
|
||||
ValueCommand Body Values next! emit! command-error
|
||||
record-of record-fields record? value-field value-line word->value)
|
||||
|
||||
;; A job of the shell: its number, its state as `jobs` writes it (Running,
|
||||
;; Stopped, Done, ...), and its command.
|
||||
(type (: JobInfo (Record (: job-number int) (: job-state string) (: job-command string))))
|
||||
|
||||
;; What a shell command and the prompt can see of the shell, and do in it.
|
||||
;; Read it with the functions below, not the fields.
|
||||
(type (: ShellView (Record (: get-status (-> int))
|
||||
(: get-var (-> string (Option string)))
|
||||
(: set-var (-> string string Unit))
|
||||
(: run-line (-> string int))
|
||||
(: list-jobs (-> (Vec JobInfo))))))
|
||||
|
||||
;; A shell command.
|
||||
;; - `name`: what the user writes.
|
||||
;; - `usage` and `help`: for `help`.
|
||||
;; - `run`: takes the shell and the arguments, and returns the status.
|
||||
(type (: ShellCommand (Record (: name string)
|
||||
(: usage string)
|
||||
(: help string)
|
||||
(: run (-> ShellView (Vec string) int)))))
|
||||
|
||||
;; What a package of user commands exports as `bjosh-extension`.
|
||||
(type (: Extension (Record (: commands (Vec ValueCommand))
|
||||
(: shell-commands (Vec ShellCommand))
|
||||
(: prompt (Option (-> ShellView string))))))
|
||||
|
||||
;; An `Extension`. Each part is empty unless given.
|
||||
(: (-> (#:commands (Vec ValueCommand)) (#:shell-commands (Vec ShellCommand))
|
||||
(#:prompt (Option (-> ShellView string)))
|
||||
Extension))
|
||||
(defun (extension #:commands [] #:shell-commands [] #:prompt None)
|
||||
(Extension (commands commands) (shell-commands shell-commands) (prompt prompt)))
|
||||
|
||||
;; The extension of a shell with no package of user commands.
|
||||
(: no-extension Extension)
|
||||
(def no-extension (extension))
|
||||
|
||||
;; The status of the last line, $?.
|
||||
(: (-> ShellView int))
|
||||
(defun (shell-status sh) ((record-ref sh get-status)))
|
||||
|
||||
;; The value of a variable: a shell variable, the environment, or one of
|
||||
;; ? $ and 0 to 9.
|
||||
(: (-> ShellView string (Option string)))
|
||||
(defun (shell-var sh name) ((record-ref sh get-var) name))
|
||||
|
||||
;; Sets a variable: in the environment when it is there, else as a shell
|
||||
;; variable, as NAME=value does.
|
||||
(: (-> ShellView string string Unit))
|
||||
(defun (shell-set-var! sh name value) ((record-ref sh set-var) name value))
|
||||
|
||||
;; Runs `line` as if it was typed, and returns its status. It writes to
|
||||
;; stdout and stderr as the line would.
|
||||
(: (-> ShellView string int))
|
||||
(defun (shell-run sh line) ((record-ref sh run-line) line))
|
||||
|
||||
;; The jobs, by number.
|
||||
(: (-> ShellView (Vec JobInfo)))
|
||||
(defun (shell-jobs sh) ((record-ref sh list-jobs)))
|
||||
|
|
@ -15,6 +15,8 @@
|
|||
(import (bjoterm line))
|
||||
(import (bjosh syntax))
|
||||
(import (bjosh shell))
|
||||
(import (bjosh api))
|
||||
(import (bjosh user))
|
||||
(import (bjosh commands))
|
||||
(import (bjosh values))
|
||||
(import (text json))
|
||||
|
|
@ -221,6 +223,14 @@
|
|||
((Some text) (println (alias-line arg text)) 0)
|
||||
(None (shell-error (str "alias: " arg ": not found")) 1))))))))
|
||||
|
||||
;; Builds the user's commands, and loads them again.
|
||||
(: (-> Shell (Vec string) int))
|
||||
(defun (reload sh args)
|
||||
(match (load-extension #t)
|
||||
(None (shell-error (str "reload: there are no user commands in " (commands-dir))) 1)
|
||||
((Some (Ok ext)) (set-extension! sh ext) 0)
|
||||
((Some (Err message)) (shell-error message) 1)))
|
||||
|
||||
(: (-> Shell (Vec string) int))
|
||||
(defun (unalias sh args)
|
||||
(each-status args
|
||||
|
|
@ -252,7 +262,15 @@
|
|||
(println (str name " is an alias for "
|
||||
(match (alias-ref sh name) ((Some t) t) (None ""))))
|
||||
0)
|
||||
((some? (user-shell-command sh name)) (println (str name " is a shell command of yours")) 0)
|
||||
((builtin? name) (println (str name " is a shell command")) 0)
|
||||
((some? (user-value-command sh name (fun (n) (some? (find-program n)))))
|
||||
(match (user-value-command sh name (fun (n) (some? (find-program n))))
|
||||
((Some c)
|
||||
(println (str name " is a value command of yours: "
|
||||
(record-ref c long) ", " (record-ref c short))))
|
||||
(None unit))
|
||||
0)
|
||||
(else
|
||||
(match (value-command name (fun (n) (some? (find-program n))))
|
||||
((Some c)
|
||||
|
|
@ -272,8 +290,21 @@
|
|||
(defun (value-help-line c)
|
||||
(str " " (string-pad-right (record-ref c usage) 26) (string-pad-right (record-ref c short) 5) (record-ref c help)))
|
||||
|
||||
(: (-> ShellCommand string))
|
||||
(defun (user-help-line c)
|
||||
(str " " (string-pad-right (record-ref c usage) 28) (record-ref c help)))
|
||||
|
||||
(: (-> Shell (Vec string) int))
|
||||
(defun (help sh args)
|
||||
(def ext (record-ref sh extension))
|
||||
(unless (vec-empty? (record-ref ext shell-commands))
|
||||
(println "Your shell commands:")
|
||||
(for-each (fun (c) (println (user-help-line c))) (record-ref ext shell-commands))
|
||||
(println ""))
|
||||
(unless (vec-empty? (record-ref ext commands))
|
||||
(println "Your value commands:")
|
||||
(for-each (fun (c) (println (value-help-line c))) (record-ref ext commands))
|
||||
(println ""))
|
||||
(println "Shell commands:")
|
||||
(for-each (fun (b) (println (builtin-help-line b))) (builtins))
|
||||
(println (str " " (string-pad-right "command NAME [ARG]..." 28) "run the program NAME, not an alias or a value command"))
|
||||
|
|
@ -312,7 +343,8 @@
|
|||
(Builtin (name "help") (usage "help") (help "this list") (run help))
|
||||
(Builtin (name "history") (usage "history") (help "the lines written before") (run history))
|
||||
(Builtin (name "alias") (usage "alias [NAME[=TEXT]]...") (help "list, show or set aliases; TEXT is one pipe") (run alias))
|
||||
(Builtin (name "unalias") (usage "unalias NAME...") (help "remove aliases") (run unalias))])
|
||||
(Builtin (name "unalias") (usage "unalias NAME...") (help "remove aliases") (run unalias))
|
||||
(Builtin (name "reload") (usage "reload") (help "build and load your commands again") (run reload))])
|
||||
|
||||
;; ===========================================================================
|
||||
;; Shell commands as values, in a pipe
|
||||
|
|
@ -360,18 +392,21 @@
|
|||
(body (OnNothing (bjoroutine (args out) (history-values sh args out)))))))
|
||||
(else None)))
|
||||
|
||||
(: (-> Builtin string))
|
||||
(defun (builtin-name b) (record-ref b name))
|
||||
|
||||
(: (-> string bool))
|
||||
(defun (builtin? name)
|
||||
(any? (fun (b) (= (record-ref b name) name)) (builtins)))
|
||||
(any? (fun (b) (= (builtin-name b) name)) (builtins)))
|
||||
|
||||
(: (Vec string))
|
||||
(def builtin-names (vec-map (fun (b) (record-ref b name)) (builtins)))
|
||||
(def builtin-names (vec-map builtin-name (builtins)))
|
||||
|
||||
;; Runs the shell command `name`. Returns its status; 127 when there is no
|
||||
;; such command.
|
||||
(: (-> Shell string (Vec string) int))
|
||||
(defun (run-builtin sh name args)
|
||||
(match (find (fun (b) (= (record-ref b name) name)) (builtins))
|
||||
(match (find (fun (b) (= (builtin-name b) name)) (builtins))
|
||||
((Some b)
|
||||
(def status ((record-ref b run) sh args))
|
||||
(flush-port (parameter-ref current-output-port))
|
||||
|
|
|
|||
50
src/exec.bjo
50
src/exec.bjo
|
|
@ -26,6 +26,9 @@
|
|||
;; - $(...) runs in a subshell (see `capture`). Its output is captured:
|
||||
;; programs write through a filter at the end of their form, and value
|
||||
;; commands write lines.
|
||||
;; - The user's commands (see (bjosh user)): a shell command of theirs runs
|
||||
;; as a shell command, before the shell's own. A value command of theirs
|
||||
;; comes before a built-in one of the same name.
|
||||
;; - >&2 copies stdout to stderr. It cannot be combined with another
|
||||
;; redirection of stdout or stderr.
|
||||
|
||||
|
|
@ -37,9 +40,10 @@
|
|||
(import (bjosh builtins))
|
||||
(import (bjosh commands))
|
||||
(import (bjosh pipeline))
|
||||
(import (bjosh api))
|
||||
(import (std os))
|
||||
|
||||
(export run-text expand-quoted)
|
||||
(export run-text expand-quoted shell-view)
|
||||
|
||||
;; A command after expansion. `program?`: it was `command NAME`, so NAME is
|
||||
;; a program, and `command` is not in `argv`.
|
||||
|
|
@ -228,7 +232,7 @@
|
|||
(def builtin-in-pipe
|
||||
(find (fun (e) (and (not (vec-empty? (record-ref e argv)))
|
||||
(not (record-ref e program?))
|
||||
(builtin? (vec-ref (record-ref e argv) 0))
|
||||
(shell-command? sh (vec-ref (record-ref e argv) 0))
|
||||
(not (some? (shell-value-command sh (vec-ref (record-ref e argv) 0))))))
|
||||
expanded))
|
||||
(cond
|
||||
|
|
@ -237,13 +241,17 @@
|
|||
((any? (fun (e) (vec-empty? (record-ref e argv))) expanded)
|
||||
(shell-error "a command in a pipe has no words")
|
||||
2)
|
||||
((and (= (vec-length expanded) 1) (not (record-ref first program?)) (builtin? name))
|
||||
((and (= (vec-length expanded) 1) (not (record-ref first program?)) (shell-command? sh name))
|
||||
(def args (vec-drop (record-ref first argv) 1))
|
||||
(cond
|
||||
(background? (shell-error (str name ": a shell command cannot run in the background")) 2)
|
||||
((not (and (vec-empty? (record-ref first assigns)) (vec-empty? (record-ref first redirects))))
|
||||
(shell-error (str name ": assignments and redirections of shell commands are not supported yet"))
|
||||
2)
|
||||
(else (run-builtin sh name (vec-slice (record-ref first argv) 1 (- (vec-length (record-ref first argv)) 1))))))
|
||||
(else
|
||||
(match (user-shell-command sh name)
|
||||
((Some c) (run-user-shell-command sh c args))
|
||||
(None (run-builtin sh name args))))))
|
||||
(else
|
||||
(match builtin-in-pipe
|
||||
((Some e) (shell-error (str (vec-ref (record-ref e argv) 0) ": a shell command cannot be in a pipe")) 2)
|
||||
|
|
@ -261,7 +269,39 @@
|
|||
(defun (stage-command sh name)
|
||||
(match (shell-value-command sh name)
|
||||
((Some c) (Some c))
|
||||
(None (value-command-of name))))
|
||||
(None
|
||||
(match (user-value-command sh name (fun (n) (some? (find-program n))))
|
||||
((Some c) (Some c))
|
||||
(None (value-command-of name))))))
|
||||
|
||||
;; What a user's shell command and prompt see of `sh`.
|
||||
(: (-> Shell ShellView))
|
||||
(defun (shell-view sh)
|
||||
(ShellView (get-status (fun () (record-ref sh status)))
|
||||
(get-var (fun (name) (var-ref sh name)))
|
||||
(set-var (fun (name value) (var-set! sh name value) unit))
|
||||
(run-line (fun (line) (run-text sh line)))
|
||||
(list-jobs (fun ()
|
||||
(vec-map (fun (e) (JobInfo (job-number (record-ref e number))
|
||||
(job-state (job-state-text e))
|
||||
(job-command (record-ref e text))))
|
||||
(record-ref sh jobs))))))
|
||||
|
||||
;; Runs the user's shell command `c`. A command that raises writes its
|
||||
;; message, and returns 1.
|
||||
(: (-> Shell ShellCommand (Vec string) int))
|
||||
(defun (run-user-shell-command sh c args)
|
||||
(def status
|
||||
(match (try ((record-ref c run) (shell-view sh) args) #:catch (System.Exception))
|
||||
((Ok status) status)
|
||||
((Err e) (shell-error (str (record-ref c name) ": " (.-Message e))) 1)))
|
||||
(flush-port (parameter-ref current-output-port))
|
||||
status)
|
||||
|
||||
;; Whether `name` is a shell command: the user's or the shell's own.
|
||||
(: (-> Shell string bool))
|
||||
(defun (shell-command? sh name)
|
||||
(or (some? (user-shell-command sh name)) (builtin? name)))
|
||||
|
||||
;; The value command of a stage: none for `command NAME`, and for echo
|
||||
;; with flags.
|
||||
|
|
|
|||
55
src/main.bjo
55
src/main.bjo
|
|
@ -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))))
|
||||
|
|
|
|||
|
|
@ -21,10 +21,12 @@
|
|||
(import (bjoterm line))
|
||||
(import (bjosh syntax))
|
||||
(import (bjosh expand))
|
||||
(import (bjosh api))
|
||||
|
||||
(export
|
||||
Shell JobEntry make-shell subshell shell-error
|
||||
alias-ref alias-set! alias-remove! set-subst-status!
|
||||
alias-ref alias-set! alias-remove! set-subst-status! set-extension! prompt-failed!
|
||||
user-value-command user-shell-command
|
||||
set-status! request-exit! warn-stopped! var-export!
|
||||
var-ref var-set! var-unset! shell-expander job-set-state!
|
||||
job-add! job-remove! job-find job-current job-mark-recent! jobs-stopped?
|
||||
|
|
@ -47,6 +49,9 @@
|
|||
;; commands write lines, not tables.
|
||||
;; - `subst-status`: the status of the last $(...) of the command being
|
||||
;; expanded. A command of only assignments returns it, as in POSIX.
|
||||
;; - `extension`: the user's commands and prompt (see (bjosh user)).
|
||||
;; - `prompt-failed?`: the user's prompt raised; it is not used until
|
||||
;; `reload`.
|
||||
(type (: Shell (Record (: vars (Map string string) #:mutable)
|
||||
(: status int #:mutable)
|
||||
(: jc (Option JobControl))
|
||||
|
|
@ -58,7 +63,9 @@
|
|||
(: warned-stopped? bool #:mutable)
|
||||
(: aliases (Map string string) #:mutable)
|
||||
(: capture? bool)
|
||||
(: subst-status (Option int) #:mutable))))
|
||||
(: subst-status (Option int) #:mutable)
|
||||
(: extension Extension #:mutable)
|
||||
(: prompt-failed? bool #:mutable))))
|
||||
|
||||
(import/extern
|
||||
(pid (: System.Environment.ProcessId int #:get #:no-raise)))
|
||||
|
|
@ -67,7 +74,8 @@
|
|||
(defun (make-shell jc editor args)
|
||||
(Shell (vars (map-empty)) (status 0) (jc jc) (editor editor) (args args)
|
||||
(jobs []) (recent []) (exit-code None) (warned-stopped? #f)
|
||||
(aliases (map-empty)) (capture? #f) (subst-status None)))
|
||||
(aliases (map-empty)) (capture? #f) (subst-status None)
|
||||
(extension no-extension) (prompt-failed? #f)))
|
||||
|
||||
;; A copy of `sh` for $(...): the same variables, aliases and status, which
|
||||
;; it can change for itself, and no jobs or job control.
|
||||
|
|
@ -76,11 +84,38 @@
|
|||
(Shell (vars (record-ref sh vars)) (status (record-ref sh status)) (jc None)
|
||||
(editor (record-ref sh editor)) (args (record-ref sh args))
|
||||
(jobs []) (recent []) (exit-code None) (warned-stopped? #f)
|
||||
(aliases (record-ref sh aliases)) (capture? #t) (subst-status None)))
|
||||
(aliases (record-ref sh aliases)) (capture? #t) (subst-status None)
|
||||
(extension (record-ref sh extension)) (prompt-failed? (record-ref sh prompt-failed?))))
|
||||
|
||||
(: (-> Shell (Option int) void))
|
||||
(defun (set-subst-status! sh status) (record-set! sh (subst-status status)))
|
||||
|
||||
(: (-> Shell Extension void))
|
||||
(defun (set-extension! sh ext)
|
||||
(record-set! sh (extension ext) (prompt-failed? #f)))
|
||||
|
||||
(: (-> Shell void))
|
||||
(defun (prompt-failed! sh) (record-set! sh (prompt-failed? #t)))
|
||||
|
||||
;; The user's value command of `name`: by its short name, or by its long
|
||||
;; name when `program?` says no program has that name (decision 4).
|
||||
(: (-> Shell string (-> string bool) (Option ValueCommand)))
|
||||
(defun (user-value-command sh name program?)
|
||||
(def commands (record-ref (record-ref sh extension) commands))
|
||||
(match (find (fun (c) (= (record-ref c short) name)) commands)
|
||||
((Some c) (Some c))
|
||||
(None (if (program? name)
|
||||
None
|
||||
(find (fun (c) (= (record-ref c long) name)) commands)))))
|
||||
|
||||
;; The user's shell command of `name`. exit, reload and command are always
|
||||
;; the shell's own.
|
||||
(: (-> Shell string (Option ShellCommand)))
|
||||
(defun (user-shell-command sh name)
|
||||
(if (vec-contains? ["exit" "reload" "command"] name)
|
||||
None
|
||||
(find (fun (c) (= (record-ref c name) name)) (record-ref (record-ref sh extension) shell-commands))))
|
||||
|
||||
(: (-> Shell string (Option string)))
|
||||
(defun (alias-ref sh name)
|
||||
(map-try-ref (record-ref sh aliases) name))
|
||||
|
|
|
|||
149
src/user.bjo
Normal file
149
src/user.bjo
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
;; (bjosh user): the package of user commands. See plan.org, phase 4.
|
||||
;;
|
||||
;; - Where: $XDG_CONFIG_HOME/bjosh/commands, or ~/.config/bjosh/commands. A
|
||||
;; Bjolang package whose module src/commands.bjo exports
|
||||
;; `bjosh-extension`, an `Extension` of (bjosh api).
|
||||
;; - Building: `bjo build` in the package, when a file of it is newer than
|
||||
;; src/commands.dll, or when asked (`reload`). A build that does nothing
|
||||
;; takes 0.4 s, so it is not run at each start.
|
||||
;; - Loading: with (std plugin). The package shares bjosh's modules, so its
|
||||
;; commands are of bjosh's own types.
|
||||
;; - Errors: a build that fails, or a load that fails, writes its message,
|
||||
;; and the extension stays as it was.
|
||||
|
||||
(import (std prelude))
|
||||
(import (std run))
|
||||
(import (std plugin))
|
||||
(import (bjosh api))
|
||||
|
||||
(export commands-dir load-extension write-example)
|
||||
|
||||
(import/extern
|
||||
(base-directory (: System.AppContext.BaseDirectory string #:get #:no-raise)))
|
||||
|
||||
;; The directory of the package of user commands.
|
||||
(: (-> string))
|
||||
(defun (commands-dir)
|
||||
(def config (match (getenv "XDG_CONFIG_HOME")
|
||||
((Some c) c)
|
||||
(None (path-combine (match (getenv "HOME") ((Some h) h) (None ".")) ".config"))))
|
||||
(path-combine config "bjosh/commands"))
|
||||
|
||||
(: (-> string long))
|
||||
(defun (modified-ms path)
|
||||
(match (file-info path)
|
||||
((Some info) (record-ref info modified-ms))
|
||||
(None 0L)))
|
||||
|
||||
;; Whether `dll` is older than the manifest or a .bjo file of `dir`.
|
||||
(: (-> string string bool))
|
||||
(defun (stale? dir dll)
|
||||
(def built (modified-ms dll))
|
||||
(or (= built 0L)
|
||||
(> (modified-ms (path-combine dir "manifest.bjodat")) built)
|
||||
(any? (fun (p) (and (string-ends-with? p ".bjo") (> (modified-ms p) built)))
|
||||
(->vec (map path->string/lossy (directory-walk (path-combine dir "src")))))))
|
||||
|
||||
;; Runs `bjo build` in `dir`. Returns the message of a failure.
|
||||
(: (-> string (Option string)))
|
||||
(defun (build dir)
|
||||
(match (run/output '(sh "-c" "cd \"$0\" && bjo build 2>&1" ,dir))
|
||||
((Ok (Tuple 0 _)) None)
|
||||
((Ok (Tuple _ out)) (Some (str "the user commands did not build:\n" (string-trim-right out))))
|
||||
((Err e) (Some (str "the user commands did not build: " (run-error-message e))))))
|
||||
|
||||
;; The extension of the package of user commands.
|
||||
;; - None when there is no package.
|
||||
;; - Builds it first when it is stale, or when `rebuild?`.
|
||||
;; - Returns the message of a build or a load that fails.
|
||||
(: (-> bool (Option (Result string Extension))))
|
||||
(defun (load-extension rebuild?)
|
||||
(def dir (commands-dir))
|
||||
(def dll (path-combine dir "src/commands.dll"))
|
||||
(if (not (file-exists? (path-combine dir "manifest.bjodat")))
|
||||
None
|
||||
(Some
|
||||
(match (if (or rebuild? (stale? dir dll)) (build dir) None)
|
||||
((Some message) (Err message))
|
||||
(None
|
||||
(: (Result PluginError Extension))
|
||||
(def loaded (plugin-load dll "bjosh-extension"))
|
||||
(match loaded
|
||||
((Ok ext) (Ok ext))
|
||||
((Err e) (Err (str "the user commands did not load: " (plugin-error-message e))))))))))
|
||||
|
||||
;; The directory of this bjosh's package: the parent of src, where main.exe
|
||||
;; is.
|
||||
(: (-> string))
|
||||
(defun (bjosh-dir)
|
||||
(path-absolute (path-combine base-directory "..")))
|
||||
|
||||
(: string)
|
||||
(def example-commands
|
||||
";; Your commands for bjosh. `reload` in bjosh builds and loads them.
|
||||
;; The types and functions are in (bjosh api): see its source.
|
||||
|
||||
(import (std prelude))
|
||||
(import (text json))
|
||||
(import (bjosh api))
|
||||
|
||||
(export bjosh-extension)
|
||||
|
||||
;; A value command: the values of its input, each twice.
|
||||
(: (-> (Vec string) Values Values Unit))
|
||||
(defbjo (twice args in out)
|
||||
(let go ()
|
||||
(match (next! in)
|
||||
(None unit)
|
||||
((Some v) (emit! out v) (emit! out v) (go)))))
|
||||
|
||||
;; A shell command, as a shell function would be: cd to a directory under
|
||||
;; ~/src.
|
||||
(: (-> ShellView (Vec string) int))
|
||||
(defun (src sh args)
|
||||
(shell-run sh (str \"cd ~/src/\" (if (vec-empty? args) \"\" (vec-ref args 0)))))
|
||||
|
||||
;; The prompt: the directory, and $ in red after a failure.
|
||||
(: (-> ShellView string))
|
||||
(defun (prompt sh)
|
||||
(def mark (if (= (shell-status sh) 0) \"$\" \"\\u{1B}[31m$\\u{1B}[0m\"))
|
||||
(str (current-directory) \" \" mark \" \"))
|
||||
|
||||
(: bjosh-extension Extension)
|
||||
(def bjosh-extension
|
||||
(extension
|
||||
#:commands [(ValueCommand (long \"twice\") (short \"tw\") (usage \"twice\")
|
||||
(help \"each value twice\")
|
||||
(body (OnValues (bjoroutine (args in out) (twice args in out)))))]
|
||||
#:shell-commands [(ShellCommand (name \"src\") (usage \"src [DIR]\")
|
||||
(help \"cd to ~/src/DIR\") (run src))]
|
||||
#:prompt (Some prompt)))
|
||||
")
|
||||
|
||||
;; Writes a package of user commands, with an example, to `commands-dir`.
|
||||
;; Returns the message of a failure: the directory is there already, or a
|
||||
;; file does not write.
|
||||
(: (-> (Option string)))
|
||||
(defun (write-example)
|
||||
(def dir (commands-dir))
|
||||
(def manifest
|
||||
(str "(package\n"
|
||||
" (name (bjosh-commands))\n"
|
||||
" (version \"0.1.0\")\n"
|
||||
" (description \"My commands for bjosh.\")\n"
|
||||
" (depends\n"
|
||||
" (package (name (bjosh)) (source (path (dir " (written (bjosh-dir)) "))))))\n"))
|
||||
(cond
|
||||
((directory-exists? dir) (Some (str dir ": there already")))
|
||||
(else
|
||||
(ignore (directory-create (path-combine dir "src")))
|
||||
(match (file-write-text (path-combine dir "manifest.bjodat") manifest)
|
||||
((Err e) (Some (fs-error-message e)))
|
||||
((Ok _)
|
||||
(match (file-write-text (path-combine dir "src/commands.bjo") example-commands)
|
||||
((Err e) (Some (fs-error-message e)))
|
||||
((Ok _) None)))))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue