echo as a value command

- echo gives its words as one string, and shadows the program.
- With -n, -e or -E it runs the program echo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 07:29:10 +01:00
parent acd84a2a4c
commit 8ded734477
5 changed files with 36 additions and 10 deletions

View file

@ -5,7 +5,8 @@
;; (bjosh commands): the value commands. See decision 4 of plan.org.
;;
;; - Each command has a long name and a short one. The short name always
;; runs the command. The long name runs it only when no program of that
;; runs the command. `echo` is both, so it shadows the program, except
;; with the program's flags (`echo-flags?`). The long name runs it only when no program of that
;; name is on PATH: `uniq` is the program, `u` the command.
;; - A command reads bytes (`OnBytes`), values (`OnValues`), or nothing
;; (`OnNothing`), and writes values. The caller connects them: see
@ -19,7 +20,7 @@
(import (text csv))
(import (bjosh values))
(export ValueCommand Body value-command value-commands value-names lines-body)
(export ValueCommand Body value-command value-commands value-names lines-body echo-flags?)
(import/extern
(regex-match? (: System.Text.RegularExpressions.Regex.IsMatch (-> string string bool)
@ -172,6 +173,16 @@
unit)
;; A file by its extension: .json as from-json, .csv as from-csv, else lines.
(: (-> (Vec string) Values Unit))
(defbjo (echo-body args out)
(emit! out (JsonStr (string-join args " "))))
;; The flags of the program echo. With one, the program runs.
(: (-> (Vec string) bool))
(defun (echo-flags? argv)
(and (> (vec-length argv) 1)
(vec-contains? ["-n" "-e" "-E" "-ne" "-en" "-nE" "-En"] (vec-ref argv 1))))
(: (-> (Vec string) Values Unit))
(defbjo (open-body args out)
(unless (= (vec-length args) 1)
@ -441,7 +452,8 @@
(: (-> (Vec ValueCommand)))
(defun (value-commands)
[(vc "files" "l" "files [DIR...]" "the files of directories, as records: name, type, size, modified" (OnNothing files-body))
[(vc "echo" "echo" "echo WORD..." "its words as one string; with -n, -e or -E, the program echo" (OnNothing echo-body))
(vc "files" "l" "files [DIR...]" "the files of directories, as records: name, type, size, modified" (OnNothing files-body))
(vc "procs" "p" "procs" "the processes, from /proc: pid, ppid, name, state, mem, command" (OnNothing procs-body))
(vc "open" "o" "open FILE" "a file as values: .json as from-json, .csv as from-csv, else lines" (OnNothing open-body))
(vc "where" "wh" "where FIELD OP VALUE" "the values that match: OP is == != < <= > >= (or eq ne lt le gt ge), =~ or contains; FIELD it is the value itself" (OnValues where-body))
@ -462,7 +474,9 @@
;; The short and long names, for completion.
(: (-> (Vec string)))
(defun (value-names)
(vec-fold (fun (c acc) (vec-add (vec-add acc (record-ref c short)) (record-ref c long)))
(vec-fold (fun (c acc)
(def with-short (vec-add acc (record-ref c short)))
(if (= (record-ref c short) (record-ref c long)) with-short (vec-add with-short (record-ref c long))))
[]
(value-commands)))

View file

@ -170,7 +170,7 @@
(match builtin-in-pipe
((Some e) (shell-error (str (vec-ref (record-ref e argv) 0) ": a shell command cannot be in a pipe")) 2)
(None
(if (any? (fun (e) (some? (stage-command sh (vec-ref (record-ref e argv) 0)))) expanded)
(if (any? (fun (e) (some? (stage-command-of sh (record-ref e argv)))) expanded)
(run-mixed sh expanded text background?)
(run-form sh expanded text background?)))))))
@ -187,6 +187,10 @@
;; Value commands next to each other, with the file the first reads (<)
;; and the file the last writes (> or >>, with append?).
(: (-> Shell (Vec string) (Option ValueCommand)))
(defun (stage-command-of sh argv)
(if (echo-flags? argv) None (stage-command sh (vec-ref argv 0))))
(type (: Segment (Record (: stages (Vec ValueStage))
(: from (Option string))
(: into (Option (Tuple string bool))))))
@ -238,7 +242,7 @@
(let* ((e (vec-ref expanded i))
(argv (record-ref e argv))
(name (vec-ref argv 0)))
(match (stage-command sh name)
(match (stage-command-of sh argv)
((Some c)
(def stage (ValueStage (command c) (args (vec-drop argv 1))))
(def seg (match current