Skip to content

Commit

Permalink
Merge pull request #15 from rolandbernard/devel
Browse files Browse the repository at this point in the history
v0.0.9
  • Loading branch information
rolandbernard committed Nov 2, 2020
2 parents 480ce48 + c03b98e commit 859cc2a
Show file tree
Hide file tree
Showing 21 changed files with 90 additions and 53 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "marvin",
"version": "0.0.8",
"version": "0.0.9",
"license": "MIT",
"scripts": {
"dev": "electron-webpack dev",
Expand Down Expand Up @@ -32,7 +32,7 @@
},
"devDependencies": {
"@babel/preset-react": "^7.10.1",
"electron": "8.2.4",
"electron": "8.5.2",
"electron-builder": "^22.4.1",
"electron-webpack": "^2.8.2",
"webpack": "~4.42.1"
Expand Down
52 changes: 26 additions & 26 deletions src/common/util.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@

export function stringMatchQuality(text, pattern) {
// TODO: Improve this
text = text.toUpperCase();
pattern = pattern.toUpperCase();
if (text === pattern) {
return 0.9;
} else if(pattern.startsWith(text)) {
return 0.7 + 0.2 * (text.length / pattern.length);
} else if (pattern.includes(text)) {
return 0.5 + 0.4 * (text.length / pattern.length);
} else {
return 0;
}
// TODO: Improve this
text = text.toUpperCase();
pattern = pattern.toUpperCase();
if (text === pattern) {
return 0.9;
} else if (pattern.startsWith(text)) {
return 0.7 + 0.2 * (text.length / pattern.length);
} else if (pattern.includes(text)) {
return 0.5 + 0.4 * (text.length / pattern.length);
} else {
return 0;
}
}

function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
return (item && typeof item === 'object' && !Array.isArray(item));
}

export function mergeDeep(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (!sources.length) return target;
const source = sources.shift();

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}

