move builtin commands out of list.ts into their own files

This commit is contained in:
2025-02-22 01:16:37 -05:00
parent c5692b1b7f
commit 1fe21b1592
6 changed files with 67 additions and 55 deletions

View File

@ -0,0 +1,6 @@
import type { Args, Term } from "../list";
export default function cat(term: Term, args: Args): boolean {
return true
}

View File

@ -0,0 +1,13 @@
import { set_working_dir, SetDirStatus } from "../../fs/fn"
import type { Args, Term } from "../list"
export default function cd(term: Term, args: Args): boolean {
const new_dir_status = set_working_dir(args[1])
if (new_dir_status === SetDirStatus.NotADirectory) {
// return <p>{"cd: \""}{bold(args[1])}{"\" is not a directory"}</p>
} else if (new_dir_status === SetDirStatus.NotFound) {
// return <p>{"cd: The directory \""}{bold(args[1])}{"\" does not exist"}</p>
}
return true
}

View File

@ -0,0 +1,19 @@
import type { Args, Term } from "../list"
export default function clear(term: Term, args: Args): boolean {
Array.from(term.children).forEach(node => {
if (node.tagName === "DIV") {
if (node.className === "shell-prompt") {
const input = node.getElementsByClassName("shell-ps1")[0] as HTMLInputElement
if (input.disabled || input.value === "clear") {
node.remove()
}
} else {
node.remove()
}
} else if (node.tagName === "P") {
node.remove()
}
})
return true
}

View File

@ -0,0 +1,11 @@
import type { Args, Term } from "../list";
export default function ls(term: Term, args: Args): boolean {
// if (args[1] === undefined) {
// for (const dir_name in working_dir) {
// }
// return <p>{`${working_dir}`}</p>
// }
return true
}

View File

@ -0,0 +1,9 @@
import { get_working_dir_name_full } from "../../fs/fn";
import type { Args, Term } from "../list";
import stdout from "../../../elements/stdout";
export default function pwd(term: Term, args: Args): boolean {
term.appendChild(stdout(get_working_dir_name_full()))
return true
}

View File

@ -1,70 +1,24 @@
import { get_working_dir_name_full, set_working_dir, SetDirStatus } from "../fs/fn"
import history_cmd from "./builtin/history"
import stdout from "../../elements/stdout"
import clear from "./builtin/clear"
import pwd from "./builtin/pwd"
import cat from "./builtin/cat"
import cd from "./builtin/cd"
import ls from "./builtin/ls"
type Term = HTMLElement
type Args = string[]
function clear(term: Term, args: Args): boolean {
Array.from(term.children).forEach(node => {
if (node.tagName === "DIV") {
if (node.className === "shell-prompt") {
const input = node.getElementsByClassName("shell-ps1")[0] as HTMLInputElement
if (input.disabled || input.value === "clear") {
node.remove()
}
} else {
node.remove()
}
} else if (node.tagName === "P") {
node.remove()
}
})
return true
}
function cd(term: Term, args: Args): boolean {
const new_dir_status = set_working_dir(args[1])
if (new_dir_status === SetDirStatus.NotADirectory) {
// return <p>{"cd: \""}{bold(args[1])}{"\" is not a directory"}</p>
} else if (new_dir_status === SetDirStatus.NotFound) {
// return <p>{"cd: The directory \""}{bold(args[1])}{"\" does not exist"}</p>
}
return true
}
function ls(term: Term, args: Args): boolean {
// if (args[1] === undefined) {
// for (const dir_name in working_dir) {
// }
// return <p>{`${working_dir}`}</p>
// }
return true
}
function pwd(term: Term, args: Args): boolean {
term.appendChild(stdout(get_working_dir_name_full()))
return true
}
function cat(term: Term, args: Args): boolean {
return true
}
type Command = (term: Term, args: Args) => boolean
interface CommandsList {
[index: string]: Command,
}
const commands: CommandsList = {
["history"]: history_cmd,
["clear"]: clear,
["cd"]: cd,
["ls"]: ls,
["pwd"]: pwd,
["cat"]: cat,
["history"]: history_cmd,
["cd"]: cd,
["ls"]: ls,
}
export default commands