aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/client.lisp38
-rw-r--r--lib/state.lisp70
2 files changed, 77 insertions, 31 deletions
diff --git a/lib/client.lisp b/lib/client.lisp
index fe29932..8d40fd5 100644
--- a/lib/client.lisp
+++ b/lib/client.lisp
@@ -131,8 +131,18 @@ running the body. If such a oneliner can be found."
(prompt "Name (leave blank for none): "
:expect 'valid-oneliner-name-p
:retry-text "Must begin with a letter contain only letters, numbers, - and _.")))
+
+ (draft-name
+ (unless (y-or-n-p "Upload immediately instead of keeping a draft?")
+ (if (plusp (length name))
+ name
+ (prompt "No name was provided, name this draft: "
+ :expect 'valid-oneliner-name-p
+ :retry-text "Must begin with a letter contain only letters, numbers, - and _."))))
+
(init-tags
(parse-oneliner-tags oneliner-string))
+
(brief
(prompt "Brief Description: "
:expect 'valid-brief-description-p
@@ -153,17 +163,31 @@ running the body. If such a oneliner can be found."
(when (y-or-n-p "Provide an explanation?")
(string-from-editor
(format nil "~a~%~%" oneliner-string)))))
- (api:request-with
- (:body (jonathan:to-json
- (list :oneliner oneliner-string
- :name (if (plusp (length name)) name :null)
+ (if draft-name
+ ;; if this is a draft, save it to disk.
+ (progn
+ (put-draft draft-name
+ (make-oneliner
+ :oneliner oneliner-string
+ :name name
:tags tags
:brief brief
:explanation explanation
:runstyle runstyle))
- :content-type "application/json")
- (api:post--oneliner :token (api-token)) ;TODO: update api to return the instance created.
- (format t "Added~%"))))
+ (format t "Saved draft ~a~%" draft-name))
+ ;; otherwise, format the oneliner as json and make a request
+ ;; to create a new oneliner in the wiki
+ (api:request-with
+ (:body (jonathan:to-json
+ (list :oneliner oneliner-string
+ :name (if (plusp (length name)) name :null)
+ :tags tags
+ :brief brief
+ :explanation explanation
+ :runstyle runstyle))
+ :content-type "application/json")
+ (api:post--oneliner :token (api-token)) ;TODO: update api to return the instance created.
+ (format t "Added Oneliner~%")))))
;;; EDITING ONELINERS
diff --git a/lib/state.lisp b/lib/state.lisp
index a156d59..123bd79 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,54 @@
(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))
+
+
+;;; 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*)))
+
+(defun drop-draft (name)
+ "Drop a draft by NAME from the *DFRAFTS* association list."
+ (setf *DRAFTS* (delete (assoc name *DRAFTS*) *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 ()
@@ -105,29 +150,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