Added more terminal commands
This commit is contained in:
parent
371c1a8300
commit
5ea3244c09
6 changed files with 82 additions and 13 deletions
|
|
@ -34,7 +34,7 @@ export function Window({ id, app, size, position, focused = false }) {
|
|||
return (
|
||||
<Draggable
|
||||
axis="both"
|
||||
handle=".Header"
|
||||
handle={".Handle"}
|
||||
defaultPosition={{ x: position.x, y: position.y }}
|
||||
position={null}
|
||||
scale={1}
|
||||
|
|
@ -56,7 +56,7 @@ export function Window({ id, app, size, position, focused = false }) {
|
|||
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`}/>
|
||||
<p>{app.name}</p>
|
||||
<button onClick={() => setMinimized(!minimized)}>
|
||||
|
|
|
|||
|
|
@ -3,6 +3,9 @@ import styles from "./Terminal.module.css";
|
|||
import { Command } from "./commands.js";
|
||||
import { useVirtualRoot } from "../../../hooks/VirtualRootContext.js";
|
||||
|
||||
const USERNAME = "user";
|
||||
const HOSTNAME = "prozilla-os";
|
||||
|
||||
function OutputLine({ text }) {
|
||||
return (
|
||||
<p className={styles.Output}>{text}</p>
|
||||
|
|
@ -34,7 +37,7 @@ export function Terminal() {
|
|||
const virtualRoot = useVirtualRoot();
|
||||
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 pushHistory = (entry) => {
|
||||
|
|
@ -80,7 +83,9 @@ export function Terminal() {
|
|||
pushHistory,
|
||||
virtualRoot,
|
||||
currentDirectory,
|
||||
setCurrentDirectory
|
||||
setCurrentDirectory,
|
||||
username: USERNAME,
|
||||
hostname: HOSTNAME,
|
||||
});
|
||||
|
||||
if (response == null)
|
||||
|
|
|
|||
|
|
@ -48,10 +48,10 @@ export class Command {
|
|||
if (!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 contents = fodlerNames.concat(fileNames);
|
||||
const contents = folderNames.concat(fileNames);
|
||||
|
||||
if (contents.length === 0)
|
||||
return { blank: true };
|
||||
|
|
@ -59,7 +59,8 @@ export class Command {
|
|||
return contents.sort((nameA, nameB) => nameA.localeCompare(nameB)).join(" ");
|
||||
}),
|
||||
new Command("cd", (args, { currentDirectory, setCurrentDirectory }) => {
|
||||
const destination = currentDirectory.navigate(args[0]);
|
||||
const path = args[0] ?? "~";
|
||||
const destination = currentDirectory.navigate(path);
|
||||
|
||||
if (!destination)
|
||||
return `cd: ${args[0]}: No such file or directory`;
|
||||
|
|
@ -67,6 +68,62 @@ export class Command {
|
|||
console.log(destination);
|
||||
setCurrentDirectory(destination);
|
||||
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;
|
||||
}),
|
||||
]
|
||||
}
|
||||
6
src/features/utils/array.js
Normal file
6
src/features/utils/array.js
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
export function removeFromArray(item, array) {
|
||||
const index = array.indexOf(item);
|
||||
if (index !== -1) {
|
||||
array.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
|
@ -42,11 +42,11 @@ export class VirtualBase {
|
|||
}
|
||||
|
||||
get path() {
|
||||
return this.parent?.path + "/" + this.id;
|
||||
return this.alias ?? this.absolutePath;
|
||||
}
|
||||
|
||||
get formattedPath() {
|
||||
return this.alias ?? this.path;
|
||||
get absolutePath() {
|
||||
return this.parent?.path + "/" + this.id;
|
||||
}
|
||||
|
||||
getRoot() {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { removeFromArray } from "../utils/array.js";
|
||||
import { VirtualBase } from "./virtual-base.js";
|
||||
import { VirtualFile } from "./virtual-file.js";
|
||||
|
||||
|
|
@ -144,9 +145,9 @@ export class VirtualFolder extends VirtualBase {
|
|||
|
||||
remove(child) {
|
||||
if (child instanceof VirtualFile) {
|
||||
// Remove file by id
|
||||
removeFromArray(child, this.files);
|
||||
} else if (child instanceof VirtualFolder) {
|
||||
// Remove folder by id
|
||||
removeFromArray(child, this.subFolders);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue