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:
parent
bdd4507692
commit
8d871b20b8
26
command.fnl
26
command.fnl
@ -5,22 +5,36 @@
|
|||||||
|
|
||||||
(local Buffer (require :buffer))
|
(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
|
;; 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)))
|
(tset commands name v)))
|
||||||
|
|
||||||
(define-command
|
(define-command
|
||||||
"quit-browser"
|
"quit-browser"
|
||||||
#(Gtk.main_quit) {})
|
#(Gtk.main_quit) [])
|
||||||
|
|
||||||
(define-command
|
(define-command
|
||||||
"visit-location"
|
"visit-location"
|
||||||
(fn [{:url url :buffer buffer}]
|
(fn [{:url url :buffer buffer}]
|
||||||
(let [b (Buffer.find buffer)] (: b :visit url)))
|
(let [b (Buffer.find buffer)] (: b :visit url)))
|
||||||
{:buffer (fn [] (. (Buffer.current) :name))
|
[:buffer (fn [] (. (Buffer.current) :name))
|
||||||
:url #(do "http://www.example.com")
|
:url #(do "http://www.example.com")
|
||||||
})
|
])
|
||||||
|
|
||||||
(fn find-command [name]
|
(fn find-command [name]
|
||||||
(. commands name))
|
(. commands name))
|
||||||
@ -38,7 +52,7 @@
|
|||||||
|
|
||||||
(fn next-param [command params]
|
(fn next-param [command params]
|
||||||
(accumulate [v nil
|
(accumulate [v nil
|
||||||
k _ (pairs command.params)
|
_ k (ipairs command.param-names)
|
||||||
&until v]
|
&until v]
|
||||||
(if (. params k) nil k)))
|
(if (. params k) nil k)))
|
||||||
|
|
||||||
|
@ -5,12 +5,12 @@
|
|||||||
(var happened false)
|
(var happened false)
|
||||||
(fn before [] (set happened false) (Command.reset-state))
|
(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
|
(Command.define-command
|
||||||
"multiply"
|
"multiply"
|
||||||
(fn [{: a : b }] (set happened (* (tonumber a) (tonumber b))))
|
(fn [{: a : b }] (set happened (* (tonumber a) (tonumber b))))
|
||||||
{:a #(do "3") :b #(do "2")})
|
[:a #(do "3") :b #(do "2")])
|
||||||
|
|
||||||
(before)
|
(before)
|
||||||
(let [(ok err)
|
(let [(ok err)
|
||||||
|
Loading…
Reference in New Issue
Block a user