From 54f263ed15d3cdf21fedb6fa1cf0a053e8619586 Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Thu, 21 Jan 2021 19:39:05 +0100 Subject: [PATCH] ambrevar/shell: Add draft wrapper to start ncurses "visual" programs. --- .../common-lisp/source/ambrevar/shell.lisp | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/.local/share/common-lisp/source/ambrevar/shell.lisp b/.local/share/common-lisp/source/ambrevar/shell.lisp index b146cbc8..bfe4a0bc 100644 --- a/.local/share/common-lisp/source/ambrevar/shell.lisp +++ b/.local/share/common-lisp/source/ambrevar/shell.lisp @@ -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."