Highlight the input as it is typed, with fish's colors; bjoterm 0.2.3

Commands that exist are blue and others red; arguments cyan, underlined
when they name a path that exists; quotes, variables, operators,
redirections and comments each have their color.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 15:00:15 +01:00
parent 2636bbbc4e
commit dc0a4e5d2b
7 changed files with 235 additions and 4 deletions

View file

@ -31,6 +31,7 @@ non-trivial functions. No essays.
- `(bjosh pipeline)`: value commands next to each other, as one unit. - `(bjosh pipeline)`: value commands next to each other, as one unit.
- `(bjosh editor)`: when Enter runs a line, the completer (fish's - `(bjosh editor)`: when Enter runs a line, the completer (fish's
completions when fish is on PATH), and the suggester. completions when fish is on PATH), and the suggester.
- `(bjosh highlight)`: the highlighter of the input, with fish's colors.
- `(bjosh dirs)`: the visited directories, ranked by frecency, saved in - `(bjosh dirs)`: the visited directories, ranked by frecency, saved in
`$XDG_STATE_HOME/bjosh/dirs`. `$XDG_STATE_HOME/bjosh/dirs`.
- `(bjosh api)`: what a package of user commands imports: `Extension`, - `(bjosh api)`: what a package of user commands imports: `Extension`,

View file

@ -1,5 +1,5 @@
(lock (lock
(locked (locked
(package (name (bjoterm)) (version "0.2.2") (package (name (bjoterm)) (version "0.2.3")
(source (git (url "https://rikspucko.koketteriet.se/bjoli/bjoterm.git"))) (source (git (url "https://rikspucko.koketteriet.se/bjoli/bjoterm.git")))
(commit "4eef978b6b7862a2ac0901539d3e46c56db49d4ef4c502e8ca62960a8e4b3a85")))) (commit "3f6ccc3c39f67c7b75a0523c9863041034d0bb090daf4db569946cf74673a992"))))

View file

@ -10,5 +10,5 @@
;; The line editor and the terminal. To work on both at once, use ;; The line editor and the terminal. To work on both at once, use
;; (source (path (dir "../bjoterm"))) instead. ;; (source (path (dir "../bjoterm"))) instead.
(package (name (bjoterm)) (package (name (bjoterm))
(version (version-at-least "0.2.2")) (version (version-at-least "0.2.3"))
(source (git (url "https://rikspucko.koketteriet.se/bjoli/bjoterm.git")))))) (source (git (url "https://rikspucko.koketteriet.se/bjoli/bjoterm.git"))))))

View file

@ -23,7 +23,8 @@
(export (export
input-done? complete suggest word-start command-position? quote-name input-done? complete suggest word-start command-position? quote-name
file-candidates path-candidates abbreviated-candidates command-line-at) file-candidates path-candidates abbreviated-candidates command-line-at
command-exists? unquote-name expand-home)
;; Whether Enter runs `text`: #f while a quote is open, or the line ends ;; Whether Enter runs `text`: #f while a quote is open, or the line ends

188
src/highlight.bjo Normal file
View file

