aboutsummaryrefslogtreecommitdiff
path: root/lib/state.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/state.lisp')
-rw-r--r--lib/state.lisp90
1 files changed, 64 insertions, 26 deletions
diff --git a/lib/state.lisp b/lib/state.lisp
index 0f69ff3..5ca5a7b 100644
--- a/lib/state.lisp
+++ b/lib/state.lisp
@@ -25,7 +25,7 @@
(host "")
(shell "bash"))
-;;; DYNAMIC VARS FOR CONFIG AND CACHE, AND SOME GETTERS
+;;; CONFIG VAR AND OPERATIONS
(defvar *config* nil
"Holds a config struct instance.")
@@ -50,9 +50,58 @@
(defun shell ()
(config-shell *config*))
+
+;;; CACHE VAR AND OPERATIONS
+
(defvar *cache* nil
"Holds cached oneliners as a list.")
+(defun merge-oneliners (new)
+ "Modifies *CACHE*. Merge updated oneliners into the *cache*, ensuring to remove old versions."
+ (setf *cache*
+ (nconc
+ new
+ (delete-if
+ (lambda (old-oneliner)
+ (find (oneliner-id old-oneliner)
+ new
+ :key #'oneliner-id
+ :test #'equal))
+ *cache*))))
+
+(defun get-cached (id-or-name)
+ "Looks up a oneliner instance by ID-OR-NAME using the current binding of *cache*. "
+ (find id-or-name
+ *cache*
+ :key (etypecase id-or-name
+ (integer #'oneliner-id)
+ (string #'oneliner-name))
+ :test #'equal))
+
+(defun remove-from-cache (id-or-name)
+ "Removes an item from the contents of *cache*."
+ (a:when-let (found (get-cached id-or-name))
+ (setf *cache* (delete found *cache*))))
+
+;;; DRAFTS VAR AND OPERATIONS
+
+(defvar *drafts* nil
+ "Holds a list of oneliner drafts yet to be sent to the server.")
+
+(defun fetch-draft (name)
+ "Fetch a draft by name form the *DRAFTS* association list."
+ (cdr (assoc name *drafts* :test #'string-equal)))
+
+(defun drop-draft (name)
+ "Drop a draft by NAME from the *DFRAFTS* association list."
+ (setf *DRAFTS* (delete (assoc name *DRAFTS* :test #'string-equal) *DRAFTS*)))
+
+(defun put-draft (name draft)
+ "Modifies *DRAFTS*, adding a new DRAFT associated with NAME. If NAME
+is already associated, that old association is deleted."
+ (drop-draft name)
+ (push (cons name draft) *drafts*))
+
;;; LOADING AND SAVING STATE
(defun config-file ()
@@ -63,6 +112,10 @@
"Returns the pathname holding the location of the cache."
(merge-pathnames ".cache/oneliners.cache" (user-homedir-pathname)))
+(defun drafts-file ()
+ "Returns the pathame holding the location of the oneliner drafts file."
+ (merge-pathnames ".cache/oneliners.drafts" (user-homedir-pathname)))
+
(defun wipe-cache ()
"Deletes the cache, if present."
(uiop:delete-file-if-exists (cached-oneliners-file)))
@@ -94,6 +147,12 @@ CACHED-ONELINERS-FILE. NIL if there is no such file."
:shell (prompt "With which shell should oneliners be run? "
:prefill "bash")))
+(defun read-drafts-file ()
+ (read-from-file (drafts-file)))
+
+(defun write-drafts-to-disk ()
+ (print-to-file *drafts* (drafts-file)))
+
(defun ensure-config ()
"Ensures that a configuration file exists on disk, prompting the
user for some input if it does not."
@@ -101,29 +160,6 @@ user for some input if it does not."
(read-config-file)
(make-fresh-config)))
-;;; GETTING AND SETTING STATE, DYNAMICALLY BOUND
-
-(defun merge-oneliners (new)
- "Modifies *CACHE*. Merge updated oneliners into the *cache*, ensuring to remove old versions."
- (setf *cache*
- (nconc
- new
- (delete-if
- (lambda (old-oneliner)
- (find (oneliner-id old-oneliner)
- new
- :key #'oneliner-id
- :test #'equal))
- *cache*))))
-
-(defun get-cached (id-or-name)
- "Looks up a oneliner instance by ID-OR-NAME using the current binding of *cache*. "
- (find id-or-name
- *cache*
- :key (etypecase id-or-name
- (integer #'oneliner-id)
- (string #'oneliner-name))
- :test #'equal))
;;; STATE LOADING MACRO
@@ -132,13 +168,15 @@ user for some input if it does not."
sets the api's *host* variable. If BODY produces no errors, the "
`(let* ((*config* (ensure-config))
(*cache* (read-cache-file))
+ (*drafts* (read-drafts-file))
(api:*host* (config-host *config*)))
- (assert api:*host* () "ol must be configured with a server host.")
- (set-term-width)
(handler-case
(progn
+ (assert api:*host* () "ol must be configured with a server host.")
+ (set-term-width)
,@body
;; only if there is no error do we save the local state.
+ (write-drafts-to-disk)
(write-cache-to-disk)
(write-config-to-disk))
(error (e)