@prozilla-os/terminal 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README.md +64 -0
- package/package.json +38 -0
- package/src/components/Ansi.tsx +174 -0
- package/src/components/InputLine.tsx +55 -0
- package/src/components/OutputLine.tsx +17 -0
- package/src/components/Terminal.module.css +126 -0
- package/src/components/Terminal.tsx +369 -0
- package/src/constants/terminal.const.ts +43 -0
- package/src/core/_utils/terminal.utils.ts +10 -0
- package/src/core/command.ts +95 -0
- package/src/core/commands/cat.ts +33 -0
- package/src/core/commands/cd.ts +20 -0
- package/src/core/commands/clear.ts +15 -0
- package/src/core/commands/cmatrix.ts +101 -0
- package/src/core/commands/compgen.ts +14 -0
- package/src/core/commands/cowsay.ts +108 -0
- package/src/core/commands/dir.ts +15 -0
- package/src/core/commands/echo.ts +10 -0
- package/src/core/commands/exit.ts +11 -0
- package/src/core/commands/fortune.ts +63 -0
- package/src/core/commands/help.ts +28 -0
- package/src/core/commands/hostname.ts +10 -0
- package/src/core/commands/lolcat.ts +54 -0
- package/src/core/commands/ls.ts +31 -0
- package/src/core/commands/make.ts +9 -0
- package/src/core/commands/man.ts +86 -0
- package/src/core/commands/mkdir.ts +17 -0
- package/src/core/commands/neofetch.ts +69 -0
- package/src/core/commands/pwd.ts +15 -0
- package/src/core/commands/reboot.ts +11 -0
- package/src/core/commands/reload.ts +11 -0
- package/src/core/commands/rev.ts +10 -0
- package/src/core/commands/rm.ts +21 -0
- package/src/core/commands/rmdir.ts +19 -0
- package/src/core/commands/sl.ts +220 -0
- package/src/core/commands/touch.ts +27 -0
- package/src/core/commands/uptime.ts +10 -0
- package/src/core/commands/whatis.ts +22 -0
- package/src/core/commands/whoami.ts +10 -0
- package/src/core/commands.ts +52 -0
- package/src/core/stream.ts +38 -0
- package/src/index.ts +7 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { formatError } from "../_utils/terminal.utils";
|
|
2
|
+
import { Command, ExecuteParams } from "../command";
|
|
3
|
+
import { Stream } from "../stream";
|
|
4
|
+
|
|
5
|
+
const ANIMATION_SPEED = 1.25;
|
|
6
|
+
|
|
7
|
+
const LOCOMOTIVE_SMOKE = [
|
|
8
|
+
[
|
|
9
|
+
" (@@) ( ) (@) ( ) @@ () @ o @ o",
|
|
10
|
+
" ( )",
|
|
11
|
+
" (@@@@)",
|
|
12
|
+
" ( )",
|
|
13
|
+
"",
|
|
14
|
+
" (@@@)",
|
|
15
|
+
],
|
|
16
|
+
[
|
|
17
|
+
" ( ) (@@) ( ) (@) () @@ o @ o @",
|
|
18
|
+
" (@@@)",
|
|
19
|
+
" ( )",
|
|
20
|
+
" (@@@@)",
|
|
21
|
+
"",
|
|
22
|
+
" ( )",
|
|
23
|
+
]
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
const LOCOMOTIVE_TOP = [
|
|
27
|
+
" ==== ________ ___________",
|
|
28
|
+
" _D _| |_______/ \\__I_I_____===__|_________|",
|
|
29
|
+
" |(_)--- | H\\________/ | | =|___ ___| ",
|
|
30
|
+
" / | | H | | | | ||_| |_|| ",
|
|
31
|
+
" | | | H |__--------------------| [___] | ",
|
|
32
|
+
" | ________|___H__/__|_____/[][]~\\_______| | ",
|
|
33
|
+
" |/ | |-----------I_____I [][] [] D |=======|__",
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
const LOCOMOTIVE_BOTTOM = [
|
|
37
|
+
[
|
|
38
|
+
"__/ =| o |=-~~\\ /~~\\ /~~\\ /~~\\ ____Y___________|__",
|
|
39
|
+
" |/-=|___|= || || || |_____/~\\___/ ",
|
|
40
|
+
" \\_/ \\O=====O=====O=====O_/ \\_/ ",
|
|
41
|
+
],
|
|
42
|
+
[
|
|
43
|
+
"__/ =| o |=-~~\\ /~~\\ /~~\\ /~~\\ ____Y___________|__",
|
|
44
|
+
" |/-=|___|=O=====O=====O=====O |_____/~\\___/ ",
|
|
45
|
+
" \\_/ \\__/ \\__/ \\__/ \\__/ \\_/ ",
|
|
46
|
+
],
|
|
47
|
+
[
|
|
48
|
+
"__/ =| o |=-O=====O=====O=====O \\ ____Y___________|__",
|
|
49
|
+
" |/-=|___|= || || || |_____/~\\___/ ",
|
|
50
|
+
" \\_/ \\__/ \\__/ \\__/ \\__/ \\_/ ",
|
|
51
|
+
],
|
|
52
|
+
[
|
|
53
|
+
"__/ =| o |=-~O=====O=====O=====O\\ ____Y___________|__",
|
|
54
|
+
" |/-=|___|= || || || |_____/~\\___/ ",
|
|
55
|
+
" \\_/ \\__/ \\__/ \\__/ \\__/ \\_/ ",
|
|
56
|
+
],
|
|
57
|
+
[
|
|
58
|
+
"__/ =| o |=-~~\\ /~~\\ /~~\\ /~~\\ ____Y___________|__",
|
|
59
|
+
" |/-=|___|= O=====O=====O=====O|_____/~\\___/ ",
|
|
60
|
+
" \\_/ \\__/ \\__/ \\__/ \\__/ \\_/ ",
|
|
61
|
+
],
|
|
62
|
+
[
|
|
63
|
+
"__/ =| o |=-~~\\ /~~\\ /~~\\ /~~\\ ____Y___________|__",
|
|
64
|
+
" |/-=|___|= || || || |_____/~\\___/ ",
|
|
65
|
+
" \\_/ \\_O=====O=====O=====O/ \\_/ ",
|
|
66
|
+
]
|
|
67
|
+
].reverse();
|
|
68
|
+
|
|
69
|
+
const COAL_WAGON = [
|
|
70
|
+
" ",
|
|
71
|
+
" _________________ ",
|
|
72
|
+
" _| \\_____A ",
|
|
73
|
+
" =| | ",
|
|
74
|
+
" -| | ",
|
|
75
|
+
"__|________________________|_",
|
|
76
|
+
"|__________________________|_",
|
|
77
|
+
" |_D__D__D_| |_D__D__D_| ",
|
|
78
|
+
" \\_/ \\_/ \\_/ \\_/ ",
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
const EXTRA_WAGONS = [
|
|
82
|
+
[
|
|
83
|
+
" _______================_______ ",
|
|
84
|
+
" | | ___ ___ ___ ___ | | ",
|
|
85
|
+
" | | | | | | | | | | | | ",
|
|
86
|
+
" | | |_| |_| |_| |_| | | ",
|
|
87
|
+
" | | | | ",
|
|
88
|
+
"__|__|____________________|__|__",
|
|
89
|
+
"_|____________________________|_",
|
|
90
|
+
" |_D__D__D_| |_D__D__D_| ",
|
|
91
|
+
" \\_/ \\_/ \\_/ \\_/ ",
|
|
92
|
+
],
|
|
93
|
+
[
|
|
94
|
+
" ",
|
|
95
|
+
" ________========__HH____ ",
|
|
96
|
+
" / HH \\ ",
|
|
97
|
+
"/ HH \\ ",
|
|
98
|
+
"\\ HH / ",
|
|
99
|
+
"_\\__________________HH____/__",
|
|
100
|
+
"|__________________________|_",
|
|
101
|
+
" |_D__D__D_| |_D__D__D_| ",
|
|
102
|
+
" \\_/ \\_/ \\_/ \\_/ ",
|
|
103
|
+
],
|
|
104
|
+
[
|
|
105
|
+
" ",
|
|
106
|
+
" ",
|
|
107
|
+
" ",
|
|
108
|
+
" ",
|
|
109
|
+
" ",
|
|
110
|
+
"_____________________________",
|
|
111
|
+
"|__________________________|_",
|
|
112
|
+
" |_D__D__D_| |_D__D__D_| ",
|
|
113
|
+
" \\_/ \\_/ \\_/ \\_/ ",
|
|
114
|
+
],
|
|
115
|
+
[
|
|
116
|
+
" ",
|
|
117
|
+
" _|__|___________|__|___ ",
|
|
118
|
+
" (_|__|___________|__|___) ",
|
|
119
|
+
" _(|__|___________|__|__)_ ",
|
|
120
|
+
" (__|__|___________|__|____)",
|
|
121
|
+
"_(___|__|___________|__|__)__",
|
|
122
|
+
"|__________________________|_",
|
|
123
|
+
" |_D__D__D_| |_D__D__D_| ",
|
|
124
|
+
" \\_/ \\_/ \\_/ \\_/ ",
|
|
125
|
+
],
|
|
126
|
+
[
|
|
127
|
+
" ",
|
|
128
|
+
" ___________________________ ",
|
|
129
|
+
" \\ | | / ",
|
|
130
|
+
" \\ | | / ",
|
|
131
|
+
" |\\ | | /| ",
|
|
132
|
+
"__|_\\___|___________|___/_|__",
|
|
133
|
+
"|__________________________|_",
|
|
134
|
+
" |_D__D__D_| |_D__D__D_| ",
|
|
135
|
+
" \\_/ \\_/ \\_/ \\_/ ",
|
|
136
|
+
],
|
|
137
|
+
];
|
|
138
|
+
|
|
139
|
+
function generateLocomotive(frame: number, wagonCount = 1) {
|
|
140
|
+
const smokeHeight = LOCOMOTIVE_SMOKE[0].length;
|
|
141
|
+
const locomotiveHeight = LOCOMOTIVE_TOP.length + LOCOMOTIVE_BOTTOM[0].length;
|
|
142
|
+
const wagonHeight = COAL_WAGON.length;
|
|
143
|
+
|
|
144
|
+
const wagonStart = smokeHeight + locomotiveHeight - wagonHeight;
|
|
145
|
+
|
|
146
|
+
const smoke = LOCOMOTIVE_SMOKE[Math.round(frame / 4) % LOCOMOTIVE_SMOKE.length];
|
|
147
|
+
const top = LOCOMOTIVE_TOP;
|
|
148
|
+
const bottom = LOCOMOTIVE_BOTTOM[frame % LOCOMOTIVE_BOTTOM.length];
|
|
149
|
+
|
|
150
|
+
const distance = 50 - frame;
|
|
151
|
+
const locomotive = smoke.concat(top, bottom).map((line, index) => {
|
|
152
|
+
if (index >= wagonStart && wagonCount > 0) {
|
|
153
|
+
for (let i = 0; i < wagonCount; i++) {
|
|
154
|
+
if (i === 0) {
|
|
155
|
+
line += COAL_WAGON[index - wagonStart];
|
|
156
|
+
} else {
|
|
157
|
+
line += EXTRA_WAGONS[(i - 1) % EXTRA_WAGONS.length][index - wagonStart];
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (distance === 0) {
|
|
163
|
+
return line;
|
|
164
|
+
} else if (distance > 0) {
|
|
165
|
+
return " ".repeat(distance) + line;
|
|
166
|
+
} else {
|
|
167
|
+
line = line.slice(-distance);
|
|
168
|
+
return line;
|
|
169
|
+
}
|
|
170
|
+
}).join("\n");
|
|
171
|
+
|
|
172
|
+
return `\n${locomotive}\n`;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export const sl = new Command()
|
|
176
|
+
.setManual({
|
|
177
|
+
purpose: "Show animations aimed to correct users who accidentally enter sl instead of ls. SL stands for Steam Locomotive.",
|
|
178
|
+
usage: "sl\n"
|
|
179
|
+
+ "sl -w number",
|
|
180
|
+
options: {
|
|
181
|
+
"-w number": "Set the amount of wagons (defaults to 1)"
|
|
182
|
+
}
|
|
183
|
+
})
|
|
184
|
+
.addOption({
|
|
185
|
+
short: "w",
|
|
186
|
+
long: "wagons",
|
|
187
|
+
isInput: true
|
|
188
|
+
})
|
|
189
|
+
.setExecute(function(this: Command, args, params) {
|
|
190
|
+
const { inputs } = params as ExecuteParams;
|
|
191
|
+
let wagonCount = 1;
|
|
192
|
+
|
|
193
|
+
if (inputs?.w) {
|
|
194
|
+
wagonCount = parseInt(inputs.w);
|
|
195
|
+
|
|
196
|
+
if (!wagonCount || wagonCount < 0) {
|
|
197
|
+
return formatError(this.name, "Please specify a valid amount of wagons");
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const stream = new Stream();
|
|
202
|
+
|
|
203
|
+
let frame = 0;
|
|
204
|
+
const interval = setInterval(() => {
|
|
205
|
+
const text = generateLocomotive(frame, wagonCount);
|
|
206
|
+
stream.send(text);
|
|
207
|
+
frame++;
|
|
208
|
+
|
|
209
|
+
if (text.trim().length === 0)
|
|
210
|
+
stream.stop();
|
|
211
|
+
}, 100 / ANIMATION_SPEED);
|
|
212
|
+
|
|
213
|
+
stream.on(Stream.EVENT_NAMES.stop, () => {
|
|
214
|
+
clearInterval(interval);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
stream.start();
|
|
218
|
+
|
|
219
|
+
return stream;
|
|
220
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { VirtualFile } from "@prozilla-os/core";
|
|
2
|
+
import { formatError } from "../_utils/terminal.utils";
|
|
3
|
+
import { Command, ExecuteParams } from "../command";
|
|
4
|
+
|
|
5
|
+
export const touch = new Command()
|
|
6
|
+
.setRequireArgs(true)
|
|
7
|
+
.setManual({
|
|
8
|
+
purpose: "Change file timestamps",
|
|
9
|
+
usage: "touch [options] files",
|
|
10
|
+
description: "Update the access and modification times of each FILE to the current time.\n\n"
|
|
11
|
+
+ "A file argument that does not exist is created empty."
|
|
12
|
+
})
|
|
13
|
+
.setExecute(function(this: Command, args, params) {
|
|
14
|
+
const { currentDirectory } = params as ExecuteParams;
|
|
15
|
+
const fileId = (args as string[])[0];
|
|
16
|
+
|
|
17
|
+
if (fileId === "girls\\" && (args as string[])[1] === "boo**")
|
|
18
|
+
return formatError((this).name, "Cannot touch 'girls boo**': Permission denied");
|
|
19
|
+
|
|
20
|
+
const { name, extension } = VirtualFile.splitId(fileId);
|
|
21
|
+
|
|
22
|
+
if (currentDirectory.findFile(name, extension))
|
|
23
|
+
return { blank: true };
|
|
24
|
+
|
|
25
|
+
currentDirectory.createFile(name, extension as string | undefined);
|
|
26
|
+
return { blank: true };
|
|
27
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Command, ExecuteParams } from "../command";
|
|
2
|
+
|
|
3
|
+
export const uptime = new Command()
|
|
4
|
+
.setManual({
|
|
5
|
+
purpose: "Display the current uptime of the system"
|
|
6
|
+
})
|
|
7
|
+
.setExecute(function(args, params) {
|
|
8
|
+
const { systemManager } = params as ExecuteParams;
|
|
9
|
+
return `Uptime: ${systemManager.getUptime(2)}`;
|
|
10
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ANSI } from "@prozilla-os/core";
|
|
2
|
+
import { formatError } from "../_utils/terminal.utils";
|
|
3
|
+
import { Command } from "../command";
|
|
4
|
+
import { CommandsManager } from "../commands";
|
|
5
|
+
|
|
6
|
+
export const whatis = new Command()
|
|
7
|
+
.setRequireArgs(true)
|
|
8
|
+
.setManual({
|
|
9
|
+
purpose: "Show information about a command"
|
|
10
|
+
})
|
|
11
|
+
.setExecute(function(this: Command, args) {
|
|
12
|
+
const commandName = (args as string[])[0].toLowerCase();
|
|
13
|
+
const command = CommandsManager.find(commandName);
|
|
14
|
+
|
|
15
|
+
if (!command)
|
|
16
|
+
return formatError(this.name, `${commandName}: Command not found`);
|
|
17
|
+
|
|
18
|
+
if (!command.manual?.purpose)
|
|
19
|
+
return formatError(this.name, `${commandName}: No information found`);
|
|
20
|
+
|
|
21
|
+
return `${commandName} - ${ANSI.fg.green}${command.manual.purpose}`;
|
|
22
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Command } from "./command";
|
|
2
|
+
|
|
3
|
+
let commands: Command[] = [];
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Dynamically import commands
|
|
7
|
+
*/
|
|
8
|
+
const loadCommands = () => {
|
|
9
|
+
commands = [];
|
|
10
|
+
|
|
11
|
+
// https://vitejs.dev/guide/features.html#glob-import
|
|
12
|
+
const modules = import.meta.glob("./commands/*.ts");
|
|
13
|
+
for (const path in modules) {
|
|
14
|
+
void modules[path]().then((commandModule) => {
|
|
15
|
+
const commandName = Object.keys(commandModule as Record<string, Command>)[0];
|
|
16
|
+
|
|
17
|
+
const command = (commandModule as Record<string, Command>)[commandName];
|
|
18
|
+
command.setName(commandName.toLowerCase());
|
|
19
|
+
|
|
20
|
+
commands.push(command);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
loadCommands();
|
|
26
|
+
|
|
27
|
+
export class CommandsManager {
|
|
28
|
+
static COMMANDS = commands;
|
|
29
|
+
|
|
30
|
+
static find(name: string): Command | null {
|
|
31
|
+
let matchCommand: Command | null = null;
|
|
32
|
+
|
|
33
|
+
this.COMMANDS.forEach((command) => {
|
|
34
|
+
if (command.name === name) {
|
|
35
|
+
matchCommand = command;
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
return matchCommand;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static search(pattern: string): Command[] {
|
|
44
|
+
const matches = this.COMMANDS.filter((command) => command.name?.match(pattern));
|
|
45
|
+
return matches;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static reload() {
|
|
49
|
+
loadCommands();
|
|
50
|
+
CommandsManager.COMMANDS = commands;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { EventEmitter } from "@prozilla-os/core";
|
|
2
|
+
|
|
3
|
+
const StreamEvents = {
|
|
4
|
+
new: "new",
|
|
5
|
+
start: "start",
|
|
6
|
+
stop: "stop",
|
|
7
|
+
} as const;
|
|
8
|
+
|
|
9
|
+
export class Stream extends EventEmitter<typeof StreamEvents> {
|
|
10
|
+
static EVENT_NAMES = StreamEvents;
|
|
11
|
+
|
|
12
|
+
enabled: boolean = false;
|
|
13
|
+
|
|
14
|
+
start(callback?: Function): Stream {
|
|
15
|
+
if (this.enabled)
|
|
16
|
+
return this;
|
|
17
|
+
|
|
18
|
+
callback?.(this);
|
|
19
|
+
this.enabled = true;
|
|
20
|
+
this.emit("start");
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
stop(): Stream {
|
|
25
|
+
if (!this.enabled)
|
|
26
|
+
return this;
|
|
27
|
+
|
|
28
|
+
this.enabled = false;
|
|
29
|
+
this.emit("stop");
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
send(text: string): Stream {
|
|
34
|
+
if (this.enabled)
|
|
35
|
+
this.emit("new", text);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/index.ts
ADDED