History suggestions, frecency, cd by keyword, fish completions; bjoterm 0.2.0

The editor suggests history lines whose command exists, cd lines by the
frecency of their directory, paths ignoring case, and the visited directory
that cd KEYWORD... goes to. Tab completes arguments with fish's completions
when fish is on PATH, and paths by the start of each part (~/P/B/B).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 13:30:23 +01:00
parent 5012a5d993
commit 61d5a96eda
10 changed files with 751 additions and 110 deletions

View file

@ -25,9 +25,14 @@
;; turns this off.
;; - The prompt is $PROMPT when it is set (see `custom-prompt`), else the
;; user's prompt function, else the directory and $.
;; - The line editor suggests `cd` directories and paths as fish does (see
;; `suggest`). On an empty line, Alt-Left and Alt-Right go back and
;; forward in the directory history, as `prevd` and `nextd`.
;; - The line editor suggests lines from the history, directories and paths
;; as fish does (see `suggest`), and Tab completes with fish's
;; completions when fish is on PATH (see `complete`).
;; - An interactive shell keeps the directories it visits, ranked by
;; frecency, and its directory history, in $XDG_STATE_HOME/bjosh (see
;; (bjosh dirs)). `cd KEYWORD...` goes to the best match.
;; - On an empty line, Alt-Left and Alt-Right go back and forward in the
;; directory history, as `prevd` and `nextd`.
(import (std prelude))
(import (std run))
@ -182,13 +187,25 @@
(def jc (match (job-control)
((Ok jc) (Some jc))
((Err _) (shell-error "no job control: & and Ctrl-Z do not work") None)))
(def ed (make-line-editor #:completer (Some complete) #:input-done? (Some input-done?)
#:suggester (Some suggest) #:command-key? (Some directory-key?)
#:suspend? #f))
;; The editor is made before the shell, which holds it.
(def current (make-box None))
(def ed (make-line-editor
#:completer (Some (fun (text)
(match (box-ref current)
((Some sh) (complete sh text))
(None (Completion (prefix "") (candidates []) (descriptions []))))))
#:input-done? (Some input-done?)
#:suggester (Some (fun (text history)
(match (box-ref current)
((Some sh) (suggest sh text history))
(None None))))
#:command-key? (Some directory-key?)
#:suspend? #f))
(def path (history-path))
(ignore (line-history-load! ed path))
(ignore (try (directory-create (path-combine path "..")) #:catch (System.Exception)))
(def sh (make-shell jc (Some ed) ["bjosh"]))
(box-set! current (Some sh))
(def rc (rc-path))
(when (file-exists? rc)
(match (file-read-text rc)
@ -196,6 +213,7 @@
((Err e) (shell-error (str rc ": " (fs-error-message e))))))
(set-up-commands)
(ignore (with-extension sh))
(dirs-persist! sh)
(let go ()
(report-jobs sh)
(match (line-read ed term (prompt sh) #:notices (Some (fun () (job-changes sh))))