builder for commands now and history help

This commit is contained in:
2025-02-20 22:01:04 -05:00
parent 129d0ff6b4
commit 4f5602a5df
8 changed files with 196 additions and 62 deletions

View File

@ -0,0 +1,24 @@
import type { Args, Term } from "./list";
import { bold } from "../color";
import stdout, { stdout_horizontal } from "../../elements/stdout";
import subcmd, { type SubCommandAction } from "./subcommand";
import history from "../history";
import create from "../../elements/create";
const history_command = subcmd()
history_command.show = {} as SubCommandAction
history_command.show.inner = function(term: Term) {
history.file.inner.forEach((entry, ind) => term.appendChild(stdout(`${ind} ${entry}`)))
}
history_command.show.description = "Show the history"
export default function history_cmd(term: Term, args: Args) {
const subc = args[1]
if (subc) {
const subc_f = history_command[subc]
if (subc_f) { subc_f.inner(term) } else { history_command.help.inner(term) }
}
return true
}

View File

@ -1,17 +1,12 @@
import { bold } from "../color"
import { get_working_dir_name_full, set_working_dir, SetDirStatus } from "../fs/fn"
import create from "../../elements/create"
import history_cmd from "./history"
import stdout from "../../elements/stdout"
type Term = HTMLElement
type Args = string[]
function strout(term: Term, s: string) {
const p = create("p")
p.innerText = s
term.appendChild(p)
}
function clear(term: Term, args: Args): boolean {
Array.from(term.children).forEach(node => {
if (node.tagName === "DIV") {
@ -52,7 +47,7 @@ function ls(term: Term, args: Args): boolean {
}
function pwd(term: Term, args: Args): boolean {
strout(term, get_working_dir_name_full())
term.appendChild(stdout(get_working_dir_name_full()))
return true
}
@ -70,7 +65,12 @@ const commands: CommandsList = {
["ls"]: ls,
["pwd"]: pwd,
["cat"]: cat,
["history"]: history_cmd,
}
export default commands
export { type Command }
export {
type Command,
type Term,
type Args
}

View File

@ -0,0 +1,28 @@
import { stdout_grid } from "../../elements/stdout";
import type { Term } from "./list";
interface SubCommandAction {
inner: (term: Term) => void,
description: string
}
interface SubCommand {
[index: string]: SubCommandAction
}
export default function subcmd(help_description?: string): SubCommand {
const subcommand = {} as SubCommand
subcommand.help = {} as SubCommandAction
subcommand.help.inner = function(term: Term) {
const descriptions: string[] = []
Object.values(subcommand).forEach(v => descriptions.push(v.description))
term.appendChild(stdout_grid(Object.keys(subcommand), descriptions))
}
subcommand.help.description = help_description ? help_description : "Show the help page"
return subcommand
}
export {
type SubCommand,
type SubCommandAction
}

View File

@ -1,15 +1,48 @@
const history_list: string[] = []
interface History {
add: (cmd: string) => void
interface HistoryFile {
inner: string[],
cursor: number,
cursor_reset: () => void
}
interface History {
file: HistoryFile
add: (cmd: string) => void,
index_up: (ps1input: HTMLInputElement) => void,
index_down: (ps1input: HTMLInputElement) => void
}
const history = {} as History
history.file = {} as HistoryFile
history.file.inner = []
history.file.cursor = 0
history.file.cursor_reset = function() {
this.cursor = 0
}
history.add = function(cmd: string) {
if (history_list[history_list.length-1] !== cmd) {
history_list.push(cmd)
if (this.file.inner[this.file.inner.length-1] !== cmd) {
this.file.inner.push(cmd)
}
}
export default history
export { history_list }
history.index_up = function(ps1input: HTMLInputElement) {
const item = this.file.inner[this.file.cursor]
if (item) {
this.file.cursor+=1
ps1input.value = item
}
}
history.index_down = function(ps1input: HTMLInputElement) {
if (this.file.cursor!==0) {
this.file.cursor-=1
if (this.file.cursor!==0) {
const item = this.file.inner[this.file.cursor]
if (item) { ps1input.value = item }
} else {
this.file.cursor_reset()
ps1input.value = ""
}
}
}
export default history