From 62afeb3f21d3d8e8db045a001271e686e944a049 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Sun, 6 Mar 2022 11:34:35 -0600 Subject: moved prompt into its own package --- src/lib.lisp | 67 ++++++++++++++++++++++++------------------------------------ 1 file changed, 27 insertions(+), 40 deletions(-) (limited to 'src/lib.lisp') diff --git a/src/lib.lisp b/src/lib.lisp index 30c953d..4e1630e 100644 --- a/src/lib.lisp +++ b/src/lib.lisp @@ -16,11 +16,11 @@ ;; along with this program. If not, see . -(defpackage oneliners.cli +(defpackage #:oneliners.cli (:use :cl) + (:import-from #:oneliners.cli.prompt #:prompt) (:local-nicknames (#:api #:oneliners.api-client) - (#:a #:alexandria) - (#:rl #:cl-readline))) + (#:a #:alexandria))) (in-package :oneliners.cli) @@ -31,6 +31,11 @@ (defvar *ol-output-timeout* 1) +(defun ensure-local-state () + (ensure-config) + (setf api::*host* (getf *config* :host))) + + (defun valid-config-p (config) (and (listp config) (evenp (length config)) @@ -135,44 +140,9 @@ the directories that appear in the value of that variable." (remove-if-not #'executable-on-system-p (ppcre:split " +" oneliner)) :test #'equal)) -(defun prompt (prompt - &key - (expect (constantly t)) - retry-text - (prefill "")) - ;; register a prefill hook - (rl:register-hook - :pre-input - (lambda () - (rl:insert-text prefill) - (rl:redisplay))) - (unwind-protect - (loop - with prompt-text = prompt - with should-retry-p = t - while should-retry-p - for line = (rl:readline :prompt prompt-text) - when (funcall expect line) - do (setf should-retry-p nil) - when retry-text - do (setf prompt-text retry-text) - finally (return line)) - ;; unregisters the hook. - (rl:register-hook :pre-input nil))) -(defun the-oneliner (name-or-id) - "Get the oneliner with name-or-id. Try to fetch from local cache, -and, failing that, try to fetch from configured server." - (a:if-let ((ol (cached-result name-or-id))) - ol - (progn - (ensure-config) - (a:when-let (ol - (api:request-with (:host (host)) - (jonathan:parse - (api:get--oneliner-entry name-or-id)))) - (merge-into-cache (list ol)) - ol)))) + + (defun cached-result (n) (when (uiop:file-exists-p (cached-oneliners-file)) @@ -207,6 +177,20 @@ and, failing that, try to fetch from configured server." ;;; API REQUEST FUNCTIONS +(defun the-oneliner (name-or-id) + "Get the oneliner with name-or-id. Try to fetch from local cache, +and, failing that, try to fetch from configured server." + (a:if-let ((ol (cached-result name-or-id))) + ol + (progn + (ensure-config) + (a:when-let (ol + (api:request-with (:host (host)) + (jonathan:parse + (api:get--oneliner-entry name-or-id)))) + (merge-into-cache (list ol)) + ol)))) + (defun flag-item (ident) (with-oneliner (ol ident) (ensure-config) @@ -633,3 +617,6 @@ printed to OUTPUT-STREAM. " (terpri output-stream) (sleep 0.005)))))) + + + -- cgit v1.2.3