Skip to content

Commit

Permalink
Merge pull request #3 from rolandbernard/devel
Browse files Browse the repository at this point in the history
0.0.2
  • Loading branch information
rolandbernard committed Jul 10, 2020
2 parents 167369b + b5195b2 commit f0c00b3
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 39 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ This module allows you to open urls.

#### Locate
This module allows you to search for files using locate.

![screenshot](img/locate.png)

#### Shortcuts
This module allows you to define shortcuts to run shell scripts.

#### Command
This module allows you to execute shell commands.

![screenshot](img/command.png)
Binary file added img/command.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/locate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"devDependencies": {
"@babel/preset-react": "^7.10.1",
"electron": "8.2.0",
"electron": "8.2.4",
"electron-builder": "^22.4.1",
"electron-webpack": "^2.8.2",
"webpack": "~4.42.1"
Expand Down
11 changes: 11 additions & 0 deletions src/common/local/english.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ const translation_english = {

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

shortcuts: 'Shortcuts',
shortcuts_description: 'This module allows you to define shortcuts to run shell scripts',
shortcut: 'Shortcut',
script: 'Script',

command: 'Command',
command_description: 'This module allows you to execute shell commands',
prefix: 'Prefix',
execute: 'Execute',
execute_in_terminal: 'Execute in terminal',
};

export default translation_english;
4 changes: 2 additions & 2 deletions src/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export function stringMatchQuality(text, pattern) {
if (text === pattern) {
return 0.9;
} else if(pattern.startsWith(text)) {
return 0.8;
return 0.7 + 0.2 * (text.length / pattern.length);
} else if (pattern.includes(text)) {
return 0.7;
return 0.5 + 0.4 * (text.length / pattern.length);
} else {
return 0;
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export let config = {
general: {
global_shortcut: 'Super+D',
language: 'en',
debounce_time: 0,
debounce_time: 100,
width: 600,
max_height: 500,
max_results: 200,
Expand Down Expand Up @@ -47,8 +47,15 @@ export let config = {
active: true,
quality: 1.0,
},
locate: {
shortcuts: {
active: false,
shortcuts: [
{ shortcut: 'Super+C', script: 'code' },
],
},
command: {
active: true,
prefix: '$',
},
},
};
Expand Down
15 changes: 10 additions & 5 deletions src/main/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import CalculatorModule from "./modules/calculator";
import LinuxApplicationModule from "./modules/linux-applications";
import UrlModule from "./modules/url";
import LocateModule from "./modules/locate";
import ShortcutModule from "./modules/shortcuts";
import CommandModule from './modules/command';

const modules = {
marvin_quote: MarvinQuoteModule,
Expand All @@ -21,22 +23,23 @@ const modules = {
linux_applications: LinuxApplicationModule,
url: UrlModule,
locate: LocateModule,
shortcuts: ShortcutModule,
command: CommandModule,
};

let last_query_timeout = null;

export function initModules() {
return Promise.all(Object.values(modules).map((module) => module.init && module.init()));
}

export function updateModules() {
return Promise.all(Object.values(modules).map((module) => module.update && module.update()));
export function updateModules(old_config) {
return Promise.all(Object.values(modules).map((module) => module.update && module.update(old_config)));
}

export function deinitModules() {
return Promise.all(Object.values(modules).map((module) => module.deinit && module.deinit()));
}

let last_query_timeout = null;
let exec_id = 0;

export function searchQuery(query, callback) {
Expand All @@ -48,7 +51,7 @@ export function searchQuery(query, callback) {
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 () => {
return new Promise((resolv) => lock.acquire('results', async () => {
if(exec_id === beginn_id) {
let result = (await modules[id].search(query));
results = results
Expand All @@ -57,8 +60,10 @@ export function searchQuery(query, callback) {
.sort((a, b) => b.quality - a.quality)
.slice(0, config.general.max_results);
callback(results);
} else {
resolve();
}
resolv();
}));
}));
if(exec_id === beginn_id) {
Expand Down
17 changes: 10 additions & 7 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,26 @@ function toggleMain() {
}
}

let last_loading = null;

async function startApp() {
const gotSingleInstanceLock = app.requestSingleInstanceLock();
if (gotSingleInstanceLock) {
loadConfig();
await initModules();
createMainWindow();
createSettingsWindow();

const ret = globalShortcut.register(config.general.global_shortcut, toggleMain);
if (!ret) {
console.error('Failed to register a global shortcut');
app.quit();
}
await initModules();
createMainWindow();
createSettingsWindow();

ipcMain.on('input-change', (_, query) => {
const loading = setTimeout(() => main_window.webContents.send('update-options', null), 500);
clearTimeout(last_loading);
last_loading = setTimeout(() => main_window.webContents.send('update-options', null), 250);
searchQuery(query, (results) => {
clearTimeout(loading);
clearTimeout(last_loading);
main_window.webContents.send('update-options', results);
});
});
Expand All @@ -111,8 +113,9 @@ async function startApp() {
new_config.general.global_shortcut = config.general.global_shortcut;
}
}
const old_config = new_config;
updateConfig(new_config);
await updateModules();
await updateModules(old_config);
});
} else {
console.error("Other instance is already running: quitting app.");
Expand Down
39 changes: 39 additions & 0 deletions src/main/modules/command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

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

const CommandModule = {
valid: (query) => {
return config.modules.command.active
&& query.startsWith(config.modules.command.prefix)
&& query.replace(config.modules.command.prefix, '').trim().length >= 1;
},
search: async (query) => {
return [
{
type: 'icon_list_item',
material_icon: 'code',
primary: query.replace(config.modules.command.prefix, '').trim(),
secondary: getTranslation(config, 'execute') + ': `' + query.replace(config.modules.command.prefix, '').trim() + '`',
executable: true,
quality: 1.0,
command: query.replace(config.modules.command.prefix, '').trim(),
},
{
type: 'icon_list_item',
material_icon: 'code',
primary: query.replace(config.modules.command.prefix, '').trim(),
secondary: getTranslation(config, 'execute_in_terminal') + ': `' + query.replace(config.modules.command.prefix, '').trim() + '`',
executable: true,
quality: 1.0,
command: `xterm -e "${query.replace(config.modules.command.prefix, '').trim().replace(/\"/g, '\\"')}"`,
},
];
},
execute: async (option) => {
exec(option.command);
},
}

export default CommandModule;
20 changes: 8 additions & 12 deletions src/main/modules/linux-applications.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,19 @@ const LinuxApplicationModule = {
primary: getProp(value, 'Name', name),
secondary: getProp(value, 'Comment', name),
executable: true,
quality: stringMatchQuality(query, name + desc + getProp(app.desktop, 'GenericName', '')),
quality: Math.max(stringMatchQuality(query, name),
0.75 * stringMatchQuality(query, desc),
0.75 * stringMatchQuality(query, getProp(app.desktop, 'GenericName', ''))),
app: value,
}));
}).reduce((a, b) => a.concat(b));
},
execute: (option) => {
return new Promise((resolve) => {
if (getProp(option.app, 'Terminal') === 'true') {
exec(`xterm -e '${getProp(option.app, 'Exec').replace(/\%./g, '')}'`, () => {
resolve();
})
} else {
exec(`${getProp(option.app, 'Exec').replace(/\%./g, '')}`, () => {
resolve();
})
}
});
if (getProp(option.app, 'Terminal') === 'true') {
exec(`xterm -e "${getProp(option.app, 'Exec').replace(/\%./g, '').replace(/\"/g, '\\"')}"`);
} else {
exec(`${getProp(option.app, 'Exec').replace(/\%./g, '')}`);
}
},
};

