search: add math, shell command, and web search prefixes (#1795)

This commit is contained in:
end-4
2025-08-19 21:51:52 +07:00
parent 891a226fdb
commit 6f907f7961
2 changed files with 46 additions and 27 deletions
@@ -266,6 +266,9 @@ Singleton {
property string action: "/"
property string clipboard: ";"
property string emojis: ":"
property string math: "="
property string shellCommand: "$"
property string webSearch: "?"
}
}
@@ -83,7 +83,11 @@ Item { // Wrapper
id: nonAppResultsTimer
interval: Config.options.search.nonAppResultDelay
onTriggered: {
mathProcess.calculateExpression(root.searchingText);
let expr = root.searchingText;
if (expr.startsWith(Config.options.search.prefix.math)) {
expr = expr.slice(Config.options.search.prefix.math.length);
}
mathProcess.calculateExpression(expr);
}
}
@@ -310,7 +314,7 @@ Item { // Wrapper
};
}).filter(Boolean);
}
if (root.searchingText.startsWith(Config.options.search.prefix.emojis)) {
else if (root.searchingText.startsWith(Config.options.search.prefix.emojis)) {
// Clipboard
const searchString = root.searchingText.slice(Config.options.search.prefix.emojis.length);
return Emojis.fuzzyQuery(searchString).map(entry => {
@@ -346,10 +350,30 @@ Item { // Wrapper
fontType: "monospace",
materialSymbol: 'terminal',
execute: () => {
const cleanedCommand = root.searchingText.replace("file://", "");
let cleanedCommand = root.searchingText.replace("file://", "");
if (cleanedCommand.startsWith(Config.options.search.prefix.shellCommand)) {
cleanedCommand = cleanedCommand.slice(Config.options.search.prefix.shellCommand.length);
}
Quickshell.execDetached(["bash", "-c", searchingText.startsWith('sudo') ? `${Config.options.apps.terminal} fish -C '${cleanedCommand}'` : cleanedCommand]);
}
};
const webSearchResultObject = {
name: root.searchingText,
clickActionName: Translation.tr("Search"),
type: Translation.tr("Search the web"),
materialSymbol: 'travel_explore',
execute: () => {
let query = root.searchingText;
if (query.startsWith(Config.options.search.prefix.webSearch)) {
query = query.slice(Config.options.search.prefix.webSearch.length);
}
let url = Config.options.search.engineBaseUrl + query;
for (let site of Config.options.search.excludedSites) {
url += ` -site:${site}`;
}
Qt.openUrlExternally(url);
}
}
const launcherActionObjects = root.searchActions.map(action => {
const actionString = `${Config.options.search.prefix.action}${action.action}`;
if (actionString.startsWith(root.searchingText) || root.searchingText.startsWith(actionString)) {
@@ -366,7 +390,19 @@ Item { // Wrapper
return null;
}).filter(Boolean);
//////// Prioritized by prefix /////////
let result = [];
const startsWithNumber = /^\d/.test(root.searchingText);
const startsWithMathPrefix = root.searchingText.startsWith(Config.options.search.prefix.math);
const startsWithShellCommandPrefix = root.searchingText.startsWith(Config.options.search.prefix.shellCommand);
const startsWithWebSearchPrefix = root.searchingText.startsWith(Config.options.search.prefix.webSearch);
if (startsWithNumber || startsWithMathPrefix) {
result.push(mathResultObject);
} else if (startsWithShellCommandPrefix) {
result.push(commandResultObject);
} else if (startsWithWebSearchPrefix) {
result.push(webSearchResultObject);
}
//////////////// Apps //////////////////
result = result.concat(AppSearch.fuzzyQuery(root.searchingText).map(entry => {
@@ -378,30 +414,10 @@ Item { // Wrapper
////////// Launcher actions ////////////
result = result.concat(launcherActionObjects);
/////////// Math result & command //////////
const startsWithNumber = /^\d/.test(root.searchingText);
if (startsWithNumber) {
result.push(mathResultObject);
result.push(commandResultObject);
} else {
result.push(commandResultObject);
result.push(mathResultObject);
}
///////////////// Web search ////////////////
result.push({
name: root.searchingText,
clickActionName: Translation.tr("Search"),
type: Translation.tr("Search the web"),
materialSymbol: 'travel_explore',
execute: () => {
let url = Config.options.search.engineBaseUrl + root.searchingText;
for (let site of Config.options.search.excludedSites) {
url += ` -site:${site}`;
}
Qt.openUrlExternally(url);
}
});
/// Math result, command, web search ///
if (!startsWithShellCommandPrefix) result.push(commandResultObject);
if (!startsWithNumber && !startsWithMathPrefix) result.push(mathResultObject);
if (!startsWithWebSearchPrefix) result.push(webSearchResultObject);
return result;
}