# HG changeset patch # User Steve Losh # Date 1577493666 0 # Node ID 99c33b39e53da54867500e05550a8c6053eca6c3 # Parent 24019e3c7df76c96f46bffd0e712ccc46af40014# Parent 1790650ca7326a5bccd9cf8f88138c6a808c84cc Merge. diff -r 1790650ca732 -r 99c33b39e53d lisp/pick.lisp --- a/lisp/pick.lisp Fri Dec 27 10:34:00 2019 -0500 +++ b/lisp/pick.lisp Sat Dec 28 00:41:06 2019 +0000 @@ -34,7 +34,7 @@ (defun filter (choices) (loop - :with width = (1+ (reduce #'max choices :key #'length)) + :with width = (1+ (reduce #'max choices :key #'length :initial-value 0)) :for choice :in choices :when (prompt "~A~vA[yN] " choice (- width (length choice)) #\space) :collect choice)) @@ -90,7 +90,15 @@ (adopt:define-string *help-text* "pick displays its arguments one-by-one on standard error and prompts you ~ interactively to choose some of them. The chosen items will be printed to ~ - standard output.") + standard output.~@ + ~@ + An argument of - will cause pick to read lines from standard input as ~ + choices. Using an explicit - instead of reading from standard input when no ~ + arguments are present prevents something like 'pick `ls -1 | grep foo`' from ~ + silently hanging forever if no files match.~@ + ~@ + This version was inspired by the pick program described in 'The UNIX ~ + Programming Environment'.") (defparameter *ui* (adopt:make-interface @@ -113,6 +121,10 @@ (with-open-file (*interactive-input* "/dev/tty" :direction :input) (let ((*separator* (gethash 'separator options)) (*interactive-output* *error-output*)) - (run (or arguments (read-lines *standard-input*)))))) + (run (mapcan (lambda (arg) + (if (string= "-" arg) + (read-lines *standard-input*) + (list arg))) + arguments))))) (error (c) (adopt:print-error-and-exit c))))