From 81d5910593a7cd3a4140bf49274e1e81cd988e4c Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Fri, 25 Feb 2022 09:12:01 -0600 Subject: added lib support for aliases. --- src/lib.lisp | 100 ++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 42 deletions(-) (limited to 'src/lib.lisp') diff --git a/src/lib.lisp b/src/lib.lisp index f596c84..4a8988a 100644 --- a/src/lib.lisp +++ b/src/lib.lisp @@ -13,10 +13,11 @@ (defvar *config* nil "A configuration plist") -(defun make-config (&key host api-token editor) +(defun make-config (&key host api-token editor (shell "bash")) (append (when host (list :host host)) (when api-token (list :api-token api-token)) - (when editor (list :editor editor)))) + (when editor (list :editor editor)) + (list :shell shell))) (defun valid-config-p (config) (and (listp config) @@ -52,10 +53,17 @@ (defun api-token () (getf *config* :api-token)) (defun (setf api-token) (newval) (setf (getf *config* :api-token) newval)) - (defun get-shell () (getf *config* :shell)) +(defun config-file () + (merge-pathnames ".config/oneliners.config" (user-homedir-pathname))) + +(defun last-search-file () + (merge-pathnames ".last_oneliners_search" (user-homedir-pathname))) + + +;;; UTILITIES (defun make-temp-file-name () (namestring (merge-pathnames (format nil "~a" (gensym "oneliners")) (uiop:temporary-directory)))) @@ -67,17 +75,6 @@ (magic-ed:magic-ed filename :eval nil :output :string) (uiop:delete-file-if-exists filename)))) -(defun config-file () - (merge-pathnames ".config/oneliners.config" (user-homedir-pathname))) - -(defun last-search-file () - (merge-pathnames ".last_oneliners_search" (user-homedir-pathname))) - -(defun fetch-nth-oneliner (n) - "Returns nil if there is no nth oneliner from the search history." - (when (uiop:file-exists-p (last-search-file)) - (nth n (uiop:read-file-form (last-search-file))))) - (defun executable-on-system-p (name) "A hack that heuristically determins whether or not an executable with the provided name is on the system. It is not perfect. It @@ -93,8 +90,6 @@ the directories that appear in the value of that variable." (defun tags-from-oneliner (oneliner) (remove-if-not #'executable-on-system-p (ppcre:split " +" oneliner))) - - (rl:register-hook :signal (lambda () (uiop:quit))) (defun prompt (prompt @@ -197,33 +192,33 @@ the directories that appear in the value of that variable." (princ #\newline) (run-with-shell ol :shell-name (or (get-shell) "bash"))))) - - +(defun bind-vars-and-run-oneliner (ol args &optional force-clip) + (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 (or force-clip (equalp runstyle "manual")))))) (defun run-item (item-number args &optional force-clip) (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 (or force-clip (equalp runstyle "manual"))))))) + (bind-vars-and-run-oneliner ol args force-clip))) (defun valid-oneliner-string-p (string) (and (not (find #\newline string)) @@ -235,6 +230,28 @@ the directories that appear in the value of that variable." (defun valid-runstyle-p (string) (member string '("auto" "manual") :test 'equalp)) +(defun aliases () + (getf *config* :aliases)) + +(defun (setf aliases) (newval) + (setf (getf *config* :aliases) newval)) + +(defun alias-item (item alias) + (with-cached-result (ol item) + (ensure-config) + (a:if-let (found (assoc alias (aliases))) + (setf (cdr found) ol) + (push (cons alias ol) (aliases))) + (write-config-to-disk))) + +(defun lookup-alias (alias) + (cdr (assoc alias (aliases)))) + +(defun run-alias (alias args &optional force-clip) + (ensure-config) + (a:when-let (ol (lookup-alias alias)) + (bind-vars-and-run-oneliner ol args force-clip))) + (defun add-new-oneliner () (ensure-config) (assert (api-token) () "Cannot add a oneliner without an api token.") @@ -354,7 +371,6 @@ the directories that appear in the value of that variable." (:host (host)) (api:delete--access-access (api-token) :token (api-token)))) - (defun cache-search-results-to-last-search-file (results) (with-open-file (output (last-search-file) :direction :output :if-exists :supersede) (print results output))) @@ -395,7 +411,7 @@ the directories that appear in the value of that variable." (format t "Run a command with: ol NUMBER [ARGS...]~%"))))) -;;; RUNNING COMMANDS +;;; RUNNING THINGS IN THE SHELL. (defun parent-process-name () "Prints the name of the parent process of the current process." -- cgit v1.2.3