the prompt now displays and works with unknown command
This commit is contained in:
@ -1,33 +0,0 @@
|
|||||||
let working_dir = "user"
|
|
||||||
|
|
||||||
const enum EntryType {
|
|
||||||
Directory,
|
|
||||||
File
|
|
||||||
}
|
|
||||||
|
|
||||||
type File = string
|
|
||||||
type Entry<T> = {
|
|
||||||
readonly inner: T,
|
|
||||||
readonly type: EntryType
|
|
||||||
}
|
|
||||||
function Entry<T = File>(inner: T): Entry<T> {
|
|
||||||
const type = typeof inner == "object" ? EntryType.Directory : EntryType.File
|
|
||||||
return { inner: inner, type: type }
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
["about_me.txt"]: Entry(""),
|
|
||||||
["services.txt"]: Entry("")
|
|
||||||
}
|
|
||||||
const home = {
|
|
||||||
["user"]: Entry(user)
|
|
||||||
}
|
|
||||||
const root = {
|
|
||||||
["bin"]: Entry({}),
|
|
||||||
["home"]: Entry(home)
|
|
||||||
}
|
|
||||||
const fs = {
|
|
||||||
["/"]: Entry(root)
|
|
||||||
}
|
|
||||||
|
|
||||||
export { fs, working_dir }
|
|
@ -1,21 +1,26 @@
|
|||||||
import { working_dir } from "../../fs"
|
import { working_dir } from "../fs"
|
||||||
|
|
||||||
function ls() {
|
type args = string[]
|
||||||
|
|
||||||
|
function ls(args: args) {
|
||||||
|
console.log(args)
|
||||||
|
}
|
||||||
|
|
||||||
|
function pwd(args: args) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function pwd() {
|
function cat(args: args) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function cat() {
|
interface commands_list {
|
||||||
|
[index: string]: (args: args) => void
|
||||||
}
|
}
|
||||||
|
const commands: commands_list = {
|
||||||
const commands = {
|
|
||||||
["ls"]: ls,
|
["ls"]: ls,
|
||||||
["pwd"]: pwd,
|
["pwd"]: pwd,
|
||||||
["cat"]: cat,
|
["cat"]: cat,
|
||||||
}
|
}
|
||||||
|
|
||||||
export { commands }
|
export default commands
|
41
src/components/react/shell/command/run.tsx
Normal file
41
src/components/react/shell/command/run.tsx
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import type { JSX } from "react";
|
||||||
|
import type { Root } from "react-dom/client";
|
||||||
|
import { type newElement } from "../../terminal/exec";
|
||||||
|
|
||||||
|
import commands from "./list";
|
||||||
|
|
||||||
|
type Renderer<T> = React.Dispatch<React.SetStateAction<T>>
|
||||||
|
|
||||||
|
function trim(stdin: string): string {
|
||||||
|
const trimmed_str: string[] = []
|
||||||
|
stdin.split(" ").forEach(s => { if (s != "") { trimmed_str.push(s) } })
|
||||||
|
return trimmed_str.join(" ")
|
||||||
|
}
|
||||||
|
|
||||||
|
function to_args(trimmed_str: string): string[] {
|
||||||
|
return trimmed_str.split(" ")
|
||||||
|
}
|
||||||
|
|
||||||
|
function valid_command(args: string[]): boolean {
|
||||||
|
for (const command_in_list in commands) {
|
||||||
|
const command = args[0]
|
||||||
|
if (command === command_in_list) {
|
||||||
|
commands[command_in_list](args)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
function unknown_command(name: string) {
|
||||||
|
return <p>{`sh: Unknown command: ${name}`}</p>
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function run(stdin: string) {
|
||||||
|
const args = to_args(trim(stdin))
|
||||||
|
|
||||||
|
if (args[0] !== "" && !valid_command(args)) {
|
||||||
|
return unknown_command(args[0])
|
||||||
|
}
|
||||||
|
return <></>
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
import { useState } from "react"
|
|
||||||
import type { JSX } from "react/jsx-dev-runtime"
|
|
||||||
|
|
||||||
import Display from "./prompt"
|
import Display from "./prompt"
|
||||||
|
import run from "./command/run"
|
||||||
|
|
||||||
|
import { type newElement } from "../terminal/exec";
|
||||||
|
import type { JSX } from "react";
|
||||||
|
|
||||||
type SetStateAction<T> = React.Dispatch<React.SetStateAction<T>>
|
|
||||||
const enum Key {
|
const enum Key {
|
||||||
Enter = "Enter",
|
Enter = "Enter",
|
||||||
ArrowUp = "ArrowUp",
|
ArrowUp = "ArrowUp",
|
||||||
@ -12,32 +12,36 @@ const enum Key {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function display_prompt() {
|
function display_prompt() {
|
||||||
return <>
|
return <div className="shell-prompt">
|
||||||
<Display/>
|
<Display/>
|
||||||
<input className="shell-ps1" type="text" spellCheck={false}/>
|
<input className="shell-ps1" type="text" spellCheck={false}/>
|
||||||
</>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_current_prompt(): HTMLInputElement | undefined {
|
function get_current_prompt(): HTMLInputElement | undefined {
|
||||||
const shell_input = document.getElementsByClassName("shell-ps1")
|
const shell_input = document.getElementsByClassName("shell-ps1")
|
||||||
|
|
||||||
return shell_input[shell_input.length-1] as HTMLInputElement
|
return shell_input[shell_input.length-1] as HTMLInputElement
|
||||||
}
|
}
|
||||||
|
|
||||||
function new_prompt([existingPrompts, setPrompt]: [JSX.Element[], SetStateAction<JSX.Element[]>]) {
|
function new_prompt(): JSX.Element {
|
||||||
const shell_prompts = document.getElementsByClassName("shell-ps1")
|
const shell_prompts = document.getElementsByClassName("shell-ps1")
|
||||||
|
|
||||||
Array.from(shell_prompts).forEach(shellps1 => {
|
Array.from(shell_prompts).forEach(shellps1 => {
|
||||||
(shellps1 as HTMLInputElement).disabled = true
|
(shellps1 as HTMLInputElement).disabled = true
|
||||||
})
|
})
|
||||||
setPrompt([...existingPrompts, display_prompt()])
|
return display_prompt()
|
||||||
}
|
}
|
||||||
|
|
||||||
function keyboard_event(terminal: HTMLElement, existingPrompts: JSX.Element[], setPrompt: SetStateAction<JSX.Element[]>) {
|
function keyboard_events(terminal_window: HTMLElement, new_element_f: newElement) {
|
||||||
const terminal_event = (keyboard_event: KeyboardEvent) => {
|
const terminal_event = (keyboard_event: KeyboardEvent) => {
|
||||||
if (keyboard_event.key === Key.Enter) {
|
if (keyboard_event.key === Key.Enter) {
|
||||||
const current_prompt = get_current_prompt()
|
const current_prompt = get_current_prompt()
|
||||||
if (current_prompt) {
|
if (current_prompt) {
|
||||||
new_prompt([existingPrompts, setPrompt])
|
const prompt = new_prompt()
|
||||||
terminal.removeEventListener("keydown", terminal_event)
|
const output = run(current_prompt.value)
|
||||||
|
new_element_f([output, prompt])
|
||||||
|
terminal_window.removeEventListener("keydown", terminal_event)
|
||||||
}
|
}
|
||||||
} else if (keyboard_event.key === Key.ArrowUp) {
|
} else if (keyboard_event.key === Key.ArrowUp) {
|
||||||
|
|
||||||
@ -47,10 +51,10 @@ function keyboard_event(terminal: HTMLElement, existingPrompts: JSX.Element[], s
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
terminal.addEventListener("keydown", terminal_event)
|
terminal_window.addEventListener("keydown", terminal_event)
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
keyboard_event,
|
keyboard_events,
|
||||||
display_prompt
|
display_prompt
|
||||||
}
|
}
|
46
src/components/react/shell/fs.ts
Normal file
46
src/components/react/shell/fs.ts
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
let working_dir = "user"
|
||||||
|
|
||||||
|
const enum EntryType {
|
||||||
|
Directory,
|
||||||
|
File
|
||||||
|
}
|
||||||
|
const enum Permissions {
|
||||||
|
r,
|
||||||
|
w,
|
||||||
|
rw
|
||||||
|
}
|
||||||
|
|
||||||
|
type File = string
|
||||||
|
type Entry<T> = {
|
||||||
|
readonly inner: T,
|
||||||
|
readonly type: EntryType,
|
||||||
|
readonly permissions: Permissions
|
||||||
|
}
|
||||||
|
function Entry<T = File>(inner: T, permissions: Permissions): Entry<T> {
|
||||||
|
const type = typeof inner == "object" ? EntryType.Directory : EntryType.File
|
||||||
|
return {
|
||||||
|
inner: inner,
|
||||||
|
type: type,
|
||||||
|
permissions: permissions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = {
|
||||||
|
["about_me.txt"]: Entry("", Permissions.rw),
|
||||||
|
["services.txt"]: Entry("", Permissions.rw)
|
||||||
|
}
|
||||||
|
const home = {
|
||||||
|
["user"]: Entry(user, Permissions.rw)
|
||||||
|
}
|
||||||
|
const root = {
|
||||||
|
["bin"]: Entry({}, Permissions.r),
|
||||||
|
["home"]: Entry(home, Permissions.r)
|
||||||
|
}
|
||||||
|
const fs = {
|
||||||
|
["/"]: Entry(root, Permissions.r)
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
fs,
|
||||||
|
working_dir
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
import { working_dir } from "../fs"
|
import { working_dir } from "./fs"
|
||||||
import { cyan, green } from "./color"
|
import { cyan, green } from "./color"
|
||||||
|
|
||||||
const userAgent = navigator.userAgent
|
const userAgent = navigator.userAgent
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import { useState } from "react"
|
import { useState, type JSX } from "react"
|
||||||
import { red } from "../shell/color"
|
import { red } from "../shell/color"
|
||||||
|
import { display_prompt, keyboard_events } from "../shell/events"
|
||||||
|
|
||||||
import { display_prompt, keyboard_event } from "../shell/events"
|
import React from "react"
|
||||||
|
|
||||||
const terminal_window = document.querySelector("main")
|
const terminal_window = document.querySelector("main")
|
||||||
|
|
||||||
@ -14,14 +15,21 @@ function panic(message: string) {
|
|||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type newElement = (elements: JSX.Element[]) => void
|
||||||
|
|
||||||
export default function Shell() {
|
export default function Shell() {
|
||||||
if (terminal_window) {
|
if (terminal_window) {
|
||||||
const [existingPrompts, setPrompt] = useState([display_prompt()])
|
const [renderedElements, renderElement] = useState([display_prompt()])
|
||||||
keyboard_event(terminal_window, existingPrompts, setPrompt)
|
const new_element_f = (elements: JSX.Element[]) => renderElement([...renderedElements, ...elements])
|
||||||
|
|
||||||
return existingPrompts.map((ps1, k) => <div className="shell-prompt" key={k}>{ps1}</div>)
|
keyboard_events(terminal_window, new_element_f)
|
||||||
|
|
||||||
|
return renderedElements.map((element, k) => <React.Fragment key={k}>{element}</React.Fragment>)
|
||||||
}
|
}
|
||||||
return panic("The <main> element is missing")
|
return panic("The <main> element is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
export { panic }
|
export {
|
||||||
|
panic,
|
||||||
|
type newElement,
|
||||||
|
}
|
@ -7,7 +7,7 @@ import Terminal from '../components/react/terminal/exec';
|
|||||||
<Webpage>
|
<Webpage>
|
||||||
<main>
|
<main>
|
||||||
<Motd/>
|
<Motd/>
|
||||||
<Terminal client:only/>
|
<Terminal client:only="react"/>
|
||||||
</main>
|
</main>
|
||||||
</Webpage>
|
</Webpage>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user