store transient args as something other than "--key=val"
pairs?
#311
Replies: 4 comments 4 replies
-
Well, that depends on what you consider "tonnes of headaches", but it definitely is a goal of Transient that this sort of thing is possible. On the other hand, no standard suffix class exists that handles this particular use-case, so you do get to define a class and a bunch of methods. I did that late last night, and to see how doable that was by trial and error, I did just that, starting with something like. (defclass my-alist-entry-infix (transient-infix)
((alist-key :initarg :alist-key)))
(transient-define-infix my-alist-entry ()
:class 'my-alist-entry-infix)
(transient-define-prefix my-alist ()
[("a" "aaa" my-alist-entry :alist-key aaa :class my-alist-entry)
("b" "bbb" my-alist-entry :alist-key bbb :class my-alist-entry)
("x" "echo" (lambda () (interactive) (message "%S" (transient-args 'my-alist))))]) After (cl-defmethod transient-init-value ((obj my-alist-entry-infix))
nil)
(cl-defmethod transient-format-value ((obj my-alist-entry-infix))
(propertize (prin1-to-string (oref obj value))
'face 'transient-value))
(cl-defmethod transient-prompt ((obj my-alist-entry-infix))
(format "Set %s: " (oref obj alist-key)))
(cl-defmethod transient-infix-value ((obj my-alist-entry-infix))
(cons (oref obj alist-key)
(oref obj value))) Of course that's just a minimal-viable approach, For a simple example where all the specialized methods are neatly kept in one place, see the |
Beta Was this translation helpful? Give feedback.
-
thanks a lot for mulling it over and sharing some code examples! |
Beta Was this translation helpful? Give feedback.
-
just for posterity, i think the first code block above gets the class wrong this works: (defclass my-alist-entry-infix (transient-infix)
((alist-key :initarg :alist-key)))
(transient-define-infix my-alist-entry ()
:class 'my-alist-entry-infix)
(transient-define-prefix my-alist ()
[("a" "aaa" my-alist-entry :alist-key aaa :class my-alist-entry-infix)
("b" "bbb" my-alist-entry :alist-key bbb :class my-alist-entry-infix)
("x" "echo" (lambda () (interactive) (message "%S" (transient-args 'my-alist))))]) |
Beta Was this translation helpful? Give feedback.
-
i have a small follow-up question, just because i don't fully grasp how transient handles what it calls "arguments". is it possible to not have to provide both unlike the example provided above, i wrote my code without defining an infix. in my code looks like this: (defclass mastodon-transient-field (tp-option-str)
((always-read :initarg :always-read :initform t))
"An infix option class for our options.
We always read.")
(transient-define-prefix mastodon-profile-fields ()
"A transient for setting profile fields."
:value
(lambda () (mastodon-transient-fetch-fields))
[:description
"Fields"
["Name"
("1 n" "" "fields.1.name" :alist-key fields.1.name :class mastodon-transient-field)
("2 n" "" "fields.2.name" :alist-key fields.2.name :class mastodon-transient-field)
("3 n" "" "fields.3.name" :alist-key fields.3.name :class mastodon-transient-field)
("4 n" "" "fields.4.name" :alist-key fields.4.name :class mastodon-transient-field)]
... i'm not sure if my "argument" strings here matter, as i'm using in another transient, i end up providing the same thing three times, as it also requires a description: ;; in a transient prefix definition, inside a group:
("n" "display name" "display_name" :alist-key display_name :class tp-option-str) |
Beta Was this translation helpful? Give feedback.
-
hi jonas, i started learning transient.el, which i've long enjoyed using in magit and forge.
i'm interested in creating menus that fetch a numerous settings from a REST API, then POSTing or PATCHing changes to them, which is bit of a deviation from the CLI args and variables model transient.el was designed for.
i have a few menus working, but the implementation still stores transient "argument" values in the format
"key=value"
(a string), and converting these to/from Elisp's parsed JSON, ie alists.i was wondering if it's possible/sane to try to handle transient's argument values as alists instead? ie as
((key1 . val1) (key2 . val2))
rather than("key1=val1" "key2=val2")
.would one just need to write some init/read methods or would it create tonnes of headaches everywhere in the package?
Beta Was this translation helpful? Give feedback.
All reactions