summaryrefslogtreecommitdiff
path: root/utils/Searcher.qml
diff options
context:
space:
mode:
author2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2025-07-19 14:25:39 +1000
committer2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2025-07-19 14:25:39 +1000
commitce26c8a75460948bccf412b3c559ea9a5777131f (patch)
tree3ad3aed7243298f27d85f50434f0619e7eb34f3d /utils/Searcher.qml
parentreadme: update config example (diff)
downloadcaelestia-shell-ce26c8a75460948bccf412b3c559ea9a5777131f.tar.gz
caelestia-shell-ce26c8a75460948bccf412b3c559ea9a5777131f.tar.bz2
caelestia-shell-ce26c8a75460948bccf412b3c559ea9a5777131f.zip
feat: fzf-like search instead of fuzzy
Also add license for fuzzysort lib
Diffstat (limited to 'utils/Searcher.qml')
-rw-r--r--utils/Searcher.qml42
1 files changed, 42 insertions, 0 deletions
diff --git a/utils/Searcher.qml b/utils/Searcher.qml
new file mode 100644
index 0000000..8fe8dee
--- /dev/null
+++ b/utils/Searcher.qml
@@ -0,0 +1,42 @@
+import Quickshell
+
+import "scripts/fzf.js" as Fzf
+import "scripts/fuzzysort.js" as Fuzzy
+import QtQuick
+
+Singleton {
+ required property list<QtObject> list
+ property string key: "name"
+ property bool useFuzzy: false
+ property var extraOpts: ({})
+
+ readonly property var fzf: useFuzzy ? [] : new Fzf.Finder(list, Object.assign({
+ selector: e => e[key]
+ }, extraOpts))
+ readonly property list<var> fuzzyPrepped: useFuzzy ? list.map(e => ({
+ [key]: e[key],
+ _item: e
+ })) : []
+
+ function transformSearch(search: string): string {
+ return search;
+ }
+
+ function query(search: string): list<var> {
+ search = transformSearch(search);
+ if (!search)
+ return [...list];
+
+ if (useFuzzy)
+ return Fuzzy.go(search, fuzzyPrepped, Object.assign({
+ all: true,
+ key
+ }, extraOpts)).map(r => r.obj._item);
+
+ return fzf.find(search).sort((a, b) => {
+ if (a.score === b.score)
+ return a.item[key].trim().length - b.item[key].trim().length;
+ return b.score - a.score;
+ }).map(r => r.item);
+ }
+}