Phase 2: jobs and history as values, redirections of value commands

- jobs and history are value sources in a pipe; alone they write text.
- < on the first value command of a group, > and >> on the last,
  which then writes lines. A value command after > is an error.
- File errors use fs-error-message, without a stack trace.
- plan.org: Ctrl-C does not cancel a blocking read of a terminal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 07:21:04 +01:00
parent 027acc84e6
commit acd84a2a4c
8 changed files with 252 additions and 71 deletions

View file

@ -15,8 +15,10 @@
(import (bjoterm line))
(import (bjosh shell))
(import (bjosh commands))
(import (bjosh values))
(import (text json))
(export builtin? run-builtin builtin-names find-program)
(export builtin? run-builtin builtin-names find-program shell-value-command)
(type (: Builtin (Record (: name string)
(: usage string)
@ -262,6 +264,52 @@
(Builtin (name "help") (usage "help") (help "this list") (run help))
(Builtin (name "history") (usage "history") (help "the lines written before") (run history))])
;; ===========================================================================
;; Shell commands as values, in a pipe
;; ===========================================================================
;; jobs in a pipe: number, current (+, - or nothing), state, command, id.
(: (-> Shell (Vec string) Values Unit))
(defbjo (jobs-values sh args out)
(unless (vec-empty? args)
(command-error "jobs: takes no arguments in a pipe"))
(def entries (record-ref sh jobs))
(let go ((i 0))
(when (< i (vec-length entries))
(def e (vec-ref entries i))
(emit! out (record-of [(Tuple "number" (JsonInt (cast long (record-ref e number))))
(Tuple "current" (JsonStr (string-trim (job-mark sh e))))
(Tuple "state" (JsonStr (job-state-text e)))
(Tuple "command" (JsonStr (record-ref e text)))
(Tuple "id" (JsonInt (cast long (job-id (record-ref e job)))))]))
(go (+ i 1))))
unit)
;; history in a pipe: number and line, oldest first.
(: (-> Shell (Vec string) Values Unit))
(defbjo (history-values sh args out)
(unless (vec-empty? args)
(command-error "history: takes no arguments in a pipe"))
(def entries (match (record-ref sh editor) ((Some ed) (line-history ed)) (None [])))
(let go ((i 0))
(when (< i (vec-length entries))
(emit! out (record-of [(Tuple "number" (JsonInt (cast long (+ i 1))))
(Tuple "line" (JsonStr (vec-ref entries i)))]))
(go (+ i 1))))
unit)
;; The shell commands that are value sources in a pipe: jobs and history.
(: (-> Shell string (Option ValueCommand)))
(defun (shell-value-command sh name)
(cond
((= name "jobs")
(Some (ValueCommand (long "jobs") (short "jobs") (usage "jobs") (help "the jobs, as records")
(body (OnNothing (bjoroutine (args out) (jobs-values sh args out)))))))
((= name "history")
(Some (ValueCommand (long "history") (short "history") (usage "history") (help "the lines written before, as records")
(body (OnNothing (bjoroutine (args out) (history-values sh args out)))))))
(else None)))
(: (-> string bool))
(defun (builtin? name)
(any? (fun (b) (= (record-ref b name) name)) (builtins)))