@prozilla-os/terminal 1.0.1 → 1.0.3
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/dist/assets/main.css +1 -0
- package/dist/cat-Dp7MxLJO.js +17 -0
- package/dist/cat-Dp7MxLJO.js.map +1 -0
- package/dist/cd-zk1UnOo7.js +14 -0
- package/dist/cd-zk1UnOo7.js.map +1 -0
- package/dist/clear-DtYBxmDr.js +14 -0
- package/dist/clear-DtYBxmDr.js.map +1 -0
- package/dist/cmatrix-O49Zkqb8.js +59 -0
- package/dist/cmatrix-O49Zkqb8.js.map +1 -0
- package/dist/command-nBL1aO7T.js +43 -0
- package/dist/command-nBL1aO7T.js.map +1 -0
- package/dist/compgen-CA_GMRpy.js +14 -0
- package/dist/compgen-CA_GMRpy.js.map +1 -0
- package/dist/cowsay-BAYsU72K.js +50 -0
- package/dist/cowsay-BAYsU72K.js.map +1 -0
- package/dist/dir-BlrznBqV.js +11 -0
- package/dist/dir-BlrznBqV.js.map +1 -0
- package/dist/echo-PyZ6XYF6.js +11 -0
- package/dist/echo-PyZ6XYF6.js.map +1 -0
- package/dist/exit-CizX3N_u.js +11 -0
- package/dist/exit-CizX3N_u.js.map +1 -0
- package/dist/fortune-DNnPar5a.js +59 -0
- package/dist/fortune-DNnPar5a.js.map +1 -0
- package/dist/help-CYIno6WY.js +18 -0
- package/dist/help-CYIno6WY.js.map +1 -0
- package/dist/hostname-BBOQWx6L.js +11 -0
- package/dist/hostname-BBOQWx6L.js.map +1 -0
- package/dist/lolcat-LdzLGQur.js +36 -0
- package/dist/lolcat-LdzLGQur.js.map +1 -0
- package/dist/ls-Csd6ypmW.js +19 -0
- package/dist/ls-Csd6ypmW.js.map +1 -0
- package/dist/main-tAp_mbfZ.js +904 -0
- package/dist/main-tAp_mbfZ.js.map +1 -0
- package/dist/main.d.ts +11 -0
- package/dist/main.js +6 -0
- package/dist/main.js.map +1 -0
- package/dist/make-DOc--PtL.js +10 -0
- package/dist/make-DOc--PtL.js.map +1 -0
- package/dist/man-CZoHwxyo.js +48 -0
- package/dist/man-CZoHwxyo.js.map +1 -0
- package/dist/mkdir-BTSAC1PK.js +11 -0
- package/dist/mkdir-BTSAC1PK.js.map +1 -0
- package/dist/neofetch-KcdwHJSJ.js +39 -0
- package/dist/neofetch-KcdwHJSJ.js.map +1 -0
- package/dist/pwd-CSapfDtw.js +11 -0
- package/dist/pwd-CSapfDtw.js.map +1 -0
- package/dist/reboot-Bwxhoa8q.js +11 -0
- package/dist/reboot-Bwxhoa8q.js.map +1 -0
- package/dist/reload-zi8L0xNG.js +11 -0
- package/dist/reload-zi8L0xNG.js.map +1 -0
- package/dist/rev-C8qQIyj_.js +11 -0
- package/dist/rev-C8qQIyj_.js.map +1 -0
- package/dist/rm-KQ9gM6fT.js +13 -0
- package/dist/rm-KQ9gM6fT.js.map +1 -0
- package/dist/rmdir-UqUqHZTI.js +12 -0
- package/dist/rmdir-UqUqHZTI.js.map +1 -0
- package/dist/sl-BOyfa7Iu.js +167 -0
- package/dist/sl-BOyfa7Iu.js.map +1 -0
- package/dist/touch-D_kKTiUQ.js +20 -0
- package/dist/touch-D_kKTiUQ.js.map +1 -0
- package/dist/uptime-BYmCcOUM.js +11 -0
- package/dist/uptime-BYmCcOUM.js.map +1 -0
- package/dist/whatis-CYD52uq2.js +14 -0
- package/dist/whatis-CYD52uq2.js.map +1 -0
- package/dist/whoami-CQNGmqxL.js +11 -0
- package/dist/whoami-CQNGmqxL.js.map +1 -0
- package/package.json +16 -6
- package/src/components/Ansi.tsx +0 -174
- package/src/components/InputLine.tsx +0 -55
- package/src/components/OutputLine.tsx +0 -17
- package/src/components/Terminal.module.css +0 -126
- package/src/components/Terminal.tsx +0 -369
- package/src/constants/terminal.const.ts +0 -43
- package/src/core/_utils/terminal.utils.ts +0 -10
- package/src/core/command.ts +0 -95
- package/src/core/commands/cat.ts +0 -33
- package/src/core/commands/cd.ts +0 -20
- package/src/core/commands/clear.ts +0 -15
- package/src/core/commands/cmatrix.ts +0 -101
- package/src/core/commands/compgen.ts +0 -14
- package/src/core/commands/cowsay.ts +0 -108
- package/src/core/commands/dir.ts +0 -15
- package/src/core/commands/echo.ts +0 -10
- package/src/core/commands/exit.ts +0 -11
- package/src/core/commands/fortune.ts +0 -63
- package/src/core/commands/help.ts +0 -28
- package/src/core/commands/hostname.ts +0 -10
- package/src/core/commands/lolcat.ts +0 -54
- package/src/core/commands/ls.ts +0 -31
- package/src/core/commands/make.ts +0 -9
- package/src/core/commands/man.ts +0 -86
- package/src/core/commands/mkdir.ts +0 -17
- package/src/core/commands/neofetch.ts +0 -69
- package/src/core/commands/pwd.ts +0 -15
- package/src/core/commands/reboot.ts +0 -11
- package/src/core/commands/reload.ts +0 -11
- package/src/core/commands/rev.ts +0 -10
- package/src/core/commands/rm.ts +0 -21
- package/src/core/commands/rmdir.ts +0 -19
- package/src/core/commands/sl.ts +0 -220
- package/src/core/commands/touch.ts +0 -27
- package/src/core/commands/uptime.ts +0 -10
- package/src/core/commands/whatis.ts +0 -22
- package/src/core/commands/whoami.ts +0 -10
- package/src/core/commands.ts +0 -52
- package/src/core/stream.ts +0 -38
- package/src/index.ts +0 -7
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
.Terminal {
|
|
2
|
-
--char-width: 0.585rem;
|
|
3
|
-
|
|
4
|
-
display: flex;
|
|
5
|
-
flex-direction: column;
|
|
6
|
-
align-items: flex-start;
|
|
7
|
-
padding: 0.5rem;
|
|
8
|
-
overflow: auto;
|
|
9
|
-
height: 100%;
|
|
10
|
-
cursor: text;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
.Terminal * {
|
|
14
|
-
font-family: var(--mono-font-family);
|
|
15
|
-
letter-spacing: -0.03em;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
.Terminal p, .Terminal pre {
|
|
19
|
-
margin: 0;
|
|
20
|
-
height: 1.25rem;
|
|
21
|
-
line-height: inherit;
|
|
22
|
-
font-size: inherit;
|
|
23
|
-
letter-spacing: inherit;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
.Prefix {
|
|
27
|
-
display: flex;
|
|
28
|
-
width: max-content;
|
|
29
|
-
white-space: nowrap;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
.Input, .Output {
|
|
33
|
-
display: flex;
|
|
34
|
-
flex-direction: row;
|
|
35
|
-
align-items: center;
|
|
36
|
-
width: 100%;
|
|
37
|
-
line-height: 1.25rem;
|
|
38
|
-
font-size: 1rem;
|
|
39
|
-
text-align: start;
|
|
40
|
-
white-space: pre;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
.Input {
|
|
44
|
-
height: 1.25rem;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
.Input-container {
|
|
48
|
-
--cursor-width: var(--char-width);
|
|
49
|
-
--cursor-offset: 0;
|
|
50
|
-
|
|
51
|
-
position: relative;
|
|
52
|
-
height: 100%;
|
|
53
|
-
width: fit-content;
|
|
54
|
-
/* margin-left: var(--char-width); */
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
.Input-container::after {
|
|
58
|
-
content: "";
|
|
59
|
-
position: absolute;
|
|
60
|
-
top: 0;
|
|
61
|
-
left: calc(var(--cursor-offset) * var(--char-width) * 0.9746124950079872);
|
|
62
|
-
width: var(--cursor-width);
|
|
63
|
-
height: 100%;
|
|
64
|
-
background-color: var(--foreground-color-0);
|
|
65
|
-
animation: blink 1000ms step-end infinite;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
.Input-container input {
|
|
69
|
-
opacity: 0;
|
|
70
|
-
position: absolute;
|
|
71
|
-
left: 0;
|
|
72
|
-
width: 100%;
|
|
73
|
-
padding: 0;
|
|
74
|
-
background: none;
|
|
75
|
-
border: none;
|
|
76
|
-
outline: none;
|
|
77
|
-
font-size: inherit;
|
|
78
|
-
caret-color: transparent;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
@keyframes blink {
|
|
82
|
-
from, to {
|
|
83
|
-
background-color: transparent;
|
|
84
|
-
}
|
|
85
|
-
50% {
|
|
86
|
-
background-color: var(--foreground-color-0);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
.Input label {
|
|
91
|
-
position: absolute;
|
|
92
|
-
width: 100%;
|
|
93
|
-
height: 100%;
|
|
94
|
-
top: 0;
|
|
95
|
-
left: 0;
|
|
96
|
-
cursor: text;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
.ansi-black-fg { color: var(--black-2); }
|
|
100
|
-
.ansi-red-fg { color: var(--red-0); }
|
|
101
|
-
.ansi-green-fg { color: var(--green-0); }
|
|
102
|
-
.ansi-yellow-fg { color: var(--yellow-0); }
|
|
103
|
-
.ansi-blue-fg { color: var(--blue-0); }
|
|
104
|
-
.ansi-magenta-fg { color: var(--purple-0); }
|
|
105
|
-
.ansi-cyan-fg { color: var(--cyan-0); }
|
|
106
|
-
.ansi-white-fg { color: var(--foreground-color-0); }
|
|
107
|
-
|
|
108
|
-
.ansi-bright-black-fg { color: var(--black-2); }
|
|
109
|
-
.ansi-bright-red-fg { color: var(--red-0); }
|
|
110
|
-
.ansi-bright-green-fg { color: var(--green-0); }
|
|
111
|
-
.ansi-bright-yellow-fg { color: var(--yellow-0); }
|
|
112
|
-
.ansi-bright-blue-fg { color: var(--blue-0); }
|
|
113
|
-
.ansi-bright-magenta-fg { color: var(--purple-0); }
|
|
114
|
-
.ansi-bright-cyan-fg { color: var(--cyan-0); }
|
|
115
|
-
.ansi-bright-white-fg { color: var(--foreground-color-0); }
|
|
116
|
-
|
|
117
|
-
.ansi-black-bg { background-color: var(--black-2); }
|
|
118
|
-
.ansi-red-bg { background-color: var(--red-0); }
|
|
119
|
-
.ansi-green-bg { background-color: var(--green-0); }
|
|
120
|
-
.ansi-yellow-bg { background-color: var(--yellow-0); }
|
|
121
|
-
.ansi-blue-bg { background-color: var(--blue-0); }
|
|
122
|
-
.ansi-magenta-bg { background-color: var(--purple-0); }
|
|
123
|
-
.ansi-cyan-bg { background-color: var(--cyan-0); }
|
|
124
|
-
.ansi-white-bg { background-color: var(--foreground-color-0); }
|
|
125
|
-
|
|
126
|
-
.ansi-dim { opacity: 0.65; }
|
|
@@ -1,369 +0,0 @@
|
|
|
1
|
-
import { MouseEventHandler, MutableRefObject, useEffect, useRef, useState } from "react";
|
|
2
|
-
import styles from "./Terminal.module.css";
|
|
3
|
-
import { OutputLine } from "./OutputLine";
|
|
4
|
-
import { InputLine } from "./InputLine";
|
|
5
|
-
import { ANSI, App, clamp, removeFromArray, SettingsManager, useSettingsManager, useSystemManager, useVirtualRoot, VirtualFolder, WindowProps } from "@prozilla-os/core";
|
|
6
|
-
import { HOSTNAME, USERNAME, WELCOME_MESSAGE } from "../constants/terminal.const";
|
|
7
|
-
import { Stream } from "../core/stream";
|
|
8
|
-
import { CommandResponse } from "../core/command";
|
|
9
|
-
import { formatError } from "../core/_utils/terminal.utils";
|
|
10
|
-
import { CommandsManager } from "../core/commands";
|
|
11
|
-
|
|
12
|
-
interface TerminalProps extends WindowProps {
|
|
13
|
-
path: string;
|
|
14
|
-
input: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
interface HistoryEntry {
|
|
18
|
-
text: string;
|
|
19
|
-
isInput: boolean;
|
|
20
|
-
value?: string;
|
|
21
|
-
clear?: boolean;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function Terminal({ app, path: startPath, input, setTitle, close: exit, active, focus }: TerminalProps) {
|
|
25
|
-
const systemManager = useSystemManager();
|
|
26
|
-
const [inputKey, setInputKey] = useState(0);
|
|
27
|
-
const [inputValue, setInputValue] = useState(input ?? "");
|
|
28
|
-
const [history, setHistory] = useState<HistoryEntry[]>([{
|
|
29
|
-
text: app ? WELCOME_MESSAGE.replace("$APP_NAME", app.name) : WELCOME_MESSAGE,
|
|
30
|
-
isInput: false,
|
|
31
|
-
}]);
|
|
32
|
-
const virtualRoot = useVirtualRoot();
|
|
33
|
-
const [currentDirectory, setCurrentDirectory] = useState(virtualRoot?.navigate(startPath ?? "~"));
|
|
34
|
-
const inputRef = useRef(null);
|
|
35
|
-
const [historyIndex, setHistoryIndex] = useState(0);
|
|
36
|
-
const [stream, setStream] = useState<Stream | null>(null);
|
|
37
|
-
const [streamOutput, setStreamOutput] = useState<string | null>(null);
|
|
38
|
-
const ref = useRef(null);
|
|
39
|
-
const [streamFocused, setStreamFocused] = useState(false);
|
|
40
|
-
const settingsManager = useSettingsManager();
|
|
41
|
-
|
|
42
|
-
useEffect(() => {
|
|
43
|
-
if (currentDirectory != null)
|
|
44
|
-
setTitle?.(`${USERNAME}@${HOSTNAME}: ${currentDirectory.root ? "/" : currentDirectory.path}`);
|
|
45
|
-
}, [currentDirectory?.path, currentDirectory?.root, setTitle]);
|
|
46
|
-
|
|
47
|
-
useEffect(() => {
|
|
48
|
-
if (!inputRef.current || !active)
|
|
49
|
-
return;
|
|
50
|
-
|
|
51
|
-
(inputRef.current as unknown as HTMLInputElement).focus();
|
|
52
|
-
}, [inputRef, active]);
|
|
53
|
-
|
|
54
|
-
const scrollDown = () => {
|
|
55
|
-
(ref.current as unknown as HTMLDivElement).scrollTop = (ref.current as unknown as HTMLDivElement).scrollHeight;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
useEffect(() => {
|
|
59
|
-
if (streamFocused || ref.current == null || streamOutput == null)
|
|
60
|
-
return;
|
|
61
|
-
|
|
62
|
-
scrollDown();
|
|
63
|
-
setStreamFocused(true);
|
|
64
|
-
}, [streamFocused, streamOutput, ref]);
|
|
65
|
-
|
|
66
|
-
useEffect(() => {
|
|
67
|
-
if (ref.current == null || stream != null)
|
|
68
|
-
return;
|
|
69
|
-
|
|
70
|
-
scrollDown();
|
|
71
|
-
}, [inputValue]);
|
|
72
|
-
|
|
73
|
-
const prefix = `${ANSI.fg.cyan + USERNAME}@${HOSTNAME + ANSI.reset}:`
|
|
74
|
-
+ `${ANSI.fg.blue + ((currentDirectory?.root || currentDirectory == null) ? "/" : currentDirectory?.path) + ANSI.reset}$ `;
|
|
75
|
-
|
|
76
|
-
const updatedHistory = history;
|
|
77
|
-
const pushHistory = (entry: HistoryEntry) => {
|
|
78
|
-
updatedHistory.push(entry);
|
|
79
|
-
setHistory(updatedHistory);
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
const promptOutput = (text: string) => {
|
|
83
|
-
pushHistory({
|
|
84
|
-
text,
|
|
85
|
-
isInput: false
|
|
86
|
-
});
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
const connectStream = (stream: Stream, pipes: string[]) => {
|
|
90
|
-
setStream(stream);
|
|
91
|
-
setStreamFocused(false);
|
|
92
|
-
|
|
93
|
-
const onKeyDown = (event: KeyboardEvent) => {
|
|
94
|
-
if (active && (event.ctrlKey || event.metaKey) && event.key === "c") {
|
|
95
|
-
stream.stop();
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
let lastOutput: CommandResponse | null = null;
|
|
100
|
-
|
|
101
|
-
stream.on(Stream.EVENT_NAMES.new, (text) => {
|
|
102
|
-
void (async () => {
|
|
103
|
-
let output: CommandResponse = text as CommandResponse;
|
|
104
|
-
|
|
105
|
-
for (const pipe of pipes) {
|
|
106
|
-
if (output instanceof Stream)
|
|
107
|
-
continue;
|
|
108
|
-
|
|
109
|
-
// Output from the previous command gets added as an argument for the next command
|
|
110
|
-
output = await handleInput(output ? `${pipe} ${output as string}` : pipe);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if ((output as unknown) instanceof Stream) {
|
|
114
|
-
stream.stop();
|
|
115
|
-
promptOutput(ANSI.fg.red + "Stream failed");
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
lastOutput = output;
|
|
120
|
-
setStreamOutput(output as string);
|
|
121
|
-
})();
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
stream.on(Stream.EVENT_NAMES.stop, () => {
|
|
125
|
-
document.removeEventListener("keydown", onKeyDown);
|
|
126
|
-
|
|
127
|
-
promptOutput(lastOutput as string);
|
|
128
|
-
|
|
129
|
-
setStream(null);
|
|
130
|
-
setStreamOutput(null);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
document.addEventListener("keydown", onKeyDown);
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
const handleInput = async (value: string): Promise<CommandResponse> => {
|
|
137
|
-
const rawInputValueStart = value.indexOf(" ") + 1;
|
|
138
|
-
const rawInputValue = rawInputValueStart <= 0 ? "" : value.substr(rawInputValueStart);
|
|
139
|
-
const timestamp = Date.now();
|
|
140
|
-
|
|
141
|
-
value = value.trim();
|
|
142
|
-
if (value === "") return;
|
|
143
|
-
|
|
144
|
-
// Parse arguments
|
|
145
|
-
const args = value.match(/(?:[^\s"]+|"[^"]*")+/g);
|
|
146
|
-
if (args == null) return;
|
|
147
|
-
if (args[0].toLowerCase() === "sudo" && args.length >= 2) args.shift();
|
|
148
|
-
|
|
149
|
-
// Get command
|
|
150
|
-
const commandName = args.shift()?.toLowerCase();
|
|
151
|
-
if (commandName == null) return;
|
|
152
|
-
const command = CommandsManager.find(commandName);
|
|
153
|
-
|
|
154
|
-
if (!command) return formatError(commandName, "Command not found");
|
|
155
|
-
|
|
156
|
-
// Get options
|
|
157
|
-
const options: string[] = [];
|
|
158
|
-
const inputs: Record<string, string> = {};
|
|
159
|
-
args.filter((arg: string) => arg.startsWith("-")).forEach((option: string) => {
|
|
160
|
-
const addOption = (key: string) => {
|
|
161
|
-
if (options.includes(key))
|
|
162
|
-
return;
|
|
163
|
-
|
|
164
|
-
options.push(key);
|
|
165
|
-
const commandOption = command.getOption(options[options.length - 1]);
|
|
166
|
-
|
|
167
|
-
if (commandOption?.isInput) {
|
|
168
|
-
const optionInput = args[args.indexOf(option) + 1];
|
|
169
|
-
inputs[commandOption.short] = optionInput;
|
|
170
|
-
removeFromArray(optionInput, args);
|
|
171
|
-
}
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
if (option.startsWith("--")) {
|
|
175
|
-
const longOption = option.substring(2).toLowerCase();
|
|
176
|
-
addOption(longOption);
|
|
177
|
-
} else {
|
|
178
|
-
const shortOptions = option.substring(1).split("");
|
|
179
|
-
shortOptions.forEach((shortOption: string) => {
|
|
180
|
-
addOption(shortOption);
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
removeFromArray(option, args);
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
// Check usage
|
|
188
|
-
if (command.requireArgs && args.length === 0)
|
|
189
|
-
return formatError(commandName, `Incorrect usage: ${commandName} requires at least 1 argument`);
|
|
190
|
-
|
|
191
|
-
if (command.requireOptions && options.length === 0)
|
|
192
|
-
return formatError(commandName, `Incorrect usage: ${commandName} requires at least 1 option`);
|
|
193
|
-
|
|
194
|
-
// Execute command
|
|
195
|
-
let response: CommandResponse | null = null;
|
|
196
|
-
|
|
197
|
-
try {
|
|
198
|
-
response = await command.execute(args, {
|
|
199
|
-
promptOutput,
|
|
200
|
-
pushHistory,
|
|
201
|
-
virtualRoot,
|
|
202
|
-
currentDirectory: currentDirectory as VirtualFolder,
|
|
203
|
-
setCurrentDirectory,
|
|
204
|
-
username: USERNAME,
|
|
205
|
-
hostname: HOSTNAME,
|
|
206
|
-
rawInputValue,
|
|
207
|
-
options,
|
|
208
|
-
exit,
|
|
209
|
-
inputs,
|
|
210
|
-
timestamp,
|
|
211
|
-
settingsManager: settingsManager as SettingsManager,
|
|
212
|
-
systemManager,
|
|
213
|
-
app: app as App,
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
if (response == null)
|
|
217
|
-
return formatError(commandName, "Command failed");
|
|
218
|
-
|
|
219
|
-
if (!(response as { blank: boolean }).blank)
|
|
220
|
-
return response;
|
|
221
|
-
} catch (error) {
|
|
222
|
-
console.error(error);
|
|
223
|
-
return formatError(commandName, "Command failed");
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
const resetInput = () => {
|
|
228
|
-
setInputValue("");
|
|
229
|
-
setHistoryIndex(0);
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
const submitInput = async (value: string) => {
|
|
233
|
-
pushHistory({
|
|
234
|
-
text: prefix + value,
|
|
235
|
-
isInput: true,
|
|
236
|
-
value
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
// Piping is used to chain commands
|
|
240
|
-
let pipes = value.split(" | ");
|
|
241
|
-
const completedPipes: string[] = [];
|
|
242
|
-
|
|
243
|
-
let output: CommandResponse | null = null;
|
|
244
|
-
for (const pipe of pipes) {
|
|
245
|
-
if (output instanceof Stream)
|
|
246
|
-
continue;
|
|
247
|
-
|
|
248
|
-
// Output from the previous command gets added as an argument for the next command
|
|
249
|
-
output = await handleInput(output ? `${pipe} ${output as string}` : pipe);
|
|
250
|
-
completedPipes.push(pipe);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
resetInput();
|
|
254
|
-
|
|
255
|
-
pipes = pipes.filter((pipe) => !completedPipes.includes(pipe));
|
|
256
|
-
|
|
257
|
-
if (output) {
|
|
258
|
-
if (output instanceof Stream) {
|
|
259
|
-
connectStream(output, pipes);
|
|
260
|
-
} else {
|
|
261
|
-
promptOutput(`${output as string}\n`);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
const updateHistoryIndex = (delta: number) => {
|
|
267
|
-
const inputHistory = history.filter(({ isInput }) => isInput);
|
|
268
|
-
const index = clamp(historyIndex + delta, 0, inputHistory.length);
|
|
269
|
-
|
|
270
|
-
if (index === historyIndex) {
|
|
271
|
-
if (delta < 0) {
|
|
272
|
-
setInputValue("");
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
return;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
if (index === 0) {
|
|
279
|
-
setInputValue("");
|
|
280
|
-
} else {
|
|
281
|
-
setInputValue(inputHistory[inputHistory.length - index].value ?? "");
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
setHistoryIndex(index);
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
const onKeyDown = (event: KeyboardEvent) => {
|
|
288
|
-
const value = (event.target as HTMLInputElement).value;
|
|
289
|
-
const { key } = event;
|
|
290
|
-
|
|
291
|
-
if (key === "Enter") {
|
|
292
|
-
void submitInput(value);
|
|
293
|
-
setInputKey((previousKey) => previousKey + 1);
|
|
294
|
-
} else if (key === "ArrowUp") {
|
|
295
|
-
event.preventDefault();
|
|
296
|
-
updateHistoryIndex(1);
|
|
297
|
-
} else if (key === "ArrowDown") {
|
|
298
|
-
event.preventDefault();
|
|
299
|
-
updateHistoryIndex(-1);
|
|
300
|
-
} else if (!stream && (event.ctrlKey || event.metaKey) && key === "c") {
|
|
301
|
-
setInputValue((value) => value + "^C");
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
|
|
305
|
-
const onChange = (event: KeyboardEvent) => {
|
|
306
|
-
const value = (event.target as HTMLInputElement).value;
|
|
307
|
-
return setInputValue(value);
|
|
308
|
-
};
|
|
309
|
-
|
|
310
|
-
const displayHistory = () => {
|
|
311
|
-
const visibleHistory = history.slice(-16);
|
|
312
|
-
let startIndex = 0;
|
|
313
|
-
|
|
314
|
-
visibleHistory.forEach((entry, index) => {
|
|
315
|
-
if (entry.clear)
|
|
316
|
-
startIndex = index + 1;
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
return visibleHistory.slice(startIndex).map(({ text }, index) => {
|
|
320
|
-
return <OutputLine text={text} key={index}/>;
|
|
321
|
-
});
|
|
322
|
-
};
|
|
323
|
-
|
|
324
|
-
const onMouseDown = (event: MouseEvent) => {
|
|
325
|
-
focus?.(event);
|
|
326
|
-
|
|
327
|
-
if (event.button === 2) {
|
|
328
|
-
event.preventDefault();
|
|
329
|
-
|
|
330
|
-
navigator.clipboard.readText?.().then((text) => {
|
|
331
|
-
setInputValue(inputValue + text);
|
|
332
|
-
}).catch((error) => {
|
|
333
|
-
console.error(error);
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
};
|
|
337
|
-
|
|
338
|
-
const onContextMenu = (event: Event) => {
|
|
339
|
-
event.preventDefault();
|
|
340
|
-
};
|
|
341
|
-
|
|
342
|
-
return (
|
|
343
|
-
<div
|
|
344
|
-
ref={ref}
|
|
345
|
-
className={styles.Terminal}
|
|
346
|
-
onMouseDown={onMouseDown as unknown as MouseEventHandler}
|
|
347
|
-
onContextMenu={onContextMenu as unknown as MouseEventHandler}
|
|
348
|
-
onClick={(event) => {
|
|
349
|
-
if (window.getSelection()?.toString() === "") {
|
|
350
|
-
event.preventDefault();
|
|
351
|
-
(inputRef.current as HTMLInputElement | null)?.focus();
|
|
352
|
-
}
|
|
353
|
-
}}
|
|
354
|
-
>
|
|
355
|
-
{displayHistory()}
|
|
356
|
-
{!stream
|
|
357
|
-
? <InputLine
|
|
358
|
-
key={inputKey}
|
|
359
|
-
value={inputValue}
|
|
360
|
-
prefix={prefix}
|
|
361
|
-
onKeyDown={onKeyDown}
|
|
362
|
-
onChange={onChange}
|
|
363
|
-
inputRef={inputRef as unknown as MutableRefObject<HTMLInputElement>}
|
|
364
|
-
/>
|
|
365
|
-
: <OutputLine text={streamOutput ?? ""}/>
|
|
366
|
-
}
|
|
367
|
-
</div>
|
|
368
|
-
);
|
|
369
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { ANSI } from "@prozilla-os/core";
|
|
2
|
-
|
|
3
|
-
export const USERNAME = "user";
|
|
4
|
-
export const HOSTNAME = "prozilla-os";
|
|
5
|
-
export const MAX_WIDTH = 50;
|
|
6
|
-
|
|
7
|
-
export const WELCOME_MESSAGE = `${ANSI.fg.cyan + ANSI.decoration.dim}$APP_NAME - Made by Prozilla${ANSI.reset}`
|
|
8
|
-
+ `\n${ANSI.decoration.dim}Type 'help' for a list of commands.${ANSI.reset}\n`;
|
|
9
|
-
|
|
10
|
-
export const ASCII_LOGO = `
|
|
11
|
-
:.
|
|
12
|
-
-==.
|
|
13
|
-
.=====:
|
|
14
|
-
---::..:=======-.
|
|
15
|
-
:===+=----------::..
|
|
16
|
-
=+=---------------:..
|
|
17
|
-
--------------------:.
|
|
18
|
-
.:-+=----*###*--*####=---.
|
|
19
|
-
:==+----#%+-+%#-##%*+----:.
|
|
20
|
-
.=----#%+-+%#-*+-%#+---:.
|
|
21
|
-
==----*###*--*###*----.
|
|
22
|
-
==+-------------------:.
|
|
23
|
-
...::---------------:.
|
|
24
|
-
.::---------::..
|
|
25
|
-
....::... `;
|
|
26
|
-
|
|
27
|
-
export const ANSI_LOGO_COLOR = ANSI.fg.cyan;
|
|
28
|
-
export const ANSI_ASCII_LOGO = `
|
|
29
|
-
:.
|
|
30
|
-
-==.
|
|
31
|
-
.=====:
|
|
32
|
-
---::..:=======-.
|
|
33
|
-
:===+=----------::..
|
|
34
|
-
=+=---------------:..
|
|
35
|
-
--------------------:.
|
|
36
|
-
.:-+=----${ANSI.fg.white}*###*${ANSI_LOGO_COLOR}--${ANSI.fg.white}*####=${ANSI_LOGO_COLOR}---.
|
|
37
|
-
:==+----${ANSI.fg.white}#%+${ANSI_LOGO_COLOR}-${ANSI.fg.white}+%#${ANSI_LOGO_COLOR}-${ANSI.fg.white}##%*+${ANSI_LOGO_COLOR}----:.
|
|
38
|
-
.=----${ANSI.fg.white}#%+${ANSI_LOGO_COLOR}-${ANSI.fg.white}+%#${ANSI_LOGO_COLOR}-${ANSI.fg.white}*+${ANSI_LOGO_COLOR}-${ANSI.fg.white}%#+${ANSI_LOGO_COLOR}---:.
|
|
39
|
-
==----${ANSI.fg.white}*###*${ANSI_LOGO_COLOR}--${ANSI.fg.white}*###*${ANSI_LOGO_COLOR}----.
|
|
40
|
-
==+-------------------:.
|
|
41
|
-
...::---------------:.
|
|
42
|
-
.::---------::..
|
|
43
|
-
....::... `;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { ANSI } from "@prozilla-os/core";
|
|
2
|
-
|
|
3
|
-
export function formatError(commandName: string, error: string): string {
|
|
4
|
-
return `${ANSI.fg.red}${commandName}: ${error}${ANSI.reset}`;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function removeAnsi(string: string): string {
|
|
8
|
-
// eslint-disable-next-line no-control-regex
|
|
9
|
-
return string.replace(/\u001b\[([0-9]+)m/gm, "");
|
|
10
|
-
}
|
package/src/core/command.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { App, SettingsManager, SystemManager, VirtualFolder, VirtualRoot } from "@prozilla-os/core";
|
|
2
|
-
import { Stream } from "./stream";
|
|
3
|
-
|
|
4
|
-
type Option = {
|
|
5
|
-
long: string,
|
|
6
|
-
short: string,
|
|
7
|
-
isInput: boolean
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export type CommandResponse = string | { blank: boolean } | void | Stream;
|
|
11
|
-
export type ExecuteParams = {
|
|
12
|
-
promptOutput?: Function,
|
|
13
|
-
pushHistory?: Function,
|
|
14
|
-
virtualRoot?: VirtualRoot,
|
|
15
|
-
currentDirectory: VirtualFolder,
|
|
16
|
-
setCurrentDirectory?: Function,
|
|
17
|
-
username?: string,
|
|
18
|
-
hostname?: string,
|
|
19
|
-
rawInputValue?: string,
|
|
20
|
-
options?: string[],
|
|
21
|
-
exit?: Function,
|
|
22
|
-
inputs?: Record<string, string>,
|
|
23
|
-
timestamp: number,
|
|
24
|
-
settingsManager: SettingsManager,
|
|
25
|
-
systemManager: SystemManager,
|
|
26
|
-
app: App,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
type Execute = (args?: string[], params?: ExecuteParams) => CommandResponse | Promise<CommandResponse>;
|
|
30
|
-
|
|
31
|
-
type Manual = {
|
|
32
|
-
purpose?: string,
|
|
33
|
-
usage?: string,
|
|
34
|
-
description?: string,
|
|
35
|
-
options?: object
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export class Command {
|
|
39
|
-
name: string = "command";
|
|
40
|
-
options: Option[] = [];
|
|
41
|
-
manual: Manual | undefined;
|
|
42
|
-
requireArgs: boolean | undefined;
|
|
43
|
-
requireOptions: boolean | undefined;
|
|
44
|
-
|
|
45
|
-
execute: Execute = () => {};
|
|
46
|
-
|
|
47
|
-
setName(name: string): Command {
|
|
48
|
-
this.name = name;
|
|
49
|
-
|
|
50
|
-
if (!this.manual?.usage) {
|
|
51
|
-
if (!this.manual)
|
|
52
|
-
this.manual = {};
|
|
53
|
-
|
|
54
|
-
this.manual.usage = name;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return this;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
setExecute(execute: Execute): Command {
|
|
61
|
-
this.execute = execute;
|
|
62
|
-
return this;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
setRequireArgs(value: boolean): Command {
|
|
66
|
-
this.requireArgs = value;
|
|
67
|
-
return this;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
setRequireOptions(value: boolean): Command {
|
|
71
|
-
this.requireOptions = value;
|
|
72
|
-
return this;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
setManual({ purpose, usage, description, options }: Manual): Command {
|
|
76
|
-
this.manual = { purpose, usage, description, options };
|
|
77
|
-
return this;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
addOption({ short, long, isInput }: Option): Command {
|
|
81
|
-
this.options.push({ short, long, isInput });
|
|
82
|
-
return this;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
getOption(key: string): Option | null {
|
|
86
|
-
let matchingOption: Option | null = null;
|
|
87
|
-
|
|
88
|
-
this.options.forEach((option) => {
|
|
89
|
-
if (option.short === key || option.long === key)
|
|
90
|
-
matchingOption = option;
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
return matchingOption;
|
|
94
|
-
}
|
|
95
|
-
}
|
package/src/core/commands/cat.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { VirtualFile } from "@prozilla-os/core";
|
|
2
|
-
import { formatError } from "../_utils/terminal.utils";
|
|
3
|
-
import { Command, ExecuteParams } from "../command";
|
|
4
|
-
|
|
5
|
-
export const cat = new Command()
|
|
6
|
-
.setRequireArgs(true)
|
|
7
|
-
.setManual({
|
|
8
|
-
purpose: "Concetenate files and display on the terminal screen",
|
|
9
|
-
usage: "cat [options] [files]",
|
|
10
|
-
description: "Concetenate files to standard output."
|
|
11
|
-
})
|
|
12
|
-
.setExecute(function(this: Command, args, params) {
|
|
13
|
-
const { currentDirectory, options } = params as ExecuteParams;
|
|
14
|
-
const fileId = (args as string[])[0];
|
|
15
|
-
const { name, extension } = VirtualFile.splitId(fileId);
|
|
16
|
-
const file = currentDirectory.findFile(name, extension);
|
|
17
|
-
|
|
18
|
-
if (!file)
|
|
19
|
-
return formatError(this.name, `${fileId}: No such file`);
|
|
20
|
-
|
|
21
|
-
if (file.content) {
|
|
22
|
-
if (!options?.includes("e")) {
|
|
23
|
-
return file.content;
|
|
24
|
-
} else {
|
|
25
|
-
// Append "$" at the end of every line
|
|
26
|
-
return file.content.split("\n").join("$\n") + "$";
|
|
27
|
-
}
|
|
28
|
-
} else if (file.source) {
|
|
29
|
-
return `Src: ${file.source}`;
|
|
30
|
-
} else {
|
|
31
|
-
return { blank: true };
|
|
32
|
-
}
|
|
33
|
-
});
|
package/src/core/commands/cd.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { formatError } from "../_utils/terminal.utils";
|
|
2
|
-
import { Command, ExecuteParams } from "../command";
|
|
3
|
-
|
|
4
|
-
export const cd = new Command()
|
|
5
|
-
.setManual({
|
|
6
|
-
purpose: "Change the current directory",
|
|
7
|
-
usage: "cd [PATH]",
|
|
8
|
-
description: "Change working directory to given path (the home directory by default)."
|
|
9
|
-
})
|
|
10
|
-
.setExecute(function(this: Command, args, params) {
|
|
11
|
-
const { currentDirectory, setCurrentDirectory } = params as ExecuteParams;
|
|
12
|
-
const path = (args as string[])[0] ?? "~";
|
|
13
|
-
const destination = currentDirectory.navigate(path);
|
|
14
|
-
|
|
15
|
-
if (!destination)
|
|
16
|
-
return formatError(this.name, `${(args as string[])[0]}: No such file or directory`);
|
|
17
|
-
|
|
18
|
-
setCurrentDirectory?.(destination);
|
|
19
|
-
return { blank: true };
|
|
20
|
-
});
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Command, ExecuteParams } from "../command";
|
|
2
|
-
|
|
3
|
-
export const clear = new Command()
|
|
4
|
-
.setManual({
|
|
5
|
-
purpose: "Clear terminal screen",
|
|
6
|
-
})
|
|
7
|
-
.setExecute(function(args, params) {
|
|
8
|
-
const { pushHistory } = params as ExecuteParams;
|
|
9
|
-
pushHistory?.({
|
|
10
|
-
clear: true,
|
|
11
|
-
isInput: false
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
return { blank: true };
|
|
15
|
-
});
|