Suggestions for cd and paths, and a directory history; bjoterm 0.1.2

The editor suggests, as fish does, the newest cd line of the history whose
directory exists, else the directories or files that start with the word.
prevd, nextd and cdh move in the directory history, and so do Alt-Left and
Alt-Right on an empty line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 13:10:06 +01:00
parent 77c6d7977a
commit 5012a5d993
8 changed files with 262 additions and 14 deletions

View file

@ -52,6 +52,24 @@
(any? (fun (c) (= c "jobs ")) (record-ref (complete "jo") candidates)) #t)
(expect "a first word with a / completes files" (record-ref (complete "./ab") candidates)
["./abc.txt " "./abd/"])
;; Suggestions.
(ignore (directory-create (path-combine dir "abd/deeper")))
(ignore (directory-create (path-combine dir "other")))
(expect "cd suggests the only directory" (suggest "cd ot" []) (Some "her/"))
(expect "and not files" (suggest "cd abc" []) None)
(expect "the common prefix of several" (suggest "cd abd/" []) (Some "deeper/"))
(expect "a path argument suggests files, with no space" (suggest "cat abc" []) (Some ".txt"))
(expect "several files, their common prefix" (suggest "cat a" []) None)
(expect "in a directory" (suggest "cat abd/in" []) (Some "ner"))
(expect "not the command" (suggest "ab" []) None)
(expect "not an empty argument" (suggest "cat " []) None)
(expect "not a second line" (suggest "echo 'a\ncat abc" []) None)
(expect "cd from the history, newest first"
(suggest "cd a" ["cd abd" "cd abd/deeper" "ls"]) (Some "bd/deeper"))
(expect "not a directory that is gone"
(suggest "cd o" ["cd other" "cd old-gone"]) (Some "ther"))
(expect "only cd lines" (suggest "cd o" ["cdx other"]) (Some "ther/"))
(set-current-directory! here)
(ignore (directory-delete-tree dir))
0)

View file

@ -109,6 +109,15 @@
(expect "cd and pwd" (c "cd /tmp; pwd") "/tmp")
(expect "cd -" (c "cd /tmp; cd /; cd -") "/tmp")
(expect "cd to nowhere" (string-contains? (c "cd /no/such/dir") "code: 1") #t)
(expect "prevd goes back" (c "cd /tmp; cd /; prevd; pwd") "/tmp")
(expect "and nextd forward" (c "cd /tmp; cd /; prevd; nextd; pwd") "/")
(expect "prevd N" (c "cd /tmp; cd /usr; cd /; prevd 2; pwd") "/tmp")
(expect "cd empties the way forward" (string-contains? (c "cd /tmp; cd /; prevd; cd /usr; nextd") "no next directory") #t)
(expect "prevd with no history" (string-contains? (c "prevd") "code: 1") #t)
(expect "cdh lists newest first" (c "cd /tmp; cd /usr; cd /; cdh")
(str " 1 /usr\n 2 /tmp\n 3 " (current-directory)))
(expect "cdh N goes there" (c "cd /tmp; cd /usr; cd /; cdh 2; pwd") "/tmp")
(expect "the same directory is not history" (c "cd /tmp; cd /tmp; cdh") (str " 1 " (current-directory)))
(expect "type" (c "type cd") "cd is a shell command")
(expect "a shell command in a pipe" (c "cd | cat")
"err: bjosh: cd: a shell command cannot be in a pipe\ncode: 2")