Phase 1: POSIX command lines, job control and the prompt
- (bjosh syntax): a part of POSIX sh, parsed with (text
parser-combinators). Syntax that is not supported raises an error that
names it.
- (bjosh expand): variables, ~ and globs. Values are not split.
- (bjosh exec): pipes as (std run) jobs, && || ; &, redirections, and
NAME=value through env.
- (bjosh shell) and (bjosh builtins): the job table and cd, pwd, exit,
export, unset, jobs, fg, bg, kill, type, help, history.
- (bjosh editor): Enter waits for open quotes and trailing operators;
completion of commands and files.
- -c LINE, FILE, and stdin. A Native AOT build too.
- Tests: the parser, expansion, the editor, lines through -c, and job
control under script.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 06:19:45 +01:00
|
|
|
;; 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 editor): when Enter runs a line, and completion.
|
|
|
|
|
;;
|
|
|
|
|
;; bjo test tests/editor.bjo
|
|
|
|
|
|
|
|
|
|
(import (std prelude))
|
|
|
|
|
(import (std simpletest))
|
|
|
|
|
(import (bjoterm line))
|
|
|
|
|
(import (bjosh editor))
|
2026-12-01 15:00:15 +01:00
|
|
|
(import (bjosh highlight))
|
|
|
|
|
(import (bjoterm terminal))
|
2026-12-01 13:30:23 +01:00
|
|
|
(import (bjosh shell))
|
|
|
|
|
(import (bjosh dirs))
|
|
|
|
|
(import (bjosh builtins))
|
Phase 1: POSIX command lines, job control and the prompt
- (bjosh syntax): a part of POSIX sh, parsed with (text
parser-combinators). Syntax that is not supported raises an error that
names it.
- (bjosh expand): variables, ~ and globs. Values are not split.
- (bjosh exec): pipes as (std run) jobs, && || ; &, redirections, and
NAME=value through env.
- (bjosh shell) and (bjosh builtins): the job table and cd, pwd, exit,
export, unset, jobs, fg, bg, kill, type, help, history.
- (bjosh editor): Enter waits for open quotes and trailing operators;
completion of commands and files.
- -c LINE, FILE, and stdin. A Native AOT build too.
- Tests: the parser, expansion, the editor, lines through -c, and job
control under script.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 06:19:45 +01:00
|
|
|
|
|
|
|
|
(import/extern
|
|
|
|
|
(temp-dir (: System.IO.Path.GetTempPath (-> string)))
|
|
|
|
|
(process-id (: System.Environment.ProcessId int #:get #:no-raise)))
|
|
|
|
|
|
|
|
|
|
(defun (main args)
|
|
|
|
|
(expect "a whole line runs" (input-done? "ls -l") #t)
|
|
|
|
|
(expect "an open quote waits" (input-done? "echo 'abc") #f)
|
|
|
|
|
(expect "an open double quote waits" (input-done? "echo \"abc") #f)
|
|
|
|
|
(expect "| at the end waits" (input-done? "ls |") #f)
|
|
|
|
|
(expect "&& at the end waits" (input-done? "make &&") #f)
|
|
|
|
|
(expect "\\ at the end waits" (input-done? "echo a \\") #f)
|
|
|
|
|
(expect "\\\\ at the end runs" (input-done? "echo a \\\\") #t)
|
|
|
|
|
(expect "a line that is not supported runs, to show the error" (input-done? "if true") #t)
|
|
|
|
|
(expect "two lines" (input-done? "echo 'a\nb'") #t)
|
|
|
|
|
|
|
|
|
|
(expect "the word before the cursor" (word-start "ls -l fo") 6)
|
|
|
|
|
(expect "after an operator" (word-start "a|b") 2)
|
|
|
|
|
(expect "the first word is a command" (command-position? "ls" 0) #t)
|
|
|
|
|
(expect "the second is not" (command-position? "ls -l" 3) #f)
|
|
|
|
|
(expect "after | it is" (command-position? "a | b" 4) #t)
|
|
|
|
|
(expect "after && it is" (command-position? "a && b" 5) #t)
|
|
|
|
|
(expect "spaces are quoted" (quote-name "a b") "a\\ b")
|
|
|
|
|
(expect "and $" (quote-name "$x") "\\$x")
|
|
|
|
|
|
|
|
|
|
(def dir (path-combine (temp-dir) (str "bjosh-editor-" (int->string process-id))))
|
|
|
|
|
(ignore (directory-create (path-combine dir "abd")))
|
|
|
|
|
(ignore (file-write-text (path-combine dir "abc.txt") ""))
|
|
|
|
|
(ignore (file-write-text (path-combine dir "a b") ""))
|
|
|
|
|
(ignore (file-write-text (path-combine dir ".hidden") ""))
|
|
|
|
|
(ignore (file-write-text (path-combine dir "abd/inner") ""))
|
|
|
|
|
(def here (current-directory))
|
|
|
|
|
(set-current-directory! dir)
|
|
|
|
|
(expect "files and directories" (file-candidates "ab") ["abc.txt " "abd/"])
|
|
|
|
|
(expect "not hidden files" (file-candidates "") ["a\\ b " "abc.txt " "abd/"])
|
|
|
|
|
(expect "hidden files for a dot" (file-candidates ".h") [".hidden "])
|
|
|
|
|
(expect "in a directory" (file-candidates "abd/i") ["abd/inner "])
|
2026-12-01 13:30:23 +01:00
|
|
|
(set-environment-variable! "XDG_STATE_HOME" (path-combine dir "state"))
|
|
|
|
|
(def sh (make-shell None None ["bjosh"]))
|
|
|
|
|
(defun (cands text) (record-ref (complete sh text) candidates))
|
|
|
|
|
;; The ghost and the line of a suggestion.
|
|
|
|
|
(defun (sg text history)
|
|
|
|
|
(match (suggest sh text history)
|
|
|
|
|
((Some s) (Some (Tuple (record-ref s ghost) (record-ref s line))))
|
|
|
|
|
(None None)))
|
|
|
|
|
(defun (g text history)
|
|
|
|
|
(match (sg text history) ((Some t) (Some (fst t))) (None None)))
|
|
|
|
|
|
|
|
|
|
;; Completion.
|
|
|
|
|
(expect "the second word completes files" (cands "cat ab") ["abc.txt " "abd/"])
|
|
|
|
|
(expect "the first word completes commands" (any? (fun (c) (= c "jobs ")) (cands "jo")) #t)
|
|
|
|
|
(expect "with their help"
|
|
|
|
|
(let ((c (complete sh "prevd")))
|
|
|
|
|
(Tuple (record-ref c candidates) (record-ref c descriptions)))
|
|
|
|
|
(Tuple ["prevd "] ["go back in the directory history; also Alt-Left"]))
|
2026-12-01 14:01:43 +01:00
|
|
|
(expect "and directories" (complete sh "ab")
|
|
|
|
|
(Completion (prefix "ab") (candidates ["abd/"]) (descriptions ["directory"])))
|
|
|
|
|
(expect "ignoring case" (cands "AB") ["abd/"])
|
2026-12-01 13:30:23 +01:00
|
|
|
(expect "a first word with a / completes files" (cands "./ab") ["./abc.txt " "./abd/"])
|
|
|
|
|
(expect "a path ignoring case when nothing else matches" (cands "./ABC") ["./abc.txt "])
|
|
|
|
|
(expect "each part as the start of a name" (cands "ls ./a/in") ["./abd/inner "])
|
|
|
|
|
(expect "abbreviated" (abbreviated-candidates "a/i") ["abd/inner "])
|
|
|
|
|
(expect "variables" (vec-contains? (cands "echo $HOM") "$HOME ") #t)
|
|
|
|
|
(expect "the command at the end" (command-line-at "ls | git ch") "git ch")
|
|
|
|
|
(expect "not inside quotes" (command-line-at "echo 'a|b' x") "echo 'a|b' x")
|
|
|
|
|
(expect "after &&" (command-line-at "make && ./run -") "./run -")
|
|
|
|
|
(when (some? (find-program "fish"))
|
|
|
|
|
(expect "fish's completions, with descriptions"
|
|
|
|
|
(let ((c (complete sh "systemctl sta")))
|
|
|
|
|
(Tuple (vec-contains? (record-ref c candidates) "start ")
|
|
|
|
|
(any? (fun (d) (not (string-empty? d))) (record-ref c descriptions))))
|
|
|
|
|
(Tuple #t #t)))
|
2026-12-01 13:10:06 +01:00
|
|
|
|
2026-12-01 15:00:15 +01:00
|
|
|
;; Highlighting: (start end style) of each part.
|
|
|
|
|
(defun (hl text)
|
|
|
|
|
(vec-map (fun (h) (Tuple (record-ref h start) (record-ref h end) (record-ref h style))) (highlight sh text)))
|
|
|
|
|
(def blue [(StyleFg (Ansi 4))])
|
|
|
|
|
(def red [(StyleFg (Ansi 9))])
|
|
|
|
|
(def cyan [(StyleFg (Ansi 6))])
|
|
|
|
|
(expect "a command that exists, and an argument"
|
|
|
|
|
(hl "ls -l") [(Tuple 0 2 blue) (Tuple 3 5 cyan)])
|
|
|
|
|
(expect "a command that does not exist"
|
|
|
|
|
(hl "nosuch-command-x") [(Tuple 0 16 red)])
|
|
|
|
|
(expect "a path that exists is underlined"
|
|
|
|
|
(hl "cat abc.txt") [(Tuple 0 3 blue) (Tuple 4 11 (vec-add cyan StyleUnderline))])
|
|
|
|
|
(expect "quotes and variables"
|
|
|
|
|
(hl "echo 'a' \"$HOME x\"")
|
|
|
|
|
[(Tuple 0 4 blue) (Tuple 5 8 cyan) (Tuple 5 8 [(StyleFg (Ansi 3))])
|
|
|
|
|
(Tuple 9 18 cyan) (Tuple 9 18 [(StyleFg (Ansi 3))]) (Tuple 10 15 [(StyleFg (Ansi 14))])])
|
|
|
|
|
(expect "operators start a command"
|
|
|
|
|
(hl "ls | nosuch-command-x")
|
|
|
|
|
[(Tuple 0 2 blue) (Tuple 3 4 [(StyleFg (Ansi 2))]) (Tuple 5 21 red)])
|
|
|
|
|
(expect "redirections, and their targets"
|
|
|
|
|
(hl "ls >out 2>&1")
|
|
|
|
|
[(Tuple 0 2 blue) (Tuple 3 4 [(StyleFg (Ansi 6)) StyleBold]) (Tuple 4 7 cyan)
|
|
|
|
|
(Tuple 8 12 [(StyleFg (Ansi 6)) StyleBold])])
|
|
|
|
|
(expect "an open quote runs to the end"
|
|
|
|
|
(hl "echo 'ab") [(Tuple 0 4 blue) (Tuple 5 8 cyan) (Tuple 5 8 [(StyleFg (Ansi 3))])])
|
|
|
|
|
(expect "a comment" (hl "ls # x") [(Tuple 0 2 blue) (Tuple 3 6 [(StyleFg (Ansi 8))])])
|
|
|
|
|
(expect "the command after sudo and its options"
|
|
|
|
|
(hl "sudo -E ls") [(Tuple 0 4 blue) (Tuple 5 7 cyan) (Tuple 8 10 blue)])
|
|
|
|
|
(expect "an assignment before a command"
|
|
|
|
|
(hl "X=1 ls") [(Tuple 0 3 cyan) (Tuple 4 6 blue)])
|
|
|
|
|
(expect "a directory in a command's place" (hl "abd/") [(Tuple 0 4 blue)])
|
|
|
|
|
|
2026-12-01 13:10:06 +01:00
|
|
|
;; Suggestions.
|
|
|
|
|
(ignore (directory-create (path-combine dir "abd/deeper")))
|
|
|
|
|
(ignore (directory-create (path-combine dir "other")))
|
2026-12-01 13:30:23 +01:00
|
|
|
(ignore (directory-create (path-combine dir "Mixed")))
|
|
|
|
|
(expect "cd suggests the only directory" (g "cd ot" []) (Some "her/"))
|
|
|
|
|
(expect "and not files" (g "cd abc" []) None)
|
|
|
|
|
(expect "the common prefix of several" (g "cd abd/" []) (Some "deeper/"))
|
|
|
|
|
(expect "a directory ignoring case replaces the word" (sg "cd mix" []) (Some (Tuple "ed/" "cd Mixed/")))
|
|
|
|
|
(expect "a path argument suggests files, with no space" (g "cat abc" []) (Some ".txt"))
|
|
|
|
|
(expect "several files, their common prefix" (g "cat a" []) None)
|
|
|
|
|
(expect "in a directory" (g "cat abd/in" []) (Some "ner"))
|
|
|
|
|
(expect "not the command" (g "ab" []) None)
|
|
|
|
|
(expect "not an empty argument" (g "cat " []) None)
|
|
|
|
|
(expect "not a second line" (g "echo 'a\ncat abc" []) None)
|
2026-12-01 13:10:06 +01:00
|
|
|
(expect "cd from the history, newest first"
|
2026-12-01 13:30:23 +01:00
|
|
|
(g "cd a" ["cd abd" "cd abd/deeper" "ls"]) (Some "bd/deeper"))
|
2026-12-01 13:10:06 +01:00
|
|
|
(expect "not a directory that is gone"
|
2026-12-01 13:30:23 +01:00
|
|
|
(g "cd o" ["cd other" "cd old-gone"]) (Some "ther"))
|
|
|
|
|
(expect "only cd lines" (g "cd o" ["cdx other"]) (Some "ther/"))
|
|
|
|
|
(expect "any line from the history" (g "ec" ["echo hi"]) (Some "ho hi"))
|
|
|
|
|
(expect "the newest" (g "ec" ["echo hi" "echo ho"]) (Some "ho ho"))
|
|
|
|
|
(expect "not one whose command is gone" (g "nosuch" ["nosuch-command-x a"]) None)
|
|
|
|
|
(expect "ignoring case, it replaces the line" (sg "EC" ["echo hi"]) (Some (Tuple "ho hi" "echo hi")))
|
|
|
|
|
(defun (visit! path)
|
|
|
|
|
(set-current-directory! path)
|
|
|
|
|
(dir-arrived! sh)
|
|
|
|
|
(set-current-directory! dir))
|
|
|
|
|
(visit! (path-combine dir "other"))
|
|
|
|
|
(visit! (path-combine dir "other"))
|
|
|
|
|
(visit! (path-combine dir "abd/deeper"))
|
|
|
|
|
(expect "cd from the history by frecency" (g "cd " ["cd other" "cd abd/deeper"]) (Some "other"))
|
|
|
|
|
(set-current-directory! (path-combine dir "other"))
|
|
|
|
|
(def deeper (path-combine dir "abd/deeper"))
|
|
|
|
|
(expect "a visited directory the words match"
|
|
|
|
|
(sg "cd deep" []) (Some (Tuple (str " \u{2192} " deeper) (str "cd " deeper))))
|
|
|
|
|
(expect "with several words" (g "cd abd dee" []) (Some (str " \u{2192} " deeper)))
|
|
|
|
|
(expect "not the directory you are in" (g "cd oth" []) None)
|
|
|
|
|
(expect "Tab after cd completes them too" (cands "cd deep") [(str deeper "/")])
|
|
|
|
|
(set-current-directory! dir)
|
Phase 1: POSIX command lines, job control and the prompt
- (bjosh syntax): a part of POSIX sh, parsed with (text
parser-combinators). Syntax that is not supported raises an error that
names it.
- (bjosh expand): variables, ~ and globs. Values are not split.
- (bjosh exec): pipes as (std run) jobs, && || ; &, redirections, and
NAME=value through env.
- (bjosh shell) and (bjosh builtins): the job table and cd, pwd, exit,
export, unset, jobs, fg, bg, kill, type, help, history.
- (bjosh editor): Enter waits for open quotes and trailing operators;
completion of commands and files.
- -c LINE, FILE, and stdin. A Native AOT build too.
- Tests: the parser, expansion, the editor, lines through -c, and job
control under script.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 06:19:45 +01:00
|
|
|
(set-current-directory! here)
|
|
|
|
|
(ignore (directory-delete-tree dir))
|
|
|
|
|
0)
|