Skip to content

Commit

Permalink
Added the locate module
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandbernard committed Jul 5, 2020
1 parent 54ed343 commit e44ae48
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ This module allows you to start applications on linux.
#### URL module
This module allows you to open urls.

#### Locate
This module allows you to search for files using locate.
3 changes: 3 additions & 0 deletions src/common/local/english.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ const translation_english = {

url: 'URL module',
url_description: 'This module allows you to open urls',

locate: 'Locate',
locate_description: 'This module allows you to search for files using locate',
};

export default translation_english;
3 changes: 3 additions & 0 deletions src/main/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export let config = {
active: true,
quality: 1.0,
},
locate: {
active: true,
},
},
};

Expand Down
28 changes: 19 additions & 9 deletions src/main/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import HtmlModule from "./modules/html";
import CalculatorModule from "./modules/calculator";
import LinuxApplicationModule from "./modules/linux-applications";
import UrlModule from "./modules/url";
import LocateModule from "./modules/locate";

const modules = {
marvin_quote: MarvinQuoteModule,
Expand All @@ -19,6 +20,7 @@ const modules = {
calculator: CalculatorModule,
linux_applications: LinuxApplicationModule,
url: UrlModule,
locate: LocateModule,
};

let last_query_timeout = null;
Expand All @@ -35,25 +37,33 @@ export function deinitModules() {
return Promise.all(Object.values(modules).map((module) => module.deinit && module.deinit()));
}

let exec_id = 0;

export function searchQuery(query, callback) {
return new Promise((resolve) => {
exec_id++;
const beginn_id = exec_id;
clearTimeout(last_query_timeout);
last_query_timeout = setTimeout(async () => {
let results = [];
let lock = new AsyncLock();
await Promise.all(Object.keys(modules).filter((id) => modules[id].valid(query)).map((id) => {
return new Promise((resolve) => lock.acquire('results', async () => {
let result = (await modules[id].search(query));
results = results
.concat(result.map((option) => ({ ...option, module: id })))
.filter((option) => option.quality > 0)
.sort((a, b) => b.quality - a.quality)
.slice(0, config.general.max_results);
// callback(results);
resolve();
if(exec_id === beginn_id) {
let result = (await modules[id].search(query));
results = results
.concat(result.map((option) => ({ ...option, module: id })))
.filter((option) => option.quality > 0)
.sort((a, b) => b.quality - a.quality)
.slice(0, config.general.max_results);
callback(results);
resolve();
}
}));
}));
callback(results);
if(exec_id === beginn_id) {
callback(results);
}
resolve();
}, config.general.debounce_time);
});
Expand Down
46 changes: 46 additions & 0 deletions src/main/modules/locate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

import { stringMatchQuality } from "../../common/util";
import { config } from "../config";
import { stat, exists, readdir } from "fs";
import path from 'path';
import { exec } from "child_process";
import { app } from "electron";

const LocateModule = {
valid: (query) => {
return config.modules.locate.active && query.length >= 1;
},
search: (query) => {
return new Promise((resolve) => {
exec(`locate -i -e -b ${query}`, async (_, stdout, __) => {
if(stdout) {
resolve(await Promise.all(stdout.split('\n').map((file) => {
return new Promise(async (resolve) => {
let option = {
type: 'icon_list_item',
uri_icon: (await app.getFileIcon(file)).toDataURL(),
primary: path.basename(file),
secondary: file,
executable: true,
quality: query[query.length - 1] === '/' ? 0.25 : 0.5 * stringMatchQuality(path.basename(query), path.basename(file)),
file: file,
};
resolve(option);
});
})));
} else {
resolve([]);
}
});
});
},
execute: (option) => {
return new Promise((resolve) => {
exec(`xdg-open ${option.file}`, () => {
resolve();
})
});
},
}

export default LocateModule;
4 changes: 1 addition & 3 deletions src/main/modules/url.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

import { stringMatchQuality } from "../../common/util";
import { config } from "../config";
import { exec } from 'child_process';
import { getTranslation } from "../../common/local/locale";

function isValidUrl(str) {
let pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
Expand Down Expand Up @@ -41,7 +39,7 @@ const UrlModule = {
return new Promise((resolve) => {
exec(`xdg-open ${option.url}`, () => {
resolve();
})
});
});
},
}
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/settings/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const config_definition = [
{ name: 'active', type: 'boolean' },
{ name: 'quality', type: 'quality' },
], type: 'page', description: 'url_description' },
{ name: 'locate', active: 'active', options: [
{ name: 'active', type: 'boolean' },
], type: 'page', description: 'locate_description' },
], type: 'subheader' }
];

Expand Down

0 comments on commit e44ae48

Please sign in to comment.