search: add levelshtein distance based search

This commit is contained in:
end-4
2025-05-26 22:52:25 +02:00
parent a6556f3890
commit 02a3434e58
5 changed files with 172 additions and 6 deletions
+15 -1
View File
@@ -1,6 +1,8 @@
pragma Singleton
import "root:/modules/common"
import "root:/modules/common/functions/fuzzysort.js" as Fuzzy
import "root:/modules/common/functions/levendist.js" as Levendist
import Quickshell
import Quickshell.Io
@@ -9,16 +11,28 @@ import Quickshell.Io
*/
Singleton {
id: root
property bool sloppySearch: ConfigOptions?.search.sloppy ?? false
property real scoreThreshold: 0.2
readonly property list<DesktopEntry> list: Array.from(DesktopEntries.applications.values)
.sort((a, b) => a.name.localeCompare(b.name))
readonly property var preppedNames: list.map(a => ({
name: Fuzzy.prepare(`${a.name} `),
entry: a
}))
function fuzzyQuery(search: string): var { // Idk why list<DesktopEntry> doesn't work
if (root.sloppySearch) {
const results = list.map(obj => ({
entry: obj,
score: Levendist.computeScore(obj.name.toLowerCase(), search.toLowerCase())
})).filter(item => item.score > root.scoreThreshold)
.sort((a, b) => b.score - a.score)
return results
.map(item => item.entry)
}
return Fuzzy.go(search, preppedNames, {
all: true,
key: "name"
+14 -4
View File
@@ -2,6 +2,8 @@ pragma Singleton
pragma ComponentBehavior: Bound
import "root:/modules/common/functions/fuzzysort.js" as Fuzzy
import "root:/modules/common/functions/levendist.js" as Levendist
import "root:/modules/common/functions/string_utils.js" as StringUtils
import "root:/modules/common"
import "root:/"
import QtQuick
@@ -10,21 +12,29 @@ import Quickshell.Io
Singleton {
id: root
property bool sloppySearch: ConfigOptions?.search.sloppy ?? false
property real scoreThreshold: 0.2
property list<string> entries: []
property string highlightPrefix: `<b><font color="${Appearance.m3colors.m3primary}">`
property string highlightSuffix: `</font></b>`
readonly property var preparedEntries: entries.map(a => ({
name: Fuzzy.prepare(`${a}`),
entry: a
}))
function fuzzyQuery(search: string): var {
if (root.sloppySearch) {
const results = entries.slice(0, 100).map(str => ({
entry: str,
score: Levendist.computeTextMatchScore(str.toLowerCase(), search.toLowerCase())
})).filter(item => item.score > root.scoreThreshold)
.sort((a, b) => b.score - a.score)
return results
.map(item => item.entry)
}
return Fuzzy.go(search, preparedEntries, {
all: true,
key: "name"
}).map(r => {
return r.obj.entry
// console.log(JSON.stringify(r))
// return r.highlight(highlightPrefix, highlightSuffix);
});
}