launcher: better scheme search
This commit is contained in:
parent
5455cc3af5
commit
b4dc9e13cb
2 changed files with 28 additions and 8 deletions
|
|
@ -9,12 +9,18 @@ import QtQuick
|
||||||
Searcher {
|
Searcher {
|
||||||
id: root
|
id: root
|
||||||
|
|
||||||
function transformSearch(search: string): var {
|
function transformSearch(search: string): string {
|
||||||
return search.slice(`${Config.launcher.actionPrefix}scheme `.length);
|
return search.slice(`${Config.launcher.actionPrefix}scheme `.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function selector(item: var): string {
|
||||||
|
return `${item.name} ${item.flavour}`;
|
||||||
|
}
|
||||||
|
|
||||||
list: schemes.instances
|
list: schemes.instances
|
||||||
useFuzzy: Config.launcher.useFuzzy.schemes
|
useFuzzy: Config.launcher.useFuzzy.schemes
|
||||||
|
keys: ["name", "flavour"]
|
||||||
|
weights: [0.9, 0.1]
|
||||||
|
|
||||||
Variants {
|
Variants {
|
||||||
id: schemes
|
id: schemes
|
||||||
|
|
|
||||||
|
|
@ -10,18 +10,31 @@ Singleton {
|
||||||
property bool useFuzzy: false
|
property bool useFuzzy: false
|
||||||
property var extraOpts: ({})
|
property var extraOpts: ({})
|
||||||
|
|
||||||
|
// Extra stuff for fuzzy
|
||||||
|
property list<string> keys: [key]
|
||||||
|
property list<real> weights: [1]
|
||||||
|
|
||||||
readonly property var fzf: useFuzzy ? [] : new Fzf.Finder(list, Object.assign({
|
readonly property var fzf: useFuzzy ? [] : new Fzf.Finder(list, Object.assign({
|
||||||
selector: e => e[key]
|
selector
|
||||||
}, extraOpts))
|
}, extraOpts))
|
||||||
readonly property list<var> fuzzyPrepped: useFuzzy ? list.map(e => ({
|
readonly property list<var> fuzzyPrepped: useFuzzy ? list.map(e => {
|
||||||
[key]: e[key],
|
const obj = {
|
||||||
_item: e
|
_item: e
|
||||||
})) : []
|
};
|
||||||
|
for (const k of keys)
|
||||||
|
obj[k] = Fuzzy.prepare(e[k]);
|
||||||
|
return obj;
|
||||||
|
}) : []
|
||||||
|
|
||||||
function transformSearch(search: string): string {
|
function transformSearch(search: string): string {
|
||||||
return search;
|
return search;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function selector(item: var): string {
|
||||||
|
// Only for fzf
|
||||||
|
return item[key];
|
||||||
|
}
|
||||||
|
|
||||||
function query(search: string): list<var> {
|
function query(search: string): list<var> {
|
||||||
search = transformSearch(search);
|
search = transformSearch(search);
|
||||||
if (!search)
|
if (!search)
|
||||||
|
|
@ -30,12 +43,13 @@ Singleton {
|
||||||
if (useFuzzy)
|
if (useFuzzy)
|
||||||
return Fuzzy.go(search, fuzzyPrepped, Object.assign({
|
return Fuzzy.go(search, fuzzyPrepped, Object.assign({
|
||||||
all: true,
|
all: true,
|
||||||
key
|
keys,
|
||||||
|
scoreFn: r => weights.reduce((a, w, i) => a + r[i].score * w, 0)
|
||||||
}, extraOpts)).map(r => r.obj._item);
|
}, extraOpts)).map(r => r.obj._item);
|
||||||
|
|
||||||
return fzf.find(search).sort((a, b) => {
|
return fzf.find(search).sort((a, b) => {
|
||||||
if (a.score === b.score)
|
if (a.score === b.score)
|
||||||
return a.item[key].trim().length - b.item[key].trim().length;
|
return selector(a.item).trim().length - selector(b.item).trim().length;
|
||||||
return b.score - a.score;
|
return b.score - a.score;
|
||||||
}).map(r => r.item);
|
}).map(r => r.item);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue