Added more terminal commands

This commit is contained in:
Prozilla 2023-07-22 20:48:45 +02:00
parent 371c1a8300
commit 5ea3244c09
No known key found for this signature in database
GPG key ID: 5858DFE71CAF31EE
6 changed files with 82 additions and 13 deletions

View file

@ -34,7 +34,7 @@ export function Window({ id, app, size, position, focused = false }) {
return ( return (
<Draggable <Draggable
axis="both" axis="both"
handle=".Header" handle={".Handle"}
defaultPosition={{ x: position.x, y: position.y }} defaultPosition={{ x: position.x, y: position.y }}
position={null} position={null}
scale={1} scale={1}
@ -56,7 +56,7 @@ export function Window({ id, app, size, position, focused = false }) {
height: maximized ? screenHeight : size.y, height: maximized ? screenHeight : size.y,
}} }}
> >
<div className={styles["Header"]}> <div className={`${styles.Header} Handle`}>
<ReactSVG className={styles["Window-icon"]} src={process.env.PUBLIC_URL + `/media/applications/icons/${app.id}.svg`}/> <ReactSVG className={styles["Window-icon"]} src={process.env.PUBLIC_URL + `/media/applications/icons/${app.id}.svg`}/>
<p>{app.name}</p> <p>{app.name}</p>
<button onClick={() => setMinimized(!minimized)}> <button onClick={() => setMinimized(!minimized)}>

View file

@ -3,6 +3,9 @@ import styles from "./Terminal.module.css";
import { Command } from "./commands.js"; import { Command } from "./commands.js";
import { useVirtualRoot } from "../../../hooks/VirtualRootContext.js"; import { useVirtualRoot } from "../../../hooks/VirtualRootContext.js";
const USERNAME = "user";
const HOSTNAME = "prozilla-os";
function OutputLine({ text }) { function OutputLine({ text }) {
return ( return (
<p className={styles.Output}>{text}</p> <p className={styles.Output}>{text}</p>
@ -34,7 +37,7 @@ export function Terminal() {
const virtualRoot = useVirtualRoot(); const virtualRoot = useVirtualRoot();
const [currentDirectory, setCurrentDirectory] = useState(virtualRoot.navigate("~")); const [currentDirectory, setCurrentDirectory] = useState(virtualRoot.navigate("~"));
const prefix = `user@prozilla-os:${currentDirectory.formattedPath}$ `; const prefix = `${USERNAME}@${HOSTNAME}:${currentDirectory.root ? "/" : currentDirectory.path}$ `;
const updatedHistory = history; const updatedHistory = history;
const pushHistory = (entry) => { const pushHistory = (entry) => {
@ -80,7 +83,9 @@ export function Terminal() {
pushHistory, pushHistory,
virtualRoot, virtualRoot,
currentDirectory, currentDirectory,
setCurrentDirectory setCurrentDirectory,
username: USERNAME,
hostname: HOSTNAME,
}); });
if (response == null) if (response == null)

View file

@ -48,10 +48,10 @@ export class Command {
if (!directory) if (!directory)
return `ls: Cannot access '${args[0]}': No such file or directory`; return `ls: Cannot access '${args[0]}': No such file or directory`;
const fodlerNames = directory.subFolders.map((folder) => folder.id); const folderNames = directory.subFolders.map((folder) => folder.id);
const fileNames = directory.files.map((file) => file.id); const fileNames = directory.files.map((file) => file.id);
const contents = fodlerNames.concat(fileNames); const contents = folderNames.concat(fileNames);
if (contents.length === 0) if (contents.length === 0)
return { blank: true }; return { blank: true };
@ -59,7 +59,8 @@ export class Command {
return contents.sort((nameA, nameB) => nameA.localeCompare(nameB)).join(" "); return contents.sort((nameA, nameB) => nameA.localeCompare(nameB)).join(" ");
}), }),
new Command("cd", (args, { currentDirectory, setCurrentDirectory }) => { new Command("cd", (args, { currentDirectory, setCurrentDirectory }) => {
const destination = currentDirectory.navigate(args[0]); const path = args[0] ?? "~";
const destination = currentDirectory.navigate(path);
if (!destination) if (!destination)
return `cd: ${args[0]}: No such file or directory`; return `cd: ${args[0]}: No such file or directory`;
@ -67,6 +68,62 @@ export class Command {
console.log(destination); console.log(destination);
setCurrentDirectory(destination); setCurrentDirectory(destination);
return { blank: true }; return { blank: true };
}) }),
new Command("dir", (args, { currentDirectory }) => {
const folderNames = currentDirectory.subFolders.map((folder) => folder.id);
if (folderNames.length === 0)
return { blank: true };
return folderNames.sort((nameA, nameB) => nameA.localeCompare(nameB)).join(" ");
}),
new Command("pwd", (args, { currentDirectory }) => {
if (currentDirectory.root) {
return "/";
} else {
return currentDirectory.absolutePath;
}
}),
new Command("touch", (args, { currentDirectory }) => {
const [name, extension] = args[0].split(".");
if (currentDirectory.findFile(name, extension))
return { blank: true };
currentDirectory.createFile(name, extension);
return { blank: true };
}),
new Command("mkdir", (args, { currentDirectory }) => {
const name = args[0];
if (currentDirectory.findSubFolder(name))
return { blank: true };
currentDirectory.createFolder(name);
return { blank: true };
}),
new Command("rm", (args, { currentDirectory }) => {
const [name, extension] = args[0].split(".");
const file = currentDirectory.findFile(name, extension);
if (!file)
return `rm: ${args[0]}: No such file`;
file.delete();
return { blank: true };
}),
new Command("rmdir", (args, { currentDirectory }) => {
const name = args[0];
const folder = currentDirectory.findSubFolder(name);
if (!folder)
return `rm: ${args[0]}: No such directory`;
folder.delete();
return { blank: true };
}),
new Command("hostname", (args, { hostname }) => {
return hostname;
}),
] ]
} }

View file

@ -0,0 +1,6 @@
export function removeFromArray(item, array) {
const index = array.indexOf(item);
if (index !== -1) {
array.splice(index, 1);
}
}

View file

@ -42,11 +42,11 @@ export class VirtualBase {
} }
get path() { get path() {
return this.parent?.path + "/" + this.id; return this.alias ?? this.absolutePath;
} }
get formattedPath() { get absolutePath() {
return this.alias ?? this.path; return this.parent?.path + "/" + this.id;
} }
getRoot() { getRoot() {

View file

@ -1,3 +1,4 @@
import { removeFromArray } from "../utils/array.js";
import { VirtualBase } from "./virtual-base.js"; import { VirtualBase } from "./virtual-base.js";
import { VirtualFile } from "./virtual-file.js"; import { VirtualFile } from "./virtual-file.js";
@ -144,9 +145,9 @@ export class VirtualFolder extends VirtualBase {
remove(child) { remove(child) {
if (child instanceof VirtualFile) { if (child instanceof VirtualFile) {
// Remove file by id removeFromArray(child, this.files);
} else if (child instanceof VirtualFolder) { } else if (child instanceof VirtualFolder) {
// Remove folder by id removeFromArray(child, this.subFolders);
} }
} }