aboutsummaryrefslogtreecommitdiff
path: root/src/lib.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.lisp')
-rw-r--r--src/lib.lisp62
1 files changed, 58 insertions, 4 deletions
diff --git a/src/lib.lisp b/src/lib.lisp
index 300d6d5..442e023 100644
--- a/src/lib.lisp
+++ b/src/lib.lisp
@@ -52,6 +52,9 @@
(defun (setf api-token) (newval)
(setf (getf *config* :api-token) newval))
+(defun get-shell ()
+ (getf *config* :shell))
+
(defun make-temp-file-name ()
(namestring
(merge-pathnames (format nil "~a" (gensym "oneliners")) (uiop:temporary-directory))))
@@ -137,6 +140,53 @@ the directories that appear in the value of that variable."
(:host (host))
(api:put--oneliner-oneliner-locked (getf ol :id) :token (api-token) :value "false"))))
+(defun collect-positional-arguments (oneliner)
+ (remove-duplicates
+ (sort
+ (ppcre:all-matches-as-strings "\\$[1-9][0-9]*" oneliner)
+ #'string<)
+ :test #'equal))
+
+(defun collect-named-arguments (oneliner)
+ (remove-duplicates
+ (ppcre:all-matches-as-strings "\\$[a-zA-Z_][a-zA-Z0-9_]*" oneliner)
+ :test #'equal))
+
+(defun handle-run-oneliner (ol &key (runstyle "auto"))
+ (if (equalp runstyle "manual")
+ (progn (trivial-clipboard:text ol)
+ (format t "Copied oneliner to clipboard~%"))
+ (progn
+ (ensure-config)
+ (format t "Attempting to run: ~%")
+ (princ ol) (terpri)
+ (run-with-shell ol :shell-name (or (get-shell) "bash")))))
+
+
+(defun run-item (item-number args)
+ (with-cached-result (ol item-number)
+ (let* ((oneliner (getf ol :oneliner))
+ (runstyle (getf ol :runstyle))
+ (pos-args (collect-positional-arguments oneliner))
+ (named-args (collect-named-arguments oneliner)))
+
+ (when (or (not (getf ol :isflagged))
+ (y-or-n-p "This oneliner is flagged. Are you sure you want to run it?"))
+ ;; 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 :runstyle runstyle)))))
(defun add-new-oneliner ()
@@ -151,7 +201,7 @@ the directories that appear in the value of that variable."
(tags
(append init-tags
(ppcre:split " +"
- (prompt (format nil "Additional Tags? [~{~a ~}]" init-tags)))))
+ (prompt (format nil "Tags in addition to ~{~a ~} ?" init-tags)))))
(explanation
(when (y-or-n-p "Provide an explanation?")
(string-from-editor)))
@@ -213,7 +263,9 @@ the directories that appear in the value of that variable."
(print results output)))
(defun print-oneliner-result-for-user (number oneliner)
- (format t "~3a~a~a~a: ~a"
+ (dotimes (n 80) (princ #\_))
+ (terpri)
+ (format t "~3a~a~a~a ~a"
number
(if (getf oneliner :isflagged)
"⚠" " ")
@@ -222,7 +274,8 @@ the directories that appear in the value of that variable."
(if (equalp "manual" (getf oneliner :runstyle))
"📋" " ")
(getf oneliner :brief))
- (format t "~% ~a~%~%" (getf oneliner :oneliner)))
+ (format t "~% by: ~12a tags: ~{~a~^ ~}" (getf oneliner :createdby) (getf oneliner :tags))
+ (format t "~%~% ~a~%~%" (getf oneliner :oneliner)))
(defun search-for-oneliners (terms limit not-flagged-p)
@@ -241,7 +294,8 @@ the directories that appear in the value of that variable."
(loop for number from 1
for oneliner in oneliners
collect (list* :result-number number oneliner)
- do (print-oneliner-result-for-user number oneliner)))))))
+ do (print-oneliner-result-for-user number oneliner)))
+ (format t "Run a command with: ol NUMBER [ARGS...]~%")))))
;;; RUNNING COMMANDS