-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex2.js
More file actions
73 lines (66 loc) · 1.62 KB
/
index2.js
File metadata and controls
73 lines (66 loc) · 1.62 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
import jq from "node-jq";
import terminal from "node:readline";
import readline from "node:readline/promises";
import {
stdin,
stdout,
} from "node:process";
import fs from "fs";
const promptText = "> ";
const rl = readline.createInterface({
input: stdin,
output: stdout,
prompt: promptText,
autoCommit: true,
completer: (word) => {
return [ [ `${word}, you complete me!` ], word ];
},
});
const json = fs.readFileSync(0).toString();
const {
// colums: xMax,
rows: yMax,
} = stdout;
(async () => {
terminal.cursorTo(stdout, 0, 0);
terminal.clearScreenDown(stdout);
const res = await jqa(".env", json);
prompt(".");
printResult(res, yMax);
rl.on("line", (line) => {
// terminal.cursorTo(stdout, i, ++i);
// stdout.write(`${++i}: ${line.trim()}`);
switch (line.trim()) {
case "q":
rl.close();
process.exitCode = 0;
break;
}
if (process.exitCode === undefined) {
prompt(line);
terminal.cursorTo(stdout, 1, 1);
// stdout.write("fooo");
terminal.cursorTo(stdout, promptText.length + rl.line.length, 0);
}
});
})();
function prompt(str) {
// terminal.moveCursor(stdout, 0, -1);
// terminal.clearScreenDown(stdout);
// terminal.cursorTo(stdout, 40, 58);
// terminal.cursorTo(stdout, 0, 0);
rl.prompt(true);
rl.write(str);
}
function printResult(res, max) {
let row = 0;
for (const line of res.split("\n").slice(0, max)) {
terminal.cursorTo(stdout, 0, ++row);
stdout.write(line);
}
}
function jqa(filter, data) {
return new Promise((resolve) => {
jq.run(filter, data, { input: "string" }).then(resolve);
});
}