@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.
Files changed (43) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +64 -0
  3. package/package.json +38 -0
  4. package/src/components/Ansi.tsx +174 -0
  5. package/src/components/InputLine.tsx +55 -0
  6. package/src/components/OutputLine.tsx +17 -0
  7. package/src/components/Terminal.module.css +126 -0
  8. package/src/components/Terminal.tsx +369 -0
  9. package/src/constants/terminal.const.ts +43 -0
  10. package/src/core/_utils/terminal.utils.ts +10 -0
  11. package/src/core/command.ts +95 -0
  12. package/src/core/commands/cat.ts +33 -0
  13. package/src/core/commands/cd.ts +20 -0
  14. package/src/core/commands/clear.ts +15 -0
  15. package/src/core/commands/cmatrix.ts +101 -0
  16. package/src/core/commands/compgen.ts +14 -0
  17. package/src/core/commands/cowsay.ts +108 -0
  18. package/src/core/commands/dir.ts +15 -0
  19. package/src/core/commands/echo.ts +10 -0
  20. package/src/core/commands/exit.ts +11 -0
  21. package/src/core/commands/fortune.ts +63 -0
  22. package/src/core/commands/help.ts +28 -0
  23. package/src/core/commands/hostname.ts +10 -0
  24. package/src/core/commands/lolcat.ts +54 -0
  25. package/src/core/commands/ls.ts +31 -0
  26. package/src/core/commands/make.ts +9 -0
  27. package/src/core/commands/man.ts +86 -0
  28. package/src/core/commands/mkdir.ts +17 -0
  29. package/src/core/commands/neofetch.ts +69 -0
  30. package/src/core/commands/pwd.ts +15 -0
  31. package/src/core/commands/reboot.ts +11 -0
  32. package/src/core/commands/reload.ts +11 -0
  33. package/src/core/commands/rev.ts +10 -0
  34. package/src/core/commands/rm.ts +21 -0
  35. package/src/core/commands/rmdir.ts +19 -0
  36. package/src/core/commands/sl.ts +220 -0
  37. package/src/core/commands/touch.ts +27 -0
  38. package/src/core/commands/uptime.ts +10 -0
  39. package/src/core/commands/whatis.ts +22 -0
  40. package/src/core/commands/whoami.ts +10 -0
  41. package/src/core/commands.ts +52 -0
  42. package/src/core/stream.ts +38 -0
  43. package/src/index.ts +7 -0
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Sieben De Beule
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,64 @@
1
+ <div align="center">
2
+ <br />
3
+ <p>
4
+ <a href="https://os.prozilla.dev/"><img src="https://os.prozilla.dev/assets/logo.svg" height="200" alt="ProzillaOS" /></a>
5
+ </p>
6
+ <p>
7
+ <a href="https://github.com/prozilla-os/ProzillaOS/blob/main/LICENSE.md"><img alt="License" src="https://img.shields.io/github/license/Prozilla/ProzillaOS?style=flat-square&color=FF4D5B&label=License"></a>
8
+ <a href="https://github.com/prozilla-os/ProzillaOS"><img alt="Stars" src="https://img.shields.io/github/stars/Prozilla/ProzillaOS?style=flat-square&color=FED24C&label=%E2%AD%90"></a>
9
+ <a href="https://github.com/prozilla-os/ProzillaOS"><img alt="Forks" src="https://img.shields.io/github/forks/Prozilla/ProzillaOS?style=flat-square&color=4D9CFF&label=Forks&logo=github"></a>
10
+ <a href="https://www.npmjs.com/package/prozilla-os"><img alt="NPM Version" src="https://img.shields.io/npm/v/prozilla-os?logo=npm&style=flat-square&label=prozilla-os&color=FF4D5B"></a>
11
+ </p>
12
+ </div>
13
+
14
+ ## About
15
+
16
+ `@prozilla-os/terminal` is a terminal/shell application for ProzillaOS.
17
+
18
+ ## Installation
19
+
20
+ `@prozilla-os/core` is required to run this application.
21
+
22
+ ```sh
23
+ $ npm install @prozilla-os/core @prozilla-os/terminal
24
+ $ yarn add @prozilla-os/core @prozilla-os/terminal
25
+ $ pnpm add @prozilla-os/core @prozilla-os/terminal
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ```tsx
31
+ import { Desktop, ModalsView, ProzillaOS, Taskbar, WindowsView, AppsConfig } from "@prozilla-os/core";
32
+ import { terminal } from "@prozilla-os/terminal";
33
+
34
+ function App() {
35
+ return (
36
+ <ProzillaOS
37
+ systemName="Example"
38
+ tagLine="Powered by ProzillaOS"
39
+ config={{
40
+ apps: new AppsConfig({
41
+ apps: [ terminal ]
42
+ })
43
+ }}
44
+ >
45
+ <Taskbar/>
46
+ <WindowsView/>
47
+ <ModalsView/>
48
+ <Desktop/>
49
+ </ProzillaOS>
50
+ );
51
+ }
52
+ ```
53
+
54
+ ## Links
55
+
56
+ - [Website/demo][website]
57
+ - [GitHub][github]
58
+ - [npm][npm]
59
+ - [Ko-fi][ko-fi]
60
+
61
+ [website]: https://os.prozilla.dev/terminal
62
+ [github]: https://github.com/prozilla-os/ProzillaOS/tree/convert-to-monorepo/packages/apps/terminal
63
+ [npm]: https://www.npmjs.com/package/@prozilla-os/terminal
64
+ [ko-fi]: https://ko-fi.com/prozilla
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@prozilla-os/terminal",
3
+ "description": "A terminal/shell application for ProzillaOS.",
4
+ "version": "1.0.1",
5
+ "homepage": "https://os.prozilla.dev/terminal",
6
+ "author": {
7
+ "name": "Prozilla",
8
+ "email": "business@prozilla.dev",
9
+ "url": "https://prozilla.dev/"
10
+ },
11
+ "main": "src/index.ts",
12
+ "type": "module",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/prozilla-os/ProzillaOS.git",
16
+ "directory": "packages/apps/terminal"
17
+ },
18
+ "license": "MIT",
19
+ "dependencies": {
20
+ "anser": "^2.1.1",
21
+ "escape-carriage": "^1.3.1",
22
+ "react": "^18.3.1",
23
+ "@prozilla-os/core": "1.0.11"
24
+ },
25
+ "devDependencies": {
26
+ "typescript": "^5.4.5"
27
+ },
28
+ "files": [
29
+ "src/**/*"
30
+ ],
31
+ "publishConfig": {
32
+ "registry": "https://registry.npmjs.org/",
33
+ "access": "public"
34
+ },
35
+ "scripts": {
36
+ "test": "tsc --watch"
37
+ }
38
+ }
@@ -0,0 +1,174 @@
1
+ import Anser, { AnserJsonEntry } from "anser";
2
+ import { escapeCarriageReturn } from "escape-carriage";
3
+ import * as React from "react";
4
+ import styles from "./Terminal.module.css";
5
+
6
+ /**
7
+ * Source:
8
+ * https://github.com/nteract/ansi-to-react/blob/master/src/index.ts
9
+ */
10
+
11
+ /**
12
+ * Converts ANSI strings into JSON output.
13
+ * @param input - The input string.
14
+ * @param use_classes - If `true`, HTML classes will be appended
15
+ * to the HTML output.
16
+ * @returns The parsed input.
17
+ */
18
+ function ansiToJSON(input: string, use_classes: boolean | undefined): AnserJsonEntry[] {
19
+ input = escapeCarriageReturn(fixBackspace(input));
20
+ return Anser.ansiToJson(input, {
21
+ json: true,
22
+ remove_empty: true,
23
+ use_classes,
24
+ });
25
+ }
26
+
27
+ /**
28
+ * Create a class string.
29
+ * @returns class name(s)
30
+ */
31
+ function createClass(bundle: AnserJsonEntry): string | null {
32
+ const classNames = [];
33
+
34
+ if (bundle.bg) {
35
+ classNames.push(styles[`${bundle.bg}-bg`]);
36
+ }
37
+ if (bundle.fg) {
38
+ classNames.push(styles[`${bundle.fg}-fg`]);
39
+ }
40
+ if (bundle.decoration) {
41
+ classNames.push(styles[`ansi-${bundle.decoration}`]);
42
+ }
43
+
44
+ if (classNames.length === 0) {
45
+ return null;
46
+ }
47
+
48
+ return classNames.join(" ");
49
+ }
50
+
51
+ /**
52
+ * Create the style attribute.
53
+ * @param bundle
54
+ * @returns returns the style object
55
+ */
56
+ function createStyle(bundle: AnserJsonEntry): object {
57
+ const style: Record<string, string> = {};
58
+ if (bundle.bg) {
59
+ style.backgroundColor = `rgb(${bundle.bg})`;
60
+ }
61
+ if (bundle.fg) {
62
+ style.color = `rgb(${bundle.fg})`;
63
+ }
64
+ switch (bundle.decoration) {
65
+ case "bold":
66
+ style.fontWeight = "bold";
67
+ break;
68
+ case "dim":
69
+ style.opacity = "0.5";
70
+ break;
71
+ case "italic":
72
+ style.fontStyle = "italic";
73
+ break;
74
+ case "hidden":
75
+ style.visibility = "hidden";
76
+ break;
77
+ case "strikethrough":
78
+ style.textDecoration = "line-through";
79
+ break;
80
+ case "underline":
81
+ style.textDecoration = "underline";
82
+ break;
83
+ case "blink":
84
+ style.textDecoration = "blink";
85
+ break;
86
+ default:
87
+ break;
88
+ }
89
+ return style;
90
+ }
91
+
92
+ /**
93
+ * Converts an Anser bundle into a React Node.
94
+ * @param linkify - whether links should be converting into clickable anchor tags.
95
+ * @param useClasses - should render the span with a class instead of style.
96
+ * @param bundle - Anser output.
97
+ */
98
+ function convertBundleIntoReact(linkify: boolean, useClasses: boolean, bundle: AnserJsonEntry, key: number) {
99
+ const style = useClasses ? null : createStyle(bundle);
100
+ const className = useClasses ? createClass(bundle) : null;
101
+
102
+ if (!linkify) {
103
+ return React.createElement(
104
+ "pre",
105
+ { style, key, className },
106
+ bundle.content
107
+ );
108
+ }
109
+
110
+ const content = [];
111
+ const linkRegex = /(\s|^)(https?:\/\/(?:www\.|(?!www))[^\s.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/g;
112
+
113
+ let index = 0;
114
+ let match: RegExpExecArray | null;
115
+ while ((match = linkRegex.exec(bundle.content)) !== null) {
116
+ const [, pre, url] = match;
117
+
118
+ const startIndex = match.index + pre.length;
119
+ if (startIndex > index) {
120
+ content.push(bundle.content.substring(index, startIndex));
121
+ }
122
+
123
+ // Make sure the href we generate from the link is fully qualified. We assume http
124
+ // if it starts with a www because many sites don't support https
125
+ const href = url.startsWith("www.") ? `http://${url}` : url;
126
+ content.push(
127
+ React.createElement(
128
+ "a",
129
+ {
130
+ key: index,
131
+ href,
132
+ target: "_blank",
133
+ },
134
+ `${url}`
135
+ )
136
+ );
137
+
138
+ index = linkRegex.lastIndex;
139
+ }
140
+
141
+ if (index < bundle.content.length) {
142
+ content.push(bundle.content.substring(index));
143
+ }
144
+
145
+ return React.createElement("span", { style, key, className }, content);
146
+ }
147
+
148
+ export function Ansi(props: { children?: string | undefined; linkify?: boolean | undefined; className?: string | undefined; useClasses?: boolean | undefined; }) {
149
+ const { className, useClasses, children, linkify } = props;
150
+ return React.createElement(
151
+ "code",
152
+ { className },
153
+ ansiToJSON(children ?? "", useClasses ?? false).map(
154
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
155
+ convertBundleIntoReact.bind(null, linkify ?? false, useClasses ?? false)
156
+ )
157
+ );
158
+ }
159
+
160
+ // This is copied from the Jupyter Classic source code
161
+ // notebook/static/base/js/utils.js to handle \b in a way
162
+ // that is **compatible with Jupyter classic**. One can
163
+ // argue that this behavior is questionable:
164
+ // https://stackoverflow.com/questions/55440152/multiple-b-doesnt-work-as-expected-in-jupyter#
165
+ function fixBackspace(txt: string) {
166
+ let tmp = txt;
167
+ do {
168
+ txt = tmp;
169
+ // Cancel out anything-but-newline followed by backspace
170
+ // eslint-disable-next-line no-control-regex
171
+ tmp = txt.replace(/[^\n]\x08/gm, "");
172
+ } while (tmp.length < txt.length);
173
+ return txt;
174
+ }
@@ -0,0 +1,55 @@
1
+ import { CSSProperties, MutableRefObject, useState } from "react";
2
+ import styles from "./Terminal.module.css";
3
+ import { Ansi } from "./Ansi";
4
+
5
+
6
+ interface InputLineProps {
7
+ value: string;
8
+ prefix: string;
9
+ onChange: Function;
10
+ onKeyUp?: Function;
11
+ onKeyDown?: Function;
12
+ inputRef: MutableRefObject<HTMLInputElement>;
13
+ }
14
+
15
+ export function InputLine({ value, prefix, onChange, onKeyUp, onKeyDown, inputRef }: InputLineProps) {
16
+ const [cursorPosition, setCursorPosition] = useState(0);
17
+
18
+ const checkCursorPosition = () => {
19
+ const selectionStart = inputRef.current?.selectionStart;
20
+ if (selectionStart != null) setCursorPosition(selectionStart);
21
+ };
22
+
23
+ return (
24
+ <span className={styles.Input}>
25
+ {prefix && <Ansi className={styles.Prefix} useClasses>{prefix}</Ansi>}
26
+ <span className={styles["Input-container"]} style={{ "--cursor-offset": cursorPosition } as CSSProperties}>
27
+ <span aria-hidden="true">{value}</span>
28
+ <input
29
+ id="input"
30
+ value={value}
31
+ aria-label="Command input"
32
+ onChange={(event) => {
33
+ onChange(event);
34
+ checkCursorPosition();
35
+ }}
36
+ ref={inputRef}
37
+ onKeyUp={(event) => { onKeyUp?.(event); }}
38
+ onKeyDown={(event) => {
39
+ onKeyDown?.(event);
40
+ checkCursorPosition();
41
+ }}
42
+ onClick={checkCursorPosition}
43
+ onTouchEnd={checkCursorPosition}
44
+ onSelect={checkCursorPosition}
45
+ onCut={checkCursorPosition}
46
+ onCopy={checkCursorPosition}
47
+ onPaste={checkCursorPosition}
48
+ spellCheck={false}
49
+ autoComplete="off"
50
+ autoFocus
51
+ />
52
+ </span>
53
+ </span>
54
+ );
55
+ }
@@ -0,0 +1,17 @@
1
+ import { FC, forwardRef } from "react";
2
+ import { Ansi } from "./Ansi";
3
+ import styles from "./Terminal.module.css";
4
+
5
+ interface OutputLineProps {
6
+ text?: string;
7
+ }
8
+
9
+ export const OutputLine: FC<OutputLineProps> = forwardRef<HTMLDivElement>(({ text }: OutputLineProps, ref) => {
10
+ const lines = text?.split("\n");
11
+
12
+ return <div ref={ref}>
13
+ {lines?.map((line, index) =>
14
+ <Ansi key={index} className={styles.Output} useClasses>{line === "" ? " " : line}</Ansi>
15
+ )}
16
+ </div>;
17
+ }) as FC;
@@ -0,0 +1,126 @@
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; }