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

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