diff options
Diffstat (limited to 'src/lib.lisp')
-rw-r--r-- | src/lib.lisp | 61 |
1 files changed, 40 insertions, 21 deletions
diff --git a/src/lib.lisp b/src/lib.lisp index b87ffc6..d990b22 100644 --- a/src/lib.lisp +++ b/src/lib.lisp @@ -157,10 +157,11 @@ the directories that appear in the value of that variable." (nth n contents))))) (defmacro with-cached-result ((olvar n &optional idp) &body body) - (a:with-gensyms (nvar) - `(let ((,nvar ,n)) + (a:with-gensyms (nvar idpvar) + `(let ((,nvar ,n) + (,idpvar ,idp)) (assert (plusp ,nvar) () "Item number must be 1 or greater") - (a:if-let (,olvar ,(if idp `(cached-result ,nvar t) `(cached-result (1- ,nvar)))) + (a:if-let (,olvar (if ,idpvar (cached-result ,nvar t) (cached-result (1- ,nvar)))) (progn ,@body) (format t "Could not find the oneliner specified by ~a~%" ,nvar))))) @@ -172,29 +173,29 @@ the directories that appear in the value of that variable." ;;; API REQUEST FUNCTIONS -(defun flag-item (item-number) - (with-cached-result (ol item-number) +(defun flag-item (item-number &optional idp) + (with-cached-result (ol item-number idp) (ensure-config) (api:request-with (:host (host)) (api:put--oneliner-entry-flag (getf ol :id) :token (api-token) :value "true")))) -(defun unflag-item (item-number) - (with-cached-result (ol item-number) +(defun unflag-item (item-number &optional idp) + (with-cached-result (ol item-number idp) (ensure-config) (api:request-with (:host (host)) (api:put--oneliner-entry-flag (getf ol :id) :token (api-token) :value "false")))) -(defun lock-item (item-number) - (with-cached-result (ol item-number) +(defun lock-item (item-number &optional idp) + (with-cached-result (ol item-number idp) (ensure-config) (api:request-with (:host (host)) (api:put--oneliner-oneliner-locked (getf ol :id) :token (api-token) :value "true")))) -(defun unlock-item (item-number) - (with-cached-result (ol item-number) +(defun unlock-item (item-number &optional idp) + (with-cached-result (ol item-number idp) (ensure-config) (api:request-with (:host (host)) @@ -249,12 +250,20 @@ the directories that appear in the value of that variable." (handle-run-oneliner oneliner (or force-clip (equalp runstyle "manual")))))) (defun run-item (item-number args &key by-id force-clip (timeout nil timeout-p)) - (let ((*ol-output-timeout* (if timeout-p timeout *ol-output-timeout*))) - (if by-id - (with-cached-result (ol item-number t) - (bind-vars-and-run-oneliner ol args force-clip)) - (with-cached-result (ol item-number) - (bind-vars-and-run-oneliner ol args force-clip))))) + (let ((*ol-output-timeout* (if timeout-p timeout *ol-output-timeout*)) + (found nil)) + (with-cached-result (ol item-number by-id) + (setf found t) + (bind-vars-and-run-oneliner ol args force-clip)) + (when (and by-id (not found)) + (format t "Trying to fetch that oneliner with id ~a from the wiki.~%" item-number) + (a:if-let ((ol + (api:request-with (:host (host)) + (jonathan:parse + (api::get--oneliner-entry item-number))))) + (progn (bind-vars-and-run-oneliner ol args force-clip) + (merge-into-cache (list ol))) + (format t "Still couldn't find it. Is something wrong?"))))) (defun valid-oneliner-string-p (string) (and (not (find #\newline string)) @@ -329,10 +338,10 @@ the directories that appear in the value of that variable." (api:post--oneliner :token (api-token)) (format t "Added~%")))) -(defun edit-item (n) +(defun edit-item (n &optional idp) (ensure-config) (assert (api-token) () "Cannot edit a oneliner without an api token.") - (with-cached-result (ol n) + (with-cached-result (ol n idp) (let* ((oneliner (prompt "Oneliner: " :expect 'valid-oneliner-string-p @@ -377,7 +386,9 @@ the directories that appear in the value of that variable." new-item) :content-type "application/json") (api:patch--oneliner-entry-edit (getf ol :id) :token (api-token)) - (update-history-item n new-item) + (if idp + (update-history-item n new-item) + (update-cached-item new-item)) (format t "OK~%")))))) (defun request-invite-code () @@ -480,7 +491,15 @@ the directories that appear in the value of that variable." (ol (nth (1- n) results))) (when ol (setf (nth (1- n) results) (append item ol)) - (cache-search-results-to-last-search-file results))))) + (cache-search-results-to-last-search-file results)))) + (merge-into-cache (list item))) + +(defun update-cached-item (item) + (let* ((history (with-open-file (input (last-search-file)) (read input))) + (pos (position (getf item :id) history :key (lambda (x) (getf item :id))))) + (if pos + (update-history-item (1+ pos) item) + (merge-into-cache (list item))))) (defun cache-search-results-to-last-search-file (results) (with-open-file (output (last-search-file) :direction :output :if-exists :supersede) |