2 Commits

Author SHA1 Message Date
bf40d524b7 fix history indexing down 2025-02-22 01:39:21 -05:00
1fe21b1592 move builtin commands out of list.ts into their own files 2025-02-22 01:16:37 -05:00
8 changed files with 75 additions and 62 deletions

View File

@ -8,8 +8,8 @@ interface EnterArgs {
readonly closure: InputClosure readonly closure: InputClosure
} }
interface Keys { interface Keys {
enter: (input: EnterArgs) => void, enter: (input: EnterArgs) => void,
up_arrow: (ps1input: HTMLInputElement) => void, up_arrow: (ps1input: HTMLInputElement) => void,
down_arrow: (ps1input: HTMLInputElement) => void, down_arrow: (ps1input: HTMLInputElement) => void,
} }

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,25 @@
import { get_working_dir_name_full, set_working_dir, SetDirStatus } from "../fs/fn" import history from "./builtin/history"
import clear from "./builtin/clear"
import history_cmd from "./builtin/history" import pwd from "./builtin/pwd"
import stdout from "../../elements/stdout" import cat from "./builtin/cat"
import cd from "./builtin/cd"
import ls from "./builtin/ls"
type Term = HTMLElement type Term = HTMLElement
type Args = string[] 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 type Command = (term: Term, args: Args) => boolean
interface CommandsList { interface CommandsList {
[index: string]: Command, [index: string]: Command,
} }
const commands: CommandsList = { const commands: CommandsList = {
["history"]: history,
["clear"]: clear, ["clear"]: clear,
["cd"]: cd,
["ls"]: ls,
["pwd"]: pwd, ["pwd"]: pwd,
["cat"]: cat, ["cat"]: cat,
["history"]: history_cmd, ["cd"]: cd,
["ls"]: ls,
} }
export default commands export default commands

View File

@ -4,9 +4,9 @@ interface HistoryFile {
cursor_reset: () => void cursor_reset: () => void
} }
interface History { interface History {
file: HistoryFile file: HistoryFile
add: (cmd: string) => void, add: (cmd: string) => void,
index_up: (ps1input: HTMLInputElement) => void, index_up: (ps1input: HTMLInputElement) => void,
index_down: (ps1input: HTMLInputElement) => void index_down: (ps1input: HTMLInputElement) => void
} }
@ -36,7 +36,7 @@ history.index_down = function(ps1input: HTMLInputElement) {
if (this.file.cursor!==0) { if (this.file.cursor!==0) {
this.file.cursor-=1 this.file.cursor-=1
if (this.file.cursor!==0) { if (this.file.cursor!==0) {
const item = this.file.inner[this.file.cursor] const item = this.file.inner[this.file.cursor-1]
if (item) { ps1input.value = item } if (item) { ps1input.value = item }
} else { } else {
this.file.cursor_reset() this.file.cursor_reset()