define command parameters as array not kv table

a command should be able to say in which order it wants to ask for
unsupplied parameter values
This commit is contained in:
Daniel Barlow 2022-12-23 19:48:17 +00:00
parent bdd4507692
commit 8d871b20b8
2 changed files with 22 additions and 8 deletions

View File

@ -5,22 +5,36 @@
(local Buffer (require :buffer))
(fn define-command [name function params]
(fn by-pairs [a]
(let [iter (fn [_ a]
(match a
[k v & rest] (values rest k v)
_ nil))]
(values iter a a)))
(fn define-command [name function ordered-params]
;; required parameter names and default arguments
(let [v {:name name :function function :params params}]
(let [param-names (icollect [_ name val (by-pairs ordered-params)]
name)
params (collect [_ name val (by-pairs ordered-params)]
(values name val))
v {: name
: function
: param-names
: params}]
(tset commands name v)))
(define-command
"quit-browser"
#(Gtk.main_quit) {})
#(Gtk.main_quit) [])
(define-command
"visit-location"
(fn [{:url url :buffer buffer}]
(let [b (Buffer.find buffer)] (: b :visit url)))
{:buffer (fn [] (. (Buffer.current) :name))
[:buffer (fn [] (. (Buffer.current) :name))
:url #(do "http://www.example.com")
})
])
(fn find-command [name]
(. commands name))
@ -38,7 +52,7 @@
(fn next-param [command params]
(accumulate [v nil
k _ (pairs command.params)
_ k (ipairs command.param-names)
&until v]
(if (. params k) nil k)))

View File

@ -5,12 +5,12 @@
(var happened false)
(fn before [] (set happened false) (Command.reset-state))
(Command.define-command "no-args-command" #(set happened true))
(Command.define-command "no-args-command" #(set happened true) [])
(Command.define-command
"multiply"
(fn [{: a : b }] (set happened (* (tonumber a) (tonumber b))))
{:a #(do "3") :b #(do "2")})
[:a #(do "3") :b #(do "2")])
(before)
(let [(ok err)