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:
parent
acd84a2a4c
commit
8ded734477
5 changed files with 36 additions and 10 deletions
7
plan.org
7
plan.org
|
|
@ -288,9 +288,10 @@ the text above:*
|
|||
state, command and id; and of number and line. Alone they write text.
|
||||
- ~2>&1~ is done (phase 3): ~>FILE 2>&1~ works, ~2>&1 >FILE~ is an error.
|
||||
~>&2~ has no form in ~(std run)~ yet.
|
||||
- Not done: ~echo~. A value ~echo~ would take the name from ~/bin/echo~,
|
||||
which decision 4 does not allow; to decide.
|
||||
- Tests: ~tests/values.bjo~ (51); ~tests/run.bjo~ (34);
|
||||
- ~echo~ is a value command, and shadows the program: the one exception to
|
||||
decision 4. With ~-n~, ~-e~ or ~-E~ the program runs, since a value has
|
||||
no way to leave out the newline.
|
||||
- Tests: ~tests/values.bjo~ (56); ~tests/run.bjo~ (34);
|
||||
~tests/interactive.bjo~ (12).
|
||||
|
||||
** Phase 3: more POSIX, and the rest of the edges
|
||||
|
|
|
|||
|
|
@ -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)))
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@
|
|||
(expect ">FILE 2>&1" (c (str "ls /no/such/dir >" file " 2>&1; grep -c such " file)) "1")
|
||||
(expect "2>&1 before >" (c (str "ls 2>&1 >" file))
|
||||
"err: bjosh: 2>&1 before > or >> is not supported yet: write >FILE 2>&1\ncode: 2")
|
||||
(expect ">&2 is not supported yet" (c "echo x >&2")
|
||||
(expect ">&2 is not supported yet" (c "printf x >&2")
|
||||
"err: bjosh: >&2 is not supported yet\ncode: 2")
|
||||
|
||||
;; Shell commands.
|
||||
|
|
|
|||
|
|
@ -117,6 +117,13 @@
|
|||
(expect "jobs in a pipe, without jobs" (c "jobs | n") "0")
|
||||
(expect "history in a pipe, without an editor" (c "history | n") "0")
|
||||
|
||||
;; echo.
|
||||
(expect "echo is a value command" (c "type echo") "echo is a value command: echo, echo")
|
||||
(expect "echo: its words as one string" (c "echo a b | n") "1")
|
||||
(expect "echo with no words" (c "echo | wc -c") "1")
|
||||
(expect "echo -n is the program" (c "echo -n x | wc -c") "1")
|
||||
(expect "echo > a file" (c "echo hi > e.txt; cat e.txt") "hi")
|
||||
|
||||
;; Names (decision 4).
|
||||
(expect "a long name that is a program runs the program" (c "printf 'a\\na\\n' | uniq -c") " 2 a")
|
||||
(expect "a short name is always the value command" (c "type u") "u is a value command: uniq, u")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue