Skip to content

Commit

Permalink
Merge aa8dd63 into 64c0414
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhas authored May 15, 2018
2 parents 64c0414 + aa8dd63 commit cf824d4
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ typings/
# dotenv environment variables file
.env

ueli.countstorage.json
52 changes: 52 additions & 0 deletions src/tests/unit/count-manager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as fs from "fs";
import { CountManager } from "./../../ts/count-manager";
import { CountLoader } from "./../../ts/count-loader";

const testStorage = {
"C:\\Path\\To\\Exe.exe": 10,
"D:\\Test\\Path": 40,
"E:\\Invalid\\Value": "lol",
};

const testLoader = {
loadCountFromCountFile: (): any => testStorage,
writeCountFile: (): void => {/* do nothing */},
} as CountLoader;

describe("CountManager", (): void => {
const count = new CountManager(testLoader);

it("Should set value of new path to 1", (): void => {
count.addCount("F:\\New\\Path");
expect(count.getCount("F:\\New\\Path")).toBe(1);
});

it("Should increase score of path to 11", (): void => {
count.addCount("C:\\Path\\To\\Exe.exe");
expect(count.getCount("C:\\Path\\To\\Exe.exe")).toBe(11);
});

it("Should keep score of path at 40", (): void => {
count.addCount("D:\\Test\\Path");
expect(count.getCount("D:\\Test\\Path")).toBe(40);
});

it("Should return an empty object after clearStorage", (): void => {
count.clearStorage();
expect(Object.keys(count.getStorage()).length).toBe(0);
});
});

describe("CountLoader", (): void => {
const testCountFilePath = "./test.countstorage.json";
const loader = new CountLoader(testCountFilePath);

it("Should return an empty object", (): void => {
const storage = loader.loadCountFromCountFile();
expect(Object.keys(storage).length).toBe(0);
});

afterAll((): void => {
fs.unlink(testCountFilePath);
});
});
18 changes: 18 additions & 0 deletions src/tests/unit/search-engine.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SearchResultItem } from "../../ts/search-result-item";
import { SearchEngine } from "./../../ts/search-engine";
import { CountManager } from "./../../ts/count-manager";

function getFakeItems(items: string[]): SearchResultItem[] {
return items.map((i): SearchResultItem => {
Expand Down Expand Up @@ -43,5 +44,22 @@ describe("SearchEngine", (): void => {
expect(actual.length).toBeGreaterThan(0);
expect(actual[0].name).toBe("hans");
});

it("should return frequently used result", (): void => {
const count = new CountManager();
const fakeItems = getFakeItems(["file explorer", "firefox", "find device"]);
const searchEngine = new SearchEngine(fakeItems);
const userInput = "fi";

count.addCount("firefox");
count.addCount("firefox");
count.addCount("firefox");

const actual = searchEngine.search(userInput);

expect(actual[0].name).toBe("firefox");

count.removeCountKey("firefox");
});
});
});
33 changes: 33 additions & 0 deletions src/ts/count-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as fs from "fs";
import * as os from "os";
import * as path from "path";

export interface Count {
[key: string]: number;
}

let countFilePath = path.join(process.cwd(), "ueli.countstorage.json");

export class CountLoader {
public constructor(filePath?: string) {
if (filePath !== undefined) {
countFilePath = filePath;
}
}

public loadCountFromCountFile(): Count {
try {
const fileContent = fs.readFileSync(countFilePath, "utf-8");
const parsed = JSON.parse(fileContent) as Count;
return parsed;
} catch (err) {
fs.writeFileSync(countFilePath, "{}", "utf-8");
return {};
}
}

public writeCountFile(storage: Count): void {
const stringifiedStorage = JSON.stringify(storage);
fs.writeFileSync(countFilePath, stringifiedStorage, "utf-8");
}
}
46 changes: 46 additions & 0 deletions src/ts/count-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as path from "path";
import { CountLoader, Count } from "./count-loader";

let countLoader = new CountLoader();
let countStorage = countLoader.loadCountFromCountFile();

export class CountManager {
public constructor(loader?: CountLoader) {
if (loader !== undefined) {
countLoader = loader;
countStorage = loader.loadCountFromCountFile();
}
}

public getCount(key: string): number {
const score = countStorage[key];
if (isNaN(score)) {
return 0;
} else {
return score;
}
}

public addCount(key: string): void {
let score = this.getCount(key);
score++;
if (score <= 40) {
countStorage[key] = score;
countLoader.writeCountFile(countStorage);
}
}

public removeCountKey(key: string): void {
delete countStorage[key];
countLoader.writeCountFile(countStorage);
}

public getStorage(): Count {
return countStorage;
}

public clearStorage(): void {
countStorage = {} as Count;
countLoader.writeCountFile(countStorage);
}
}
5 changes: 5 additions & 0 deletions src/ts/execution-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { IpcChannels } from "./ipc-channels";
import { EmailAddressInputValidator } from "./input-validators/email-address-input-validator";
import { EmailAddressExecutionArgumentValidator } from "./execution-argument-validators/email-address-execution-argument-validator";
import { platform } from "os";
import { CountManager } from "./count-manager";

export class ExecutionService {
private validatorExecutorCombinations: ExecutionArgumentValidatorExecutorCombination[];
Expand All @@ -40,6 +41,10 @@ export class ExecutionService {
}

combi.executor.execute(executionArgument);
if (combi.executor.logExecute()) {
const count = new CountManager();
count.addCount(executionArgument);
}
}, 50); // set delay for execution to 50ms otherwise user input reset does not work properly

return;
Expand Down
4 changes: 4 additions & 0 deletions src/ts/executors/command-line-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export class CommandLineExecutor implements Executor {
return true;
}

public logExecute(): boolean {
return false;
}

private sendCommandLineOutputToRenderer(data: string): void {
ipcMain.emit(IpcChannels.commandLineExecution, data);
}
Expand Down
1 change: 1 addition & 0 deletions src/ts/executors/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export interface Executor {
execute(executionArgument: string): void;
hideAfterExecution(): boolean;
resetUserInputAfterExecution(): boolean;
logExecute(): boolean;
}
4 changes: 4 additions & 0 deletions src/ts/executors/file-path-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export class FilePathExecutor implements Executor {
this.handleExecution(command);
}

public logExecute(): boolean {
return true;
}

private handleExecution(command: string): void {
childProcess.exec(command, (err, stoud, sterr) => {
if (err) {
Expand Down
5 changes: 5 additions & 0 deletions src/ts/executors/mac-os-settings-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export class MacOsSettingsExecutor implements Executor {
public hideAfterExecution(): boolean {
return true;
}

public logExecute(): boolean {
return true;
}

public resetUserInputAfterExecution(): boolean {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/ts/executors/ueli-command-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export class UeliCommandExecutor implements Executor {
return false;
}

public logExecute(): boolean {
return false;
}

public resetUserInputAfterExecution(): boolean {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/ts/executors/web-search-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class WebSearchExecutor implements Executor {
return true;
}

public logExecute(): boolean {
return false;
}

public execute(executionArgument: string): void {
for (const webSearch of this.webSearches) {
if (executionArgument.startsWith(`${webSearch.prefix}${WebSearchHelpers.webSearchSeparator}`)) {
Expand Down
4 changes: 4 additions & 0 deletions src/ts/executors/web-url-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export class WebUrlExecutor implements Executor {
return true;
}

public logExecute(): boolean {
return false;
}

private handleCommandExecution(command: string): void {
childProcess.exec(command, (err, stout, sterr) => {
if (err) {
Expand Down
4 changes: 4 additions & 0 deletions src/ts/executors/windows-settings-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export class WindowsSettingsExecutor implements Executor {
return true;
}

public logExecute(): boolean {
return true;
}

private replacePrefix(executionArgument: string): string {
return executionArgument.replace(WindowsSettingsHelpers.windowsSettingsPrefix, "");
}
Expand Down
19 changes: 19 additions & 0 deletions src/ts/search-engine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Fuse from "fuse.js";
import { SearchPlugin } from "./search-plugins/search-plugin";
import { SearchResultItem } from "./search-result-item";
import { CountManager } from "./count-manager";

export class SearchEngine {
private unsortedSearchResults: SearchResultItem[];
Expand All @@ -23,6 +24,8 @@ export class SearchEngine {

const fuseResults = fuse.search(searchTerm) as any[];

this.sortByCount(fuseResults);

const result = fuseResults.map((fuseResult): SearchResultItem => {
return {
executionArgument: fuseResult.item.executionArgument,
Expand All @@ -34,4 +37,20 @@ export class SearchEngine {

return result;
}

private sortByCount(fuseResults: any[]) {
const count = new CountManager();

for (let i = 0; i < fuseResults.length; i++) {
for (let j = i; j < fuseResults.length; j++) {
const scoreA = fuseResults[i].score - count.getCount(fuseResults[i].item.executionArgument) / 100;
const scoreB = fuseResults[j].score - count.getCount(fuseResults[j].item.executionArgument) / 100;
if (scoreA > scoreB) {
const temp = fuseResults[i];
fuseResults[i] = fuseResults[j];
fuseResults[j] = temp;
}
}
}
}
}

0 comments on commit cf824d4

Please sign in to comment.