aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Okay <okay@toyful.space>2022-03-15 16:29:53 -0500
committerColin Okay <okay@toyful.space>2022-03-15 16:29:53 -0500
commit3a19876d855ec8d2279293892e24716b28c4b48a (patch)
tree174ee078917e30c5a90d24fcbfbff94673709bfc
parentfbce85871c6e995ece00e3136cf7dfdeeeff2bb7 (diff)
added --confirm option. refactored run-item & co to accomodate
-rw-r--r--app/app.lisp4
-rw-r--r--lib/client.lisp54
2 files changed, 32 insertions, 26 deletions
diff --git a/app/app.lisp b/app/app.lisp
index 5139208..93181a8 100644
--- a/app/app.lisp
+++ b/app/app.lisp
@@ -139,6 +139,9 @@ Their meaning is as follows:
(flag :long-name "verbose"
:short-name "v"
:description "Prints a message indicating the oneliner text that is about to be run prior to execution.")
+ (flag :long-name "confirm"
+ :short-name "c"
+ :description "Prompts the user for confirmation before running. Implies --verbose.")
(flag :long-name "draft"
:description "Indicates that you wish to run a draft of a oneliner identified by IDENTIFIER."))
(group (:header "CLIPPING ONELINERS" :hidden t)
@@ -298,6 +301,7 @@ than the users."
(help-and-quit-unless "run" id-or-name)
(cli:run-item id-or-name (rest args)
:verbose (getopt :long-name "verbose")
+ :confirm (getopt :long-name "confirm")
:timeout (getopt :long-name "timeout")
:draftp (getopt :long-name "draft")))
(:clip
diff --git a/lib/client.lisp b/lib/client.lisp
index 9f34bb5..61c3130 100644
--- a/lib/client.lisp
+++ b/lib/client.lisp
@@ -92,47 +92,49 @@ running the body. If such a oneliner can be found."
(defvar *ol-output-timeout* 1)
-(defun run-item (ident args &key force-clip (timeout nil timeout-p) draftp verbose)
+(defun run-item (ident args &key force-clip (timeout nil timeout-p) draftp verbose confirm)
"Runs a oneliner identified by IDENT (if available) with arguments ARGS."
(let ((ol (if draftp (fetch-draft ident) (the-oneliner ident))))
(when ol
(let ((*ol-output-timeout* (if timeout-p timeout *ol-output-timeout*)))
- (bind-vars-and-run-oneliner ol args force-clip verbose)))))
+ (bind-vars-and-run-oneliner ol args force-clip verbose confirm)))))
-(defun bind-vars-and-run-oneliner (ol args &optional force-clip verbose)
+(defun bind-vars-and-run-oneliner (ol args &optional force-clip verbose confirm)
(let* ((oneliner (oneliner-oneliner ol))
(runstyle (oneliner-runstyle ol))
(pos-args (get-positional-arguments ol))
(named-args (get-named-arguments ol)))
+
+ (loop for param in pos-args
+ for arg in args
+ do (setf oneliner (str:replace-all param arg oneliner)))
+ ;; substitute named args
+
+ (setf args
+ (mapcar (lambda (s) (str:split "=" s))
+ (nthcdr (length pos-args) args)))
+ (loop for var in named-args
+ for bound = (assoc (subseq var 1) args :test #'equal)
+ when bound
+ do (setf oneliner
+ (str:replace-all var (second bound) oneliner)))
+
(when (or (not (oneliner-isflagged ol))
(y-or-n-p "This oneliner is flagged. Are you sure you want to run it?"))
+ (when (or verbose confirm)
+ (format t "Attempting to run:~%")
+ (princ oneliner)
+ (princ #\newline))
;; substitute positional args
- (loop for param in pos-args
- for arg in args
- do (setf oneliner (str:replace-all param arg oneliner)))
- ;; substitute named args
- (setf args
- (mapcar (lambda (s) (str:split "=" s))
- (nthcdr (length pos-args) args)))
- (loop for var in named-args
- for bound = (assoc (subseq var 1) args :test #'equal)
- when bound
- do (setf oneliner
- (str:replace-all var (second bound) oneliner)))
-
- (handle-run-oneliner oneliner (or force-clip (equalp runstyle "manual")) verbose))))
-
-(defun handle-run-oneliner (ol &optional clip (verbose t))
+ (when (or (not confirm)
+ (y-or-n-p "Proceed?"))
+ (handle-run-oneliner oneliner (or force-clip (equalp runstyle "manual")))))))
+
+(defun handle-run-oneliner (ol &optional clip)
(if clip
(progn (trivial-clipboard:text ol)
(format t "Copied oneliner to clipboard~%"))
- (progn
- (when verbose
- (format t "Attempting to run:~%")
- (princ ol)
- (princ #\newline)
- (princ #\newline))
- (run-with-shell ol :shell-name (or (shell) "bash") :await-output-p *ol-output-timeout*))))
+ (run-with-shell ol :shell-name (or (shell) "bash") :await-output-p *ol-output-timeout*)))
;;; ADDING ONELINERS