-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelp.ts
40 lines (37 loc) · 1.97 KB
/
help.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
/// <reference path="../userspace/index.d.ts" />
/**
* Prints this help message
*/
if (typeof webcontainer !== 'function')
throw new Error('Missing webcontainer runtime');
webcontainer(async function help(process) {
const encoder = new TextEncoder();
const print = (line: string) => process.write(1, encoder.encode(line + '\n'));
const printErr = (line: string) => process.write(2, encoder.encode(line + '\n'));
try {
/** @type {Record<string, { usage: string[], description: string }>} */
const cmds = await new Response(await process.createReadStream('/bin/help.json'), {
headers: {
'Content-Type': 'application/json'
}
}).json();
for (const [name, { usage, description }] of Object.entries<{ usage: string[], description: string }>(cmds)) {
let desc = description || '(no help available)';
desc = desc.split('\n').map(x => '\t' + x).join('\n');
await print(`${[`\x1B[1m${name}\x1B[0m`, ...usage.map(a => `\x1B[4m${a}\x1B[0m`)].join(' ')}\n${desc}\n`);
}
} catch (err) {
await printErr(`\x1B[1;31mCommands list not available (${(err instanceof Error && err.message) ?? err})\x1B[0m`);
await print('');
}
await print('\x1B[4mShortcuts\x1B[0m');
await print(' \x1B[1mCtrl + D\x1B[0m : Close stdin (stop commands like \x1B[1mcat\x1B[0m and \x1B[1mtee\x1B[0m)');
await print(' \x1B[1mCtrl + L\x1B[0m : Clear the terminal');
await print(' \x1B[1mCtrl + C\x1B[0m : DOES NOTHING (should kill a command but it is not supported yet)');
await print(' \x1B[1m TAB\x1B[0m : DOES NOTHING (no auto-compelte yet)');
await print('');
await print("Commands can be piped together with a | character (eg. \x1B[4mecho Hello world! | tee README\x1B[0m). && and || doesn't work yet.");
await print("Environment variables are also supported. Play with \x1B[4menv\x1B[0m command for more.");
await print('');
await print('You can also drop a directory from your computer here to make it accessible via this terminal.');
});