Skip to content

Commit 648a112

Browse files
committed
feat: add search features and listing in Flow Launcher
1 parent 6e13459 commit 648a112

5 files changed

Lines changed: 79 additions & 11 deletions

File tree

src/main.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import process from 'node:process';
22
import type { Arguments } from './types.js';
33
import { ScriptManager } from './scriptManager.js';
4+
import {
5+
FLSearchResultFormat,
6+
sendJsonRpcRequest
7+
} from './utils/flowLauncher.js';
48

59
const args: Arguments = JSON.parse(process.argv[2] ?? '{}');
610
const { method, parameters } = args;
711

12+
let scriptManager = new ScriptManager();
13+
scriptManager.init();
14+
815
if (method === 'query') {
16+
const result = FLSearchResultFormat(
17+
scriptManager.search(parameters[0] ?? '')
18+
);
19+
sendJsonRpcRequest({ result });
920
}
1021

11-
if (method === 'do_something_for_query') {
22+
if (method === 'run') {
1223
}
13-
14-
let scriptManager = new ScriptManager();
15-
scriptManager.init();
16-
17-
console.log(scriptManager.scripts);

src/scriptManager.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import type { Metas } from './types.js';
4-
import { defaultScriptPath, metaEndTerm, metaStartTerm } from './const.js';
5-
import { stringRange } from './utils/utils.js';
64
import { Script } from './models/Script.js';
5+
import { defaultScriptPath, metaEndTerm, metaStartTerm } from './const.js';
6+
import { matchesMetas, stringRange } from './utils/utils.js';
77

88
export class ScriptManager {
99
scripts: Script[];
@@ -44,4 +44,29 @@ export class ScriptManager {
4444
console.error(`Unable to load ${path}, Error: ${e}`);
4545
}
4646
}
47+
48+
search(query: string): Script[] {
49+
const QUERY_CHAR_LIMIT = 20;
50+
const ALL_SCRIPTS_CHAR = '*';
51+
const QUERIES_SEPARATOR = ' ';
52+
53+
const metaNameSort = (a: Script, b: Script): 1 | -1 =>
54+
(a.name || '') < (b.name || '') ? 1 : -1;
55+
56+
if (query.length > QUERY_CHAR_LIMIT) return [];
57+
58+
if (query === ALL_SCRIPTS_CHAR) return this.scripts.sort(metaNameSort);
59+
60+
const normalizeQuery = query.toLowerCase();
61+
const queries = new Set(normalizeQuery.split(QUERIES_SEPARATOR));
62+
63+
const results = this.scripts.filter(script =>
64+
matchesMetas(Array.from(queries), {
65+
name: script.name,
66+
tags: script.tags
67+
})
68+
);
69+
70+
return results.sort(metaNameSort);
71+
}
4772
}

src/utils/flowLauncher.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { Script } from '../models/Script.js';
2+
import type { Result } from '../types.js';
3+
4+
/**
5+
* Return reformatted list of scripts for Flow Launcher
6+
* @param scripts list of scripts to format.
7+
*/
8+
export function FLSearchResultFormat(scripts: Script[]): Result[] {
9+
return scripts.map(script => ({
10+
Title: script.name ?? 'No Name',
11+
Subtitle: script.desc ?? 'No description',
12+
JsonRPCAction: {
13+
method: 'run',
14+
parameters: []
15+
},
16+
IcoPath: 'icon\\app.png',
17+
score: 0
18+
}));
19+
}
20+
21+
/**
22+
* Send a Flow Launcher JsonRPC request
23+
* @param req Flow Launcher request
24+
*/
25+
export function sendJsonRpcRequest(req: any): void {
26+
console.log(JSON.stringify(req));
27+
}

src/utils/metas.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/utils/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Metas } from '../types.js';
2+
13
/**
24
* Return part of a string based on terms
35
* @param str The string range.
@@ -14,3 +16,14 @@ export function stringRange(str: string, start: string, end: string): string {
1416

1517
return str.substring(str.indexOf(start) + start.length, str.indexOf(end));
1618
}
19+
20+
/**
21+
* Return match bool based on queries and metas
22+
* @param queries The multiples search term.
23+
* @param metas The searched metas.
24+
*/
25+
export function matchesMetas(queries: string[], metas: Metas): boolean {
26+
return queries.every(query =>
27+
Object.values(metas).some(meta => meta.includes(query))
28+
);
29+
}

0 commit comments

Comments
 (0)