diff options
author | Colin Okay <okay@toyful.space> | 2022-02-25 09:12:01 -0600 |
---|---|---|
committer | Colin Okay <okay@toyful.space> | 2022-02-25 09:12:01 -0600 |
commit | 81d5910593a7cd3a4140bf49274e1e81cd988e4c (patch) | |
tree | bef0688c0234b9cd479ac46f1ff9fb1dfb12896b | |
parent | 24fc1a419c2f868be3c9b134a5780bc472cdfc57 (diff) |
added lib support for aliases.
-rw-r--r-- | build-app.lisp | 6 | ||||
-rw-r--r-- | oneliners.cli.asd | 3 | ||||
-rw-r--r-- | src/lib.lisp | 100 |
3 files changed, 61 insertions, 48 deletions
diff --git a/build-app.lisp b/build-app.lisp index 32b1777..2b49b0a 100644 --- a/build-app.lisp +++ b/build-app.lisp @@ -29,7 +29,7 @@ my1337pw.") (defsynopsis (:postfix "[TAGS ...] | N [ARGS ...]") (group (:header "SEARCH") (text :contents "Return oneliners tagged with all of TAGS") - (lispobj :long-name "limit" + (lispobj :long-name "limit" :argument-type :optional :argument-name "NUMBER" :default-value 10 @@ -57,9 +57,7 @@ my1337pw.") :enum '(:access :wiki :invites) :argument-name "TOPIC" :description "Print help for a topic. -Topics: wiki, access, invites, advanced")) - (group (:header "Advanced" :hidden t) - (flag :long-name "alias")) +Topics: wiki, access, invites")) (group (:header "Wiki" :hidden t) (flag :long-name "add" :description "Intaractively add a oneliner and update the wiki.") diff --git a/oneliners.cli.asd b/oneliners.cli.asd index d71980c..7887011 100644 --- a/oneliners.cli.asd +++ b/oneliners.cli.asd @@ -15,7 +15,6 @@ :components ((:module "src" :components ((:file "lib")))) - :description "" - :in-order-to ((test-op (test-op "oneliners.cli/tests")))) + :description "") 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." |