-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathkey-command-helper.ts
More file actions
154 lines (132 loc) · 4.02 KB
/
key-command-helper.ts
File metadata and controls
154 lines (132 loc) · 4.02 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { color } from "../color";
import { stripVTControlCharacters } from "node:util";
import {
IKeyCommandHelper,
IKeyCommandPlatform,
IValidKeyName,
SpecialKeys,
SupportedProcessType,
} from "../common/definitions/key-commands";
import { injector } from "../common/yok";
export default class KeyCommandHelper implements IKeyCommandHelper {
public keyCommandExecutionBlocked: boolean;
private platform: string = "all";
private processType: SupportedProcessType;
private overrides: { [key: string]: () => Promise<boolean> } = {};
public addOverride(key: IValidKeyName, execute: () => Promise<boolean>) {
this.overrides[key] = execute;
}
public removeOverride(key: IValidKeyName) {
this.overrides[key] = undefined;
}
private onKeyPressed = async (data: Buffer) => {
const key = data.toString();
// Allow Ctrl + C always.
if (this.keyCommandExecutionBlocked && key !== SpecialKeys.CtrlC) return;
try {
const exists = injector.getRegisteredKeyCommandsNames().includes(key);
if (exists) {
const keyCommand = injector.resolveKeyCommand(key as IValidKeyName);
if (
keyCommand.platform === "all" ||
keyCommand.platform === this.platform ||
this.platform === "all"
) {
if (
keyCommand.canExecute &&
!keyCommand.canExecute(this.processType)
) {
console.log("blocked execution");
return;
}
if (keyCommand.willBlockKeyCommandExecution)
this.keyCommandExecutionBlocked = true;
if (this.overrides[key]) {
if (!(await this.overrides[key]())) {
this.keyCommandExecutionBlocked = false;
process.stdin.resume();
return;
}
}
if (keyCommand.key !== SpecialKeys.CtrlC) {
const line = ` ${color.dim("→")} ${color.bold(keyCommand.key)} — ${
keyCommand.description
}`;
const lineLength = stripVTControlCharacters(line).length - 1;
console.log(color.dim(` ┌${"─".repeat(lineLength)}┐`));
console.log(line + color.dim(" │"));
console.log(color.dim(` └${"─".repeat(lineLength)}┘`));
console.log("");
}
const result = await keyCommand.execute(this.platform);
this.keyCommandExecutionBlocked = false;
if (process.stdin.setRawMode) {
process.stdin.resume();
}
return result;
}
}
process.stdout.write(key);
} catch (e) {
const $logger = injector.resolve("logger") as ILogger;
$logger.error(e.message);
}
};
public printCommands(platform: IKeyCommandPlatform) {
const commands = injector.getRegisteredKeyCommandsNames();
const groupings: { [key: string]: boolean } = {};
const commandHelp = commands.reduce((arr, key) => {
const command = injector.resolveKeyCommand(key as IValidKeyName);
if (
!command.description ||
(command.platform !== "all" &&
command.platform !== platform &&
platform !== "all") ||
(command.canExecute && !command.canExecute(this.processType))
) {
return arr;
} else {
if (!groupings[command.group]) {
groupings[command.group] = true;
arr.push(` \n${color.underline(color.bold(command.group))}\n`);
}
arr.push(` ${color.bold(command.key)} — ${command.description}`);
return arr;
}
}, []);
console.info(
[
"",
` The CLI is ${color.underline(
`interactive`,
)}, you can press the following keys any time (make sure the terminal has focus).`,
"",
...commandHelp,
"",
].join("\n"),
);
}
public attachKeyCommands(
platform: IKeyCommandPlatform,
processType: SupportedProcessType,
) {
this.processType = processType;
this.platform = platform;
const stdin = process.stdin;
if (!stdin.setRawMode) {
process.on("message", (key: string) => {
this.onKeyPressed(Buffer.from(key));
});
} else {
stdin.setRawMode(false);
stdin.setRawMode(true);
stdin.resume();
stdin.on("data", this.onKeyPressed);
}
}
public detachKeyCommands() {
process.stdin.off("data", this.onKeyPressed);
process.stdin.setRawMode(false);
}
}
injector.register("keyCommandHelper", KeyCommandHelper);