commit 4b78434ea4ce0ed29bd56037613c0ef2471bb0ee
parent fe25203bad7e9b44e7c931c4aca0f6c8e682c157
Author: Daniel Barlow <dan@telent.net>
Date: Tue, 17 Jan 2023 11:30:10 +0000
extract search into a module, improve it
Diffstat:
3 files changed, 54 insertions(+), 28 deletions(-)
diff --git a/command.fnl b/command.fnl
@@ -69,34 +69,6 @@
#$1.buffer.name]]
(fn [{: buffer}] (buffer:back)))
-(fn url-escape [s]
- (string.gsub s
- "([^A-Za-z0-9_])"
- #(string.format "%%%02x" (string.byte $1))))
-
-(local searchers {
- :ddg (fn [term]
- (.. "https://duckduckgo.com/?q=" (url-escape term)))
- })
-
-
-(define-command
- "search"
- [[:term
- #[(completion {:text $1})]
- #""]
- [:buffer
- #(lume.map (Buffer.match $1) #(completion { :text $1.name :value $1 }))
- #$1.buffer.name]
- [:engine
- #[(completion {:text "Duck Duck Go" :value :ddg})
- (completion {:text "Google" :value :google})
- (completion {:text "Luai (yoyo.org)" :value :luai})]
- #"Duck Duck Go"]]
- (fn [{: term : engine : buffer}]
- (buffer:visit ((. searchers engine) term))))
-
-
(fn find-command [name]
(. commands name))
diff --git a/dunlin.fnl b/dunlin.fnl
@@ -6,6 +6,8 @@
(local Frame (require :frame))
(local Buffer (require :buffer))
+(local search (require :search))
+
;;; when we decide how to do an init file/rc file, this will go in it
(local my-keymap {
diff --git a/search.fnl b/search.fnl
@@ -0,0 +1,52 @@
+(local { : Gtk } (require :lgi))
+(local { : view } (require :fennel))
+(local lume (require :lume))
+
+(local {: define-command : completion } (require :command))
+(local Buffer (require :buffer))
+
+(fn url-escape [s]
+ (string.gsub s
+ "([^A-Za-z0-9_])"
+ #(string.format "%%%02x" (string.byte $1))))
+
+(local searchers
+ {:ddg
+ {:name "Duck Duck Go"
+ :function (fn [term]
+ (.. "https://duckduckgo.com/?q=" (url-escape term)))
+ }
+ :google
+ {:name "Google"
+ :function (fn [term]
+ (.. "https://www.google.com/search?q=" (url-escape term)))
+ }
+ :luai
+ {:name "Lua interactive reference manual"
+ :function (fn [term]
+ (.. "https://pgl.yoyo.org/luai/i/" (url-escape term)))
+ }
+ })
+
+
+(fn completions [term]
+ (icollect [keyword {: name : function} (pairs searchers)]
+ (if (string.match keyword term)
+ (completion {
+ :text keyword
+ :value keyword
+ :widget (Gtk.Button { :label name })
+ }))))
+
+
+(define-command
+ "search"
+ [[:term
+ #[(completion {:text $1})]
+ #""]
+ [:buffer
+ #(lume.map (Buffer.match $1) #(completion { :text $1.name :value $1 }))
+ #$1.buffer.name]
+ [:engine completions #:ddg]]
+ (fn [{: term : engine : buffer}]
+ (buffer:visit ((. (. searchers engine) :function) term))))