Expand Down
6 changes: 1 addition & 5 deletions src/main/modules/linux-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ const LinuxSystemModule = {
];
},
execute: (option) => {
return new Promise((resolve) => {
exec(option.command, () => {
resolve();
})
});
exec(option.command);
},
}

Expand Down
38 changes: 38 additions & 0 deletions src/main/modules/shortcuts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

import { config } from "../config";
import { globalShortcut } from "electron";
import { exec } from 'child_process';

const ShortcutModule = {
init: async () => {
if (config.modules.shortcuts.active) {
config.modules.shortcuts.shortcuts.forEach((shortcut) => {
try {
globalShortcut.register(shortcut.shortcut, () => {
exec(`sh <<< "${shortcut.script.replace(/\"/g, '\\"')}"`);
});
} catch (e) { }
});
}
},
update: async (old_config) => {
old_config.modules.shortcuts.shortcuts.forEach((shortcut) => {
try {
globalShortcut.unregister(shortcut.shortcut);
} catch (e) { }
});
await ShortcutModule.init();
},
deinit: async () => {
config.modules.shortcuts.shortcuts.forEach((shortcut) => {
try {
globalShortcut.unregister(shortcut.shortcut);
} catch (e) { }
});
},
valid: (query) => {
return false;
},
}

export default ShortcutModule;
11 changes: 11 additions & 0 deletions src/renderer/settings/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ const config_definition = [
{ name: 'locate', active: 'active', options: [
{ name: 'active', type: 'boolean' },
], type: 'page', description: 'locate_description' },
{ name: 'shortcuts', active: 'active', options: [
{ name: 'active', type: 'boolean' },
{ name: 'shortcuts', type: 'array', base: [
{ name: 'shortcut', type: 'shortcut' },
{ name: 'script', type: 'code' },
], default: { shortcut: '', script: '' } },
], type: 'page', description: 'shortcuts_description' },
{ name: 'command', active: 'active', options: [
{ name: 'active', type: 'boolean' },
{ name: 'prefix', type: 'text' },
], type: 'page', description: 'command_description' },
], type: 'subheader' }
];

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/settings/setting/code-setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function CodeSetting(props) {
<TextField
style={styles.text}
defaultValue={props.option}
rows={10}
rowsMax={7}
variant="outlined"
onChange={onUpdate}
multiline
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2935,10 +2935,10 @@ electron-webpack@^2.8.2:
webpack-merge "^4.2.2"
yargs "^15.3.1"

electron@8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/electron/-/electron-8.2.0.tgz#f3e3de23a6178b5ad7baa70f4814d6332a3212c2"
integrity sha512-mnV43gKCrCUMHLmGws/DU/l8LhaxrFD53A4ofwtthdCqOZWGIdk1+eMphiVumXR5a3lC64XVvmXQ2k28i7F/zw==
electron@8.2.4:
version "8.2.4"
resolved "https://registry.yarnpkg.com/electron/-/electron-8.2.4.tgz#c4e51ca8e84b5a5beaaabdae1024bd52ba487ba4"
integrity sha512-Lle0InIgSAHZxD5KDY0wZ1A2Zlc6GHwMhAxoHMzn05mndyP1YBkCYHc0TDDofzUTrsLFofduPjlknO5Oj9fTPA==
dependencies:
"@electron/get" "^1.0.1"
"@types/node" "^12.0.12"
Expand Down

0 comments on commit f0c00b3

Please sign in to comment.