-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
131 lines (110 loc) · 2.99 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
export interface AlfredIcon {
type: "fileicon" | "filetype";
path: string; // ~/Desktop | path.to.png
}
export interface AlfredItemMod {
valid?: boolean;
arg?: string;
subtitle: string;
icon?: AlfredIcon
}
export interface AlfredItemMods {
alt?: AlfredItemMod;
cmd?: AlfredItemMod;
}
export interface AlfredItemText {
copy?: string;
largetype?: string;
}
export interface AlfredItem {
uid?: string;
type: "default" | "file" | "file:skipcheck";
title: string;
subtitle?: string;
arg?: string;
autocomplete?: string;
match?: string;
valid?: boolean;
mods?: AlfredItemMods;
icon?: AlfredIcon;
text?: AlfredItemText;
quicklookurl?: string;
}
export interface AlfredItemSet {
items: AlfredItem[]
variables?: { [key: string]: string };
}
export interface ItemNeeds {
action: string[];
matcher: (query: string[]) => boolean;
}
export class ItemHandler {
constructor(private params: ItemNeeds & AlfredItem) { }
public generateItem(): AlfredItem {
const {
uid,
type,
title,
subtitle,
arg,
autocomplete,
match,
valid,
mods,
icon,
text,
quicklookurl } = this.params;
return { uid, type, title, subtitle, arg, autocomplete, match, valid, mods, icon, text, quicklookurl };
}
public matcher(query: string[]) {
return this.params.matcher(query);
}
public get action() {
return this.params.action;
}
}
export class Parser {
constructor(private items: ItemHandler[], private input: string[]) { }
public static process(items: ItemHandler[], args: string[]): number {
const subcommand = args[0] || "";
const query = args.slice(1);
switch (subcommand.toLowerCase()) {
case "query":
console.log(JSON.stringify(new Parser(items, query).query()));
return 0;
case "execute":
new Parser(items, query).execute();
return 0;
default:
console.log(`No matching subcommand: ${subcommand}`);
return -1;
}
}
public query(): AlfredItemSet {
const query = this.cleanInput();
let results: AlfredItem[] = [];
for (const item of this.items) {
if (item.matcher(query)) {
results = results.concat(item.generateItem());
}
}
return { items: results }
}
public execute(): void {
const query = this.cleanInput();
for (const item of this.items) {
if (item.matcher(query)) {
Deno.run({ cmd: item.action });
break;
}
}
}
private cleanInput(): string[] {
if (this.input.length === 1) {
if (this.input[0] === '(null)') {
return [''];
}
}
return this.input;
}
}