From 505ff9a0b200217af7feb0e2290e9cf25790661c Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Mon, 21 Feb 2022 10:01:42 -0600 Subject: search result printing --- src/lib.lisp | 71 ++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/lib.lisp b/src/lib.lisp index b8c7343..d174c6a 100644 --- a/src/lib.lisp +++ b/src/lib.lisp @@ -53,10 +53,14 @@ (setf (getf *config* :api-token) newval)) (defun make-temp-file-name () - (merge-pathnames (format nil "~a" (gensym "oneliners")) (uiop:temporary-directory))) + (namestring + (merge-pathnames (format nil "~a" (gensym "oneliners")) (uiop:temporary-directory)))) (defun string-from-editor () - (magic-ed:magic-ed make-temp-file-name :eval nil :output :string)) + (let ((filename (make-temp-file-name))) + (unwind-protect + (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))) @@ -111,24 +115,19 @@ the directories that appear in the value of that variable." (append init-tags (when (y-or-n-p "Add tags in addition to: ~{~a ~}?" init-tags) (ppcre:split " +" (prompt "(e.g. foo bar goo): "))))) - (explaination - (when (y-or-n-p "Provide an explaination?") + (explanation + (when (y-or-n-p "Provide an explanation?") (string-from-editor)))) - (format t "Adding a new oneliner.~%~s~%" - (list :oneliners oneliner - :tags tags - :brief brief - :explanation explaination)) - - ;; (api:request-with - ;; (:host (host) - ;; :body (jonathan:to-json - ;; (list :oneliners oneliner - ;; :brief brief - ;; :tags ))) - ;; (api:post--add-oneliner :token (api-token))) - - (format t "Added~%"))) + (api:request-with + (:host (host) + :body (jonathan:to-json + (list :oneliner oneliner + :tags tags + :brief brief + :explanation explanation)) + :content-type "application/json") + (api:post--add-oneliner :token (api-token)) + (format t "Added~%")))) (defun request-invite-code () (ensure-config) @@ -156,14 +155,38 @@ the directories that appear in the value of that variable." :password1 pass :password2 pass))) +(defun cache-search-results-to-last-search-file (results) + (with-open-file (output (last-search-file) :direction :output :if-exists :supersede) + (print results output))) + +(defun print-oneliner-result-for-user (number oneliner) + (format t "~3a~a~a: ~a" + number + (if (getf oneliner :isflagged) + "⚠" " ") + (if (getf oneliner :islocked) + "🔒" " ") + (getf oneliner :brief)) + (format t "~% ~a~%~%" (getf oneliner :oneliner))) + (defun search-for-oneliners (terms limit not-flagged-p) (assert (loop for term in terms never (find #\, term) )) (ensure-config) - (print (api:request-with - (:host (host)) - (api:get--search :tags (str:join "," terms) - :limit limit - :notflagged (if not-flagged-p "true" "false"))))) + (let ((response + (api:request-with + (:host (host)) + (api:get--search :tags (str:join "," terms) + :limit limit + :notflagged (if not-flagged-p "true" "false"))))) + (handler-case + (a:when-let (oneliners + (getf (jonathan:parse response) :oneliners)) + (cache-search-results-to-last-search-file + (loop for number from 1 + for oneliner in oneliners + collect (list* :result-number number oneliner) + do (print-oneliner-result-for-user number oneliner))))))) + ;;; RUNNING COMMANDS -- cgit v1.2.3