ambrevar/shell: Add draft wrapper to start ncurses "visual" programs.

master
Pierre Neidhardt 2021-01-21 19:39:05 +01:00
parent 881a517c9c
commit 54f263ed15
1 changed files with 59 additions and 0 deletions

View File

@ -223,6 +223,65 @@ Example:
"Like `cmd-' but return a stream."
(apply #'%cmd- #'cmd> args))
(defvar *visual-command* '("htop"))
(defvar *command-wrappers* '("sudo" "env"))
(defun visual-command-p (command)
"Return true if the COMMAND list runs one of the programs in `*visual-command*'.
`*command-wrappers*' are supported, i.e.
env FOO=BAR sudo -i powertop
works."
(labels ((basename (arg)
(namestring (pathname-name arg)))
(flag? (arg)
(str:starts-with? "-" arg))
(variable? (arg)
(and (< 1 (length arg))
(str:contains? "=" (subseq arg 1))))
(first-positional-argument (command)
"Return the argument that's not a flag, not a variable setting and
not in `*command-wrappers*'."
(when command
(if (or (flag? (first command))
(variable? (first command))
(find (basename (first command))
*command-wrappers*
:test #'string=))
(first-positional-argument (rest command))
(first command)))))
(sera:and-let* ((cmd (first-positional-argument command)))
(find (basename cmd)
*visual-command*
:test #'string=))))
(defun vterm-terminal (cmd)
(list
"emacsclient" "--eval"
(let ((*print-case* :downcase))
(write-to-string
`(progn
(vterm)
(vterm-insert ,(str:join " " cmd))
(vterm-send-return))))))
(defvar *terminal* '("/gnu/store/751mcahyg3b5dpwkbfvzg6x1vdn9i49a-xterm-363/bin/xterm" "-e")
"The terminal is either a list of arguments after which will be prepended to
the visual command to run, or a function of one argument, the list of commands,
returning the new list of commands.")
(defun maybe-launch-visual-command (cmd)
(if (visual-command-p cmd)
(cmd:cmd
(if (functionp *terminal*)
(funcall *terminal* cmd)
(append *terminal* cmd)))
(cmd:cmd cmd)))
(setf *terminal* #'vterm-terminal)
;; (maybe-launch-visual-command '("htop"))
(export-always 'tee)
(defun tee (input-stream) ; TODO: Real `tee' with separate process.
"Return the INPUT-STREAM and its string representation as a second value."