Repository navigation
Expand file tree
/
Copy pathmode-runner.mjs
More file actions
70 lines (64 loc) · 1.88 KB
/
Copy pathmode-runner.mjs
File metadata and controls
70 lines (64 loc) · 1.88 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
import chalk from "chalk";
import { input } from "@inquirer/prompts";
import select from "@inquirer/select";
class ModeRunner {
constructor(lambda) {
this.lambda = lambda;
}
async invoke() {
await this.askForDisplayMode();
let command = "";
while (true) {
command = await input({
message: ">",
validate: function (value) {
if (value.length >= 1) {
return true;
} else {
return "Please enter at least one character.";
}
},
});
if (command === "exit") {
break;
process.exit(0);
}
const event = this.event(command);
const { body, statusCode } = await this.lambda.invoke(event);
if (this.displayMode === "ascii") {
this.boxedResult(body, statusCode);
} else {
console.log(body);
}
}
}
event(command) {
return { X_LAMBDA_CONSOLE: { run: command } };
}
async askForDisplayMode() {
this.displayMode = await select({
message: "Select Display Mode:",
choices: [
{ name: "ASCII", value: "ascii" },
{ name: "Plain", value: "plain" },
],
});
}
boxedResult(result, statusCode) {
const chalkBg = statusCode === 1 ? chalk.bgRedBright : chalk.bgBlueBright;
const resultCleaned = result.replace(/[\u0000-\u001F]+$/g, "");
const resultIndented = resultCleaned.replace(/^/gm, "| ");
const resultPadded = resultIndented.replace(/(?<=\| ).*$/gm, (match) => {
const numSpaces = Math.max(
0,
process.stdout.columns - (match.length || 0) - 3
);
return match + " ".repeat(numSpaces) + "|";
});
const middleLine = "─".repeat(Math.max(0, process.stdout.columns - 2));
console.log(chalkBg(`╭${middleLine}╮`));
console.log(chalkBg(resultPadded));
console.log(chalkBg(`╰${middleLine}╯`));
}
}
export default ModeRunner;