From ce26c8a75460948bccf412b3c559ea9a5777131f Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Sat, 19 Jul 2025 14:25:39 +1000 Subject: feat: fzf-like search instead of fuzzy Also add license for fuzzysort lib --- utils/Searcher.qml | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 utils/Searcher.qml (limited to 'utils/Searcher.qml') 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 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 fuzzyPrepped: useFuzzy ? list.map(e => ({ + [key]: e[key], + _item: e + })) : [] + + function transformSearch(search: string): string { + return search; + } + + function query(search: string): list { + 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); + } +} -- cgit v1.2.3-freya