From e4ed51e137e7849c4ef9e4f434ac797f09ea0a2d Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Fri, 30 Dec 2022 17:44:53 +0000 Subject: [PATCH] add gtk stuff to show completions as user types --- buffer.fnl | 5 +++++ command.fnl | 42 +++++++++++++++++++++++++++++++++++++----- test/command.fnl | 4 ++-- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/buffer.fnl b/buffer.fnl index 8b3628d..d55957a 100644 --- a/buffer.fnl +++ b/buffer.fnl @@ -34,4 +34,9 @@ (tset buffers name b) b)) :find (fn [term] (. buffers term)) + ;; will rename this to "find" once we've got rid of the + ;; only remaining call to the existing Buffer.find + :match (fn [s] (collect [name buffer (pairs buffers)] + (if (string.find name s) + (values name buffer)))) }) diff --git a/command.fnl b/command.fnl index 35eb4eb..ade75ac 100644 --- a/command.fnl +++ b/command.fnl @@ -23,8 +23,10 @@ (define-command "visit-location" - [[:buffer #[$1] (fn [f] f.buffer.name)] - [:url #[$1] #(do "http://www.example.com")] + [[:buffer + Buffer.match + #"main"] + [:url #(doto {} (tset $1 $1)) #(do "http://www.example.com")] ] (fn [{:url url :buffer buffer}] (let [b (Buffer.find buffer)] (: b :visit url)))) @@ -98,11 +100,30 @@ :default (and param (param.default self.frame)) })) -(fn update-widget-state [{ : entry : prompt} result] +(fn update-widget-state [{ : entry : completions-widget : prompt} result] (set prompt.label (or result.prompt "")) (set entry.sensitive result.active) + (if (not result.active) + (completions-widget:hide)) (set entry.text (or result.default result.error ""))) +(fn on-input [self str] + (match self.state + {:command c :this-param param-name} + (let [parent self.completions-widget + { : completer} (. c.params param-name) + completions (completer str)] + (parent:foreach #(parent:remove $1)) + (each [text _w (pairs completions)] + (parent:add (Gtk.Button { + :label text + :on_clicked + #(update-widget-state self (self:on-activate text)) + }))) + (parent:show_all) + ))) + + (fn activate [{: state : entry : prompt}] (tset state :active true) (set entry.sensitive true) @@ -130,21 +151,32 @@ (let [entry (Gtk.Entry {:sensitive false }) prompt (Gtk.Label { :label ""}) box (Gtk.Box { + :orientation Gtk.Orientation.VERTICAL + }) + hbox (Gtk.Box { :orientation Gtk.Orientation.HORIZONTAL }) + completions (Gtk.FlowBox) self { :state default-state : activate :active? (fn [self] self.state.active) + : on-input : on-activate : invoke-interactively : entry :widget box : prompt : frame + :completions-widget completions }] - (box:pack_start prompt false false 15) - (box:pack_start entry true true 5) + (hbox:pack_start prompt false false 15) + (hbox:pack_start entry true true 5) + (box:pack_start hbox true false 0) + (box:pack_start completions true true 0) + (tset entry :on_changed + (fn [event] + (self:on-input event.text))) (tset entry :on_activate (fn [event] (let [result (self:on-activate event.text)] diff --git a/test/command.fnl b/test/command.fnl index c21f049..dc11508 100644 --- a/test/command.fnl +++ b/test/command.fnl @@ -12,8 +12,8 @@ (Command.define-command "multiply" - [[:a #[$1] #(do "3")] - [:b #[$1] #(do "2")]] + [[:a #{$1 $1} #"3"] + [:b #{$1 $1} #"2"]] (fn [{: a : b }] (set happened (* (tonumber a) (tonumber b))))) (before)