Phase 3: $(...), aliases, a start-up file, PROMPT, Done lines, >&2
- $(...) runs in a subshell. A word that is only an unquoted $(...) is one word per line. X=$(cmd) sets $? to its status. - alias, unalias, and command NAME. An alias is one pipe. - ~/.config/bjosh/rc runs before the first prompt. - PROMPT, expanded as in double quotes, with \w \W \u \h \$ \e \n. - "Done" lines are written above the input while the prompt waits. - >&2, for programs and value commands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f54ceb35af
commit
f8c4ad64bc
13 changed files with 680 additions and 128 deletions
95
src/main.bjo
95
src/main.bjo
|
|
@ -10,6 +10,10 @@
|
|||
;; bjosh FILE ARG... runs the lines of FILE, with $0 FILE, $1 ARG...
|
||||
;;
|
||||
;; 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`.
|
||||
|
||||
(import (std prelude))
|
||||
(import (std run))
|
||||
|
|
@ -32,16 +36,84 @@
|
|||
(None (path-combine (match (getenv "HOME") ((Some h) h) (None ".")) ".local/state"))))
|
||||
(path-combine state "bjosh/history"))
|
||||
|
||||
;; The directory, with ~ for home, and $ green after status 0, else red.
|
||||
(: (-> Shell string))
|
||||
(defun (prompt sh)
|
||||
;; The start-up file.
|
||||
(: (-> string))
|
||||
(defun (rc-path)
|
||||
(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/rc"))
|
||||
|
||||
;; The directory, with ~ for home.
|
||||
(: (-> string))
|
||||
(defun (shown-directory)
|
||||
(def dir (current-directory))
|
||||
(def home (match (getenv "HOME") ((Some h) h) (None "")))
|
||||
(def shown (if (and (not (string-empty? home)) (string-starts-with? dir home))
|
||||
(str "~" (string-drop dir (string-length home)))
|
||||
dir))
|
||||
(def color (if (= (record-ref sh status) 0) "32" "31"))
|
||||
(str "\u{1B}[1;34m" shown "\u{1B}[0m \u{1B}[" color "m$\u{1B}[0m "))
|
||||
(if (and (not (string-empty? home)) (string-starts-with? dir home))
|
||||
(str "~" (string-drop dir (string-length home)))
|
||||
dir))
|
||||
|
||||
;; The prompt: `custom-prompt` of $PROMPT, else the directory, and $ green
|
||||
;; after status 0, else red.
|
||||
(: (-> Shell string))
|
||||
(defun (prompt sh)
|
||||
(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 "))))
|
||||
|
||||
(import/extern
|
||||
(user-name (: System.Environment.UserName string #:get #:no-raise))
|
||||
(machine-name (: System.Environment.MachineName string #:get #:no-raise)))
|
||||
|
||||
;; The text of a prompt escape: \w, \W, \u, \h, \$, \e, \n or \\.
|
||||
(: (-> char (Option string)))
|
||||
(defun (prompt-escape c)
|
||||
(cond
|
||||
((= c #\w) (Some (shown-directory)))
|
||||
((= c #\W) (Some (let ((dir (shown-directory)))
|
||||
(if (= dir "/") dir
|
||||
(match (path-filename dir) ((Some n) n) (None dir))))))
|
||||
((= c #\u) (Some user-name))
|
||||
((= c #\h) (Some machine-name))
|
||||
((= c #\$) (Some (if (= user-name "root") "#" "$")))
|
||||
((= c #\e) (Some "\u{1B}"))
|
||||
((= c #\n) (Some "\n"))
|
||||
((= c #\\) (Some "\\"))
|
||||
(else None)))
|
||||
|
||||
;; $PROMPT, with its escapes replaced (see `prompt-escape`), and the text
|
||||
;; between them expanded as in double quotes. A part that does not parse
|
||||
;; is shown as written.
|
||||
(: (-> Shell string string))
|
||||
(defun (custom-prompt sh template)
|
||||
(def chars (string->vec template))
|
||||
(def out (stringbuilder-empty))
|
||||
(def pending (stringbuilder-empty))
|
||||
(defun (flush!)
|
||||
(def text (stringbuilder->string pending))
|
||||
(stringbuilder-clear! pending)
|
||||
(unless (string-empty? text)
|
||||
(stringbuilder-add-string! out (match (expand-quoted sh text)
|
||||
((Ok expanded) expanded)
|
||||
((Err _) text)))))
|
||||
(let go ((i 0))
|
||||
(when (< i (vec-length chars))
|
||||
(def c (vec-ref chars i))
|
||||
(def escape (if (and (= c #\\) (< (+ i 1) (vec-length chars)))
|
||||
(prompt-escape (vec-ref chars (+ i 1)))
|
||||
None))
|
||||
(match escape
|
||||
((Some text)
|
||||
(flush!)
|
||||
(stringbuilder-add-string! out text)
|
||||
(go (+ i 2)))
|
||||
(None
|
||||
(stringbuilder-add! pending c)
|
||||
(go (+ i 1))))))
|
||||
(flush!)
|
||||
(stringbuilder->string out))
|
||||
|
||||
(: (-> Terminal int))
|
||||
(defun (interactive term)
|
||||
|
|
@ -54,9 +126,14 @@
|
|||
(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 rc (rc-path))
|
||||
(when (file-exists? rc)
|
||||
(match (file-read-text rc)
|
||||
((Ok text) (ignore (run-text sh text)))
|
||||
((Err e) (shell-error (str rc ": " (fs-error-message e))))))
|
||||
(let go ()
|
||||
(report-jobs sh)
|
||||
(match (line-read ed term (prompt sh))
|
||||
(match (line-read ed term (prompt sh) #:notices (Some (fun () (job-changes sh))))
|
||||
((LineText text)
|
||||
(ignore (line-history-save ed path))
|
||||
(ignore (run-text sh text)))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue