aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Okay <okay@toyful.space>2022-03-13 13:56:49 -0500
committerColin Okay <okay@toyful.space>2022-03-13 13:56:49 -0500
commit7ae6cbff1875ea271fa1724d6e53bc3d3d48dd26 (patch)
tree6559b13feda3729cc677cb5d80553dc295b2ccc1
parent07183b5bbb4d2e65514e3ec3e7cdf7e421f97749 (diff)
support for running drafts; drafts accounted for in with-local-state
-rw-r--r--app/app.lisp10
-rw-r--r--lib/client.lisp18
-rw-r--r--lib/state.lisp10
3 files changed, 29 insertions, 9 deletions
diff --git a/app/app.lisp b/app/app.lisp
index ea11a5e..86c7328 100644
--- a/app/app.lisp
+++ b/app/app.lisp
@@ -104,7 +104,9 @@ export EDITOR=/usr/bin/zile
:argument-name "SECONDS"
:default-value 1
:typespec 'integer
- :description "How many seconds to wait for standard output before giving up."))
+ :description "How many seconds to wait for standard output before giving up.")
+ (flag :long-name "draft"
+ :description "Indicates that you wish to run a draft of a oneliner identified by IDENTIFIER."))
(group (:header "CLIPPING ONELINERS" :hidden t)
(text :contents "Usage: ol clip <IDENTIFIER> [ARGS...]")
(text :contents " ")
@@ -214,10 +216,12 @@ than the users."
(getopt :long-name "newest")))
(:run
(cli:run-item id-or-name (rest args)
- :timeout (getopt :long-name "timeout")))
+ :timeout (getopt :long-name "timeout")
+ :draftp (getopt :long-name "draft")))
(:clip
(cli:run-item id-or-name (rest args)
- :force-clip t))
+ :force-clip t
+ :draftp (getopt :long-name "draft")))
(:show
(cli:print-item-explanation id-or-name))
(:new
diff --git a/lib/client.lisp b/lib/client.lisp
index 8d40fd5..db19bda 100644
--- a/lib/client.lisp
+++ b/lib/client.lisp
@@ -45,12 +45,18 @@ not in the local cache, try to fetch from configured server."
(defmacro when-oneliner ((var name-or-id) &body body)
"Finds the oneliner with name-or-id and binds it to var before
running the body. If such a oneliner can be found."
- (assert (symbolp var))
(let ((nvar (gensym)))
`(let ((,nvar ,name-or-id))
(a:when-let (,var (the-oneliner ,nvar))
,@body))))
+(defmacro when-draft ((var name) &body body)
+ "Like when-oneliner but restricts itself to local drafts."
+ (let ((nvar (gensym)))
+ `(let ((,nvar ,name))
+ (a:when-let (,var (fetch-draft ,nvar))
+ ,@body))))
+
(defun newest-oneliners (&optional limit)
(let ((response
(if limit
@@ -76,10 +82,12 @@ running the body. If such a oneliner can be found."
(defvar *ol-output-timeout* 1)
-(defun run-item (ident args &key force-clip (timeout nil timeout-p))
- (when-oneliner (ol ident)
- (let ((*ol-output-timeout* (if timeout-p timeout *ol-output-timeout*)))
- (bind-vars-and-run-oneliner ol args force-clip))))
+(defun run-item (ident args &key force-clip (timeout nil timeout-p) draftp)
+ "Runs a oneliner identified by IDENT (if available) with arguments ARGS."
+ (let ((ol (if draftp (fetch-draft ident) (the-oneliner ident))))
+ (when ol
+ (let ((*ol-output-timeout* (if timeout-p timeout *ol-output-timeout*)))
+ (bind-vars-and-run-oneliner ol args force-clip)))))
(defun bind-vars-and-run-oneliner (ol args &optional force-clip)
(let* ((oneliner (oneliner-oneliner ol))
diff --git a/lib/state.lisp b/lib/state.lisp
index 123bd79..8ab6c51 100644
--- a/lib/state.lisp
+++ b/lib/state.lisp
@@ -86,7 +86,7 @@
(defun fetch-draft (name)
"Fetch a draft by name form the *DRAFTS* association list."
- (cdr (assoc name *drafts*)))
+ (cdr (assoc name *drafts* :test #'string-equal)))
(defun drop-draft (name)
"Drop a draft by NAME from the *DFRAFTS* association list."
@@ -143,6 +143,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."
@@ -158,6 +164,7 @@ 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)
@@ -165,6 +172,7 @@ sets the api's *host* variable. If BODY produces no errors, the "
(progn
,@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)