return mergeDeep(target, ...sources);
return mergeDeep(target, ...sources);
}
15 changes: 15 additions & 0 deletions src/main/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,42 @@ export let config = {
modules: {
linux_system: {
active: true,
prefix: '',
},
folders: {
active: true,
prefix: '',
directories: [ "/", app.getPath('home') ],
},
html: {
active: false,
prefix: '',
entries: [ ],
},
calculator: {
active: true,
prefix: '',
quality: 1.0,
},
linux_applications: {
active: true,
prefix: '',
directories: [ "/usr/share/applications/", path.join(app.getPath('home'), '.local/share/applications/') ],
refresh_interval_min: 30,
},
url: {
active: true,
prefix: '',
quality: 1.0,
},
locate: {
active: false,
prefix: '',
search_limit: 1000,
},
shortcuts: {
active: false,
prefix: '',
shortcuts: [ ],
},
command: {
Expand All @@ -60,29 +68,36 @@ export let config = {
},
scripts: {
active: false,
prefix: '',
entries: [ ],
},
clipboard: {
active: false,
prefix: '',
refresh_time: 20,
maximum_history: 1000,
},
deepl: {
active: false,
prefix: '',
},
linux_windows: {
active: false,
prefix: '',
},
google_translate: {
active: false,
prefix: '',
},
duckduckgo: {
active: false,
prefix: '',
debounce_time: 500,
quality: 0.1,
},
history: {
active: false,
prefix: '',
quality: 0.1,
maximum_history: 1000,
},
Expand Down
13 changes: 11 additions & 2 deletions src/main/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,21 @@ export function searchQuery(query, callback) {
const begin_id = exec_id;
clearTimeout(last_query_timeout);
last_query_timeout = setTimeout(async () => {
query = query.trim();
let results = [];
let lock = new AsyncLock();
await Promise.all(Object.keys(modules).filter((id) => modules[id].valid(query)).map((id) => {
let to_eval = Object.keys(modules).filter((id) => config.modules[id]?.prefix
? config.modules[id].active && query.startsWith(config.modules[id].prefix) : false);
if(to_eval.length === 0) {
to_eval = Object.keys(modules).filter((id) => config.modules[id]
? config.modules[id].active && !config.modules[id].prefix : true);
}
await Promise.all(to_eval.filter((id) => config.modules[id]?.prefix
? modules[id].valid(query.replace(config.modules[id].prefix, '').trim()) : modules[id].valid(query))
.map((id) => {
return new Promise(async (resolv) => {
try {
let result = (await modules[id].search(query));
let result = (await modules[id].search(config.modules[id]?.prefix ? query.replace(config.modules[id].prefix, '').trim() : query));
lock.acquire('results', () => {
if (exec_id === begin_id) {
results = results
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getTranslation } from '../../common/local/locale';

const CalculatorModule = {
valid: (query) => {
return config.modules.calculator.active && query.trim().length >= 1;
return query.trim().length >= 1;
},
search: async (query) => {
const ret = [];
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const ClipboardModule = {
clearInterval(interval);
},
valid: (query) => {
return config.modules.clipboard.active && query.trim().length > 0;
return query.trim().length > 0;
},
search: async (query) => {
const clipboard_match = stringMatchQuality(query, getTranslation(config, 'clipboard'));
Expand Down
4 changes: 1 addition & 3 deletions src/main/modules/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ 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;
return query.length >= 1;
},
search: async (query) => {
return [
Expand Down
4 changes: 2 additions & 2 deletions src/main/modules/deepl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let cancel_last = null;
let last_lang = null;

let languages = {
'en-EN': [ 'English', 'Inglese', 'Englisch' ],
'en-US': [ 'English', 'Inglese', 'Englisch' ],
'fr-FR': [ 'French', 'Francese', 'Französisch' ],
'es-ES': [ 'Spanish', 'Spagnolo', 'Spanisch' ],
'pt-PT': [ 'Portuguese', 'Portugiesisch', 'Portoghese' ],
Expand Down Expand Up @@ -68,7 +68,7 @@ const DeeplModule = {
}
},
valid: (query) => {
if (config.modules.deepl.active && page) {
if (page) {
if (cancel_last) {
cancel_last();
cancel_last = null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/duckduckgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let last_search = null;

const DuckduckgoModule = {
valid: (query) => {
return config.modules.duckduckgo.active && query.trim().length >= 1;
return query.trim().length >= 1;
},
search: (query) => {
clearTimeout(last_search);
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/folders.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { exec } from "child_process";

const FoldersModule = {
valid: (query) => {
return config.modules.folders.active && query.trim().length >= 1;
return query.trim().length >= 1;
},
search: async (query) => {
return (await Promise.all(config.modules.folders.directories.map(async (directory) => {
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/google-translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const GoogleTranslateModule = {
}
},
valid: (query) => {
if (config.modules.google_translate.active && page) {
if (page) {
if (cancel_last) {
cancel_last();
cancel_last = null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const HistoryModule = {
}
},
valid: (query) => {
return config.modules.history.active && query.trim().length == 0;
return query.trim().length == 0;
},
search: async (query) => {
return execute_history.map((option) => ({
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { config } from "../config";

const HtmlModule = {
valid: () => {
return config.modules.html.active;
return true;
},
search: async (query) => {
return config.modules.html.entries.map((entry) => ({
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/linux-applications.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ const LinuxApplicationModule = {
clearInterval(update_interval);
},
valid: (query) => {
return config.modules.linux_applications.active && query.trim().length >= 1;
return query.trim().length >= 1;
},
search: async (query) => {
return applications.map((app) => {
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/linux-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getTranslation } from "../../common/local/locale";

const LinuxSystemModule = {
valid: (query) => {
return config.modules.linux_system.active && query.trim().length >= 1;
return query.trim().length >= 1;
},
search: async (query) => {
return [
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/linux-windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { exec } from 'child_process';

const LinuxWindowsModule = {
valid: (query) => {
return config.modules.linux_windows.active && query.trim().length >= 1;
return query.trim().length >= 1;
},
search: (query) => {
return new Promise((resolve) => {
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/locate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { app } from "electron";

const LocateModule = {
valid: (query) => {
return config.modules.locate.active && query.trim().length >= 1;
return query.trim().length >= 1;
},
search: (query) => {
return new Promise((resolve) => {
Expand Down
4 changes: 2 additions & 2 deletions src/main/modules/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { exec } from "child_process";
import { stringMatchQuality } from '../../common/util';

const ScriptsModule = {
valid: (query) => {
return config.modules.scripts.active;
valid: () => {
return true;
},
search: async (query) => {
return config.modules.scripts.entries.map((entry) => ({
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function completeUrl(str) {

const UrlModule = {
valid: (query) => {
return config.modules.url.active && isValidUrl(query);
return isValidUrl(query);
},
search: async (query) => {
return [{
Expand Down
Loading

0 comments on commit 859cc2a

Please sign in to comment.