@ -0,0 +1,188 @@
;; 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 highlight): the highlighter of the line editor, with fish's
;; colors.
;;
;; - A command that exists is blue, one that does not red. A directory in
;; its place, which an interactive shell changes to, is blue.
;; - Arguments are cyan, and underlined when they name a file or directory
;; that exists.
;; - Quotes are yellow, $NAME, ${NAME} and $(...) bright cyan, |, &&, ||,
;; ; and & green, redirections bold cyan, and comments grey.
;; - The input need not parse: an open quote or $( runs to the end.
;; - After sudo, command, exec, time, nohup and doas, the next word that
;; is not an option is a command too. An option's argument is not known:
;; in `sudo -u root ls`, root is taken as the command.
(import (std prelude))
(import (bjoterm terminal))
(import (bjoterm line))
(import (bjosh shell))
(import (bjosh editor))
(export highlight)
(def command-style [(StyleFg (Ansi 4))])
(def error-style [(StyleFg (Ansi 9))])
(def param-style [(StyleFg (Ansi 6))])
(def quote-style [(StyleFg (Ansi 3))])
(def var-style [(StyleFg (Ansi 14))])
(def operator-style [(StyleFg (Ansi 2))])
(def redirect-style [(StyleFg (Ansi 6)) StyleBold])
(def comment-style [(StyleFg (Ansi 8))])
;; Redirections, longest first. Those with #f take no target.
(def redirections
[(Tuple "2>&1" #f) (Tuple "1>&2" #f) (Tuple ">&2" #f)
(Tuple "1>>" #t) (Tuple "2>>" #t) (Tuple ">>" #t)
(Tuple "0<" #t) (Tuple "1>" #t) (Tuple "2>" #t) (Tuple ">" #t) (Tuple "<" #t)])
(def operators ["&&" "||" "|" ";" "&"])
(def prefixes ["sudo" "command" "exec" "time" "nohup" "doas"])
(: (-> char bool))
(defun (blank? c) (or (= c #\space) (= c #\tab)))
(: (-> char bool))
(defun (name-char? c)
(or (char-alphabetic? c) (char-numeric? c) (= c #\_)))
;; Whether `word` is NAME=..., an assignment.
(: (-> string bool))
(defun (assignment? word)
(match (string-split-once word "=")
((Some (Tuple name _))
(and (not (string-empty? name)) (all? name-char? (string->vec name))))
(None #f)))
;; Whether `word`, as written without quotes, names a file or directory.
(: (-> string bool))
(defun (existing-path? word)
(def path (expand-home (unquote-name word)))
(and (not (string-empty? path))
(or (file-exists? path) (directory-exists? path))))
;; The highlights of `text`. See the module comment.
(: (-> Shell string (Vec Highlight)))
(defun (highlight sh text)
(def cs (string->vec text))
(def n (vec-length cs))
(def/mutable out [])
(defun (mark! from to style)
(when (< from to)
(set! out (vec-add out (Highlight (start from) (end to) (style style))))))
;; Whether `s` is at `i`.
(defun (at? i s)
(def ss (string->vec s))
(and (<= (+ i (vec-length ss)) n)
(let go ((k 0))
(or (= k (vec-length ss))
(and (= (vec-ref cs (+ i k)) (vec-ref ss k)) (go (+ k 1)))))))
(defun (op-char? c) (string-contains? "|&;<>\n" (char->string c)))
;; Where the $ at `i` ends.
(defun (var-end i)
(cond
((at? i "$(")
(let go ((k (+ i 2)) (depth 1))
(cond
((>= k n) n)
((= (vec-ref cs k) #\() (go (+ k 1) (+ depth 1)))
((= (vec-ref cs k) #\))
(if (= depth 1) (+ k 1) (go (+ k 1) (- depth 1))))
(else (go (+ k 1) depth)))))
((at? i "${")
(let go ((k (+ i 2)))
(cond
((>= k n) n)
((= (vec-ref cs k) #\}) (+ k 1))
(else (go (+ k 1))))))
((and (< (+ i 1) n) (string-contains? "?$0123456789" (char->string (vec-ref cs (+ i 1)))))
(+ i 2))
(else
(let go ((k (+ i 1)))
(if (and (< k n) (name-char? (vec-ref cs k))) (go (+ k 1)) k)))))
;; Scans the word at `i`, marking its quotes and variables in `inner`.
;; Returns where it ends and whether it is plain: no quotes or $.
(def/mutable inner [])
(defun (inner! from to style)
(set! inner (vec-add inner (Highlight (start from) (end to) (style style)))))
(defun (scan-word i)
(let go ((j i) (plain? #t))
(cond
((or (>= j n) (blank? (vec-ref cs j)) (op-char? (vec-ref cs j))) (Tuple j plain?))
((= (vec-ref cs j) #\\) (go (min n (+ j 2)) plain?))
((= (vec-ref cs j) #\')
(def close (let find ((k (+ j 1)))
(cond ((>= k n) n) ((= (vec-ref cs k) #\') (+ k 1)) (else (find (+ k 1))))))
(inner! j close quote-style)
(go close #f))
((= (vec-ref cs j) #\")
(def/mutable vars [])
(def close (let find ((k (+ j 1)))
(cond
((>= k n) n)
((= (vec-ref cs k) #\\) (find (+ k 2)))
((= (vec-ref cs k) #\") (+ k 1))
((= (vec-ref cs k) #\$)
(def e (var-end k))
(set! vars (vec-add vars (Tuple k e)))
(find e))
(else (find (+ k 1))))))
(inner! j (min close n) quote-style)
(for-each (fun (v) (inner! (fst v) (snd v) var-style)) vars)
(go (min close n) #f))
((= (vec-ref cs j) #\$)
(def e (var-end j))
(inner! j e var-style)
(go e #f))
(else (go (+ j 1) plain?)))))
(defun (redirection-at i)
(find (fun (r) (at? i (fst r))) redirections))
;; Marks the word from `i` by where it is, and returns where it ends and
;; whether a command comes next, and after a prefix.
(defun (word! i command? target? prefix?)
(set! inner [])
(def (Tuple e plain?) (scan-word i))
(def text (string-join (vec-map char->string (vec-slice cs i (- e i))) ""))
(def path-style (if (and plain? (existing-path? text)) (vec-add param-style StyleUnderline) param-style))
(def next
(cond
(target? (mark! i e path-style) (Tuple command? prefix?))
((and command? (assignment? text)) (mark! i e param-style) (Tuple #t prefix?))
((and command? prefix? (string-starts-with? text "-")) (mark! i e param-style) (Tuple #t #t))
(command?
(def exists? (or (not plain?)
(command-exists? sh (unquote-name text))
(and (or (string-contains? text "/") (= text ".") (= text ".."))
(directory-exists? (expand-home (unquote-name text))))))
(mark! i e (if exists? command-style error-style))
(Tuple (vec-contains? prefixes text) (vec-contains? prefixes text)))
(else (mark! i e path-style) (Tuple #f #f))))
(set! out (vec-append out inner))
(Tuple e next))
(let go ((i 0) (command? #t) (target? #f) (prefix? #f))
(cond
((>= i n) unit)
((blank? (vec-ref cs i)) (go (+ i 1) command? target? prefix?))
((= (vec-ref cs i) #\newline) (go (+ i 1) #t #f #f))
((= (vec-ref cs i) #\#)
(def e (let find ((k i)) (if (or (>= k n) (= (vec-ref cs k) #\newline)) k (find (+ k 1)))))
(mark! i e comment-style)
(go e command? target? prefix?))
(else
(match (Tuple (redirection-at i) (find (fun (o) (at? i o)) operators))
((Tuple (Some (Tuple op takes-target?)) _)
(def e (+ i (string-length op)))
(mark! i e redirect-style)
(go e command? takes-target? prefix?))
((Tuple None (Some op))
(def e (+ i (string-length op)))
(mark! i e operator-style)
(go e #t #f #f))
((Tuple None None)
(def (Tuple e (Tuple command-next? prefix-next?)) (word! i command? target? prefix?))
(go e command-next? #f prefix-next?))))))
out)

View file

@ -31,6 +31,8 @@
;; - An interactive shell keeps the directories it visits, ranked by ;; - An interactive shell keeps the directories it visits, ranked by
;; frecency, and its directory history, in $XDG_STATE_HOME/bjosh (see ;; frecency, and its directory history, in $XDG_STATE_HOME/bjosh (see
;; (bjosh dirs)). `cd KEYWORD...` goes to the best match. ;; (bjosh dirs)). `cd KEYWORD...` goes to the best match.
;; - The input is highlighted as it is typed, with fish's colors (see
;; (bjosh highlight)).
;; - On an empty line, Alt-Left and Alt-Right go back and forward in the ;; - On an empty line, Alt-Left and Alt-Right go back and forward in the
;; directory history, as `prevd` and `nextd`. ;; directory history, as `prevd` and `nextd`.
@ -43,6 +45,7 @@
(import (bjosh builtins)) (import (bjosh builtins))
(import (bjosh exec)) (import (bjosh exec))
(import (bjosh editor)) (import (bjosh editor))
(import (bjosh highlight))
(import (bjosh api)) (import (bjosh api))
(import (bjosh user)) (import (bjosh user))
@ -199,6 +202,10 @@
(match (box-ref current) (match (box-ref current)
((Some sh) (suggest sh text history)) ((Some sh) (suggest sh text history))
(None None)))) (None None))))
#:highlighter (Some (fun (text)
(match (box-ref current)
((Some sh) (highlight sh text))
(None []))))
#:command-key? (Some directory-key?) #:command-key? (Some directory-key?)
#:suspend? #f)) #:suspend? #f))
(def path (history-path)) (def path (history-path))

View file

@ -10,6 +10,8 @@
(import (std simpletest)) (import (std simpletest))
(import (bjoterm line)) (import (bjoterm line))
(import (bjosh editor)) (import (bjosh editor))
(import (bjosh highlight))
(import (bjoterm terminal))
(import (bjosh shell)) (import (bjosh shell))
(import (bjosh dirs)) (import (bjosh dirs))
(import (bjosh builtins)) (import (bjosh builtins))
@ -86,6 +88,38 @@
(any? (fun (d) (not (string-empty? d))) (record-ref c descriptions)))) (any? (fun (d) (not (string-empty? d))) (record-ref c descriptions))))
(Tuple #t #t))) (Tuple #t #t)))
;; 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)])
;; Suggestions. ;; Suggestions.
(ignore (directory-create (path-combine dir "abd/deeper"))) (ignore (directory-create (path-combine dir "abd/deeper")))
(ignore (directory-create (path-combine dir "other"))) (ignore (directory-create (path-combine dir "other")))