-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmp.ts
More file actions
78 lines (62 loc) · 2.24 KB
/
Copy pathtmp.ts
File metadata and controls
78 lines (62 loc) · 2.24 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
import * as ansi from "@std/cli/unstable-ansi";
import {promptSecret} from "@std/cli/prompt-secret";
import {promptSelect} from "@std/cli/unstable-prompt-select";
import * as colors from "@std/fmt/colors";
function write(data : string) {
Deno.stdout.write(new TextEncoder().encode(data));
}
const address = prompt('Address (<management-server-host>:<management-server-port>):');
if (address === '') {
console.log(colors.red('Please provide an address!'));
Deno.exit(1);
}
const token = promptSecret('Token (<management-server-secret>):');
if (token === '') {
console.log(colors.red('Please provide a token!'));
Deno.exit(1);
}
const printPretty = promptSelect('Print pretty?', ['Yes', 'No']);
console.clear();
write(ansi.setScrollableRegion(1, Deno.consoleSize().rows - 2));
const ws = new WebSocket(`ws://${address}`, {
headers: {
'Authorization': `Bearer ${token}`
}
});
ws.addEventListener('error', () => {
});
ws.addEventListener('message', ({data}) => {
write(ansi.setCursorPosition(Deno.consoleSize().rows - 2, 0));
const message = printPretty === 'Yes' ? Deno.inspect(JSON.parse(data), {
colors: true,
breakLength: Deno.consoleSize().columns,
depth: Infinity,
compact: false,
iterableLimit: Infinity,
strAbbreviateSize: Infinity,
}) : JSON.stringify(JSON.parse(data))
for (const line of message.split('\n')) {
write(line + '\n');
}
write(ansi.setCursorPosition(Deno.consoleSize().rows, 3));
});
ws.addEventListener('close', () => {
});
ws.addEventListener('open', () => {
write(ansi.setCursorPosition(Deno.consoleSize().rows - 1, 0));
write(colors.gray('\u2500'.repeat(Deno.consoleSize().columns) + '\n> '));
});
for await (const messageRaw of Deno.stdin.readable) {
const message = new TextDecoder().decode(messageRaw);
write(ansi.setCursorPosition(Deno.consoleSize().rows - 2, 0));
write(colors.gray('\n> ') + message);
write(ansi.setCursorPosition(Deno.consoleSize().rows - 1, 0));
write(colors.gray('\u2500'.repeat(Deno.consoleSize().columns) + '\n> '));
write(ansi.eraseCharacters(100));
write(ansi.setCursorPosition(Deno.consoleSize().rows - 2, 0));
if (message.trim() !== '') {
ws.send(message);
} else {
write(ansi.setCursorPosition(Deno.consoleSize().rows, 3));
}
}