REPL: run visual commands in a terminal window

[ci skip]
This commit is contained in:
vindarel 2021-02-07 20:09:51 +01:00
parent a21f6b0f99
commit db259c7f56
3 changed files with 64 additions and 0 deletions

View file

@ -65,6 +65,16 @@ The result is concatenated into a string and printed on stdout.
This feature is only available in CIEL's REPL, not on the CIEL-USER package. This feature is only available in CIEL's REPL, not on the CIEL-USER package.
Some programs are **visual** / interactive / ncurses-based, and need
to be run in their own terminal window. CIEL recognizes a few (`vim`,
`htop`, `man`…) and runs them in the first terminal emulator found on
the system of `terminator`, `xterm`, `gnome-terminal`). See the
`*visual-commands*` variable.
> Note: this feature is experimental.
> Note: we encourage our users to use Emacs rather than a terminal!
We use the [Clesh](https://github.com/Neronus/clesh) library. We use the [Clesh](https://github.com/Neronus/clesh) library.
See also [SHCL](https://github.com/bradleyjensen/shcl) for a more unholy union of posix-shell and Common Lisp. See also [SHCL](https://github.com/bradleyjensen/shcl) for a more unholy union of posix-shell and Common Lisp.

View file

@ -46,3 +46,50 @@ bar:qux
(when (= rl:+end+ rl:*point*) (when (= rl:+end+ rl:*point*)
(format t "~c[1C" #\esc)) (format t "~c[1C" #\esc))
(finish-output))) (finish-output)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Run visual / interactive / ncurses commands in their terminal window.
;;;
;;; How to guess a program is interactive?
;;; We currently look from a hand-made list (à la Eshell).
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defparameter *visual-commands*
'(;; "emacs -nw" ;; in eshell, concept of visual-subcommands.
"vim" "vi"
"nano"
"htop" "top"
"man" "less" "more"
"screen" "tmux"
"lynx" "links" "mutt" "pine" "tin" "elm" "ncftp" "ncdu"
"ranger"
;; last but not least
"ciel-repl")
"List of visual/interactive/ncurses-based programs that will be run in their own terminal window.")
(defparameter *visual-terminal-emulator-choices*
'("terminator" "x-terminal-emulator" "xterm" "gnome-terminal"))
(defparameter *visual-terminal-switches* '("-e")
"Default options to the terminal. `-e' aka `--command'.")
(defun find-terminal ()
"Return the first terminal emulator found on the system from the `*visual-terminal-emulator-choices*' list."
(loop for program in *visual-terminal-emulator-choices*
when (which:which program)
return program))
(defun visual-command-p (text)
"The command TEXT starts by a known visual command, listed in `*visual-commands*'."
(let* ((cmd (string-left-trim "!" text)) ;; strip clesh syntax.
(first-word (first (str:words cmd))))
;; This will be smarter. https://github.com/ruricolist/cmd/issues/10
(find first-word *visual-commands* :test #'equalp)))
(defun run-visual-command (text)
"Run this text command into another terminal window."
(let ((cmd (string-left-trim "!" text)))
(uiop:launch-program `( ,(find-terminal)
;; quick way to flatten the list of switches:
,@*visual-terminal-switches*
,cmd))))

View file

@ -458,8 +458,15 @@ strings to match candidates against (for example in the form \"package:sym\")."
(sbcli::sbcli "" *prompt*)) (sbcli::sbcli "" *prompt*))
(when *hist-file* (sbcli::update-hist-file text)) (when *hist-file* (sbcli::update-hist-file text))
(cond (cond
;; Handle documentation lookup.
((str:ends-with-p " ?" text) ((str:ends-with-p " ?" text)
(sbcli::symbol-documentation (last-nested-expr text))) (sbcli::symbol-documentation (last-nested-expr text)))
;; Handle visual commands: run in their own terminal window.
((visual-command-p text)
(run-visual-command text))
;; Default: run the lisp command (with the lisp-critic and other add-ons).
(t (t
(sbcli::handle-input txt text))) (sbcli::handle-input txt text)))
(finish-output nil) (finish-output nil)