From 8d871b20b8af79d380a55e7c2f2e79eb5314d6aa Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Fri, 23 Dec 2022 19:48:17 +0000 Subject: [PATCH] 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 --- command.fnl | 26 ++++++++++++++++++++------ test/command.fnl | 4 ++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/command.fnl b/command.fnl index d23fdbf..2a02ea7 100644 --- a/command.fnl +++ b/command.fnl @@ -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))) diff --git a/test/command.fnl b/test/command.fnl index e258e2f..621fb71 100644 --- a/test/command.fnl +++ b/test/command.fnl @@ -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)