there is now a subcommand system and history
command
This commit is contained in:
@ -1,31 +1,22 @@
|
|||||||
import type { Args, Term } from "../list";
|
import type { Args, Term } from "../list";
|
||||||
import { bold } from "../../color";
|
|
||||||
|
|
||||||
import stdout, { stdout_horizontal } from "../../../elements/stdout";
|
import stdout from "../../../elements/stdout";
|
||||||
import SubCommand, { type SubCommandAction } from "../subcommand";
|
import SubCommand from "../subcommand";
|
||||||
import history from "../../history";
|
import history from "../../history";
|
||||||
import create from "../../../elements/create";
|
|
||||||
|
|
||||||
const history_command = new SubCommand("show and manipulate command history")
|
const history_command = new SubCommand("Show and manipulate command history")
|
||||||
|
|
||||||
history_command.add("show", [], "Show the history", function(term: Term) {
|
history_command.add("show", "Show the history", function(term: Term, _args: Args) {
|
||||||
history.file.inner.forEach((entry, ind) => term.appendChild(stdout(`${ind} ${entry}`)))
|
history.file.inner.forEach((entry, ind) => term.appendChild(stdout(`${ind} ${entry}`)))
|
||||||
})
|
})
|
||||||
|
|
||||||
history_command.add("clear", ["delete"], "Delete the command history", function(term: Term) {
|
history_command.add("clear", "Delete the entire command history", function(term: Term, _args: Args) {
|
||||||
const entries = history.file.inner.length
|
const entries = history.file.inner.length
|
||||||
history.file.inner = []
|
history.file.inner = []
|
||||||
term.appendChild(stdout(`Cleared ${entries} entries from the history.`))
|
term.appendChild(stdout(`Cleared ${entries} entries from the history.`))
|
||||||
})
|
})
|
||||||
|
|
||||||
export default function history_cmd(term: Term, args: Args) {
|
export default function history_cmd(term: Term, args: Args) {
|
||||||
const subc = args[1]
|
history_command.process(term, args)
|
||||||
if (subc) {
|
|
||||||
const subc_f = history_command.data[subc]
|
|
||||||
if (subc_f) { subc_f.inner(term) } else {
|
|
||||||
term.appendChild(SubCommand.unknown("history", subc))
|
|
||||||
history_command.data.help.inner(term)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
@ -2,8 +2,8 @@ import { bold } from "../color";
|
|||||||
import { to_args, trim } from "./parse";
|
import { to_args, trim } from "./parse";
|
||||||
|
|
||||||
import commands, { type Command } from "./list";
|
import commands, { type Command } from "./list";
|
||||||
import create from "../../elements/create";
|
|
||||||
import history from "../history";
|
import history from "../history";
|
||||||
|
import stdout from "../../elements/stdout";
|
||||||
|
|
||||||
type Term = HTMLElement
|
type Term = HTMLElement
|
||||||
|
|
||||||
@ -18,8 +18,7 @@ function valid_command(term: Term, args: string[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function unknown_command(cmd_name: string) {
|
function unknown_command(cmd_name: string) {
|
||||||
const unknown_element = create("p")
|
const unknown_element = stdout("shell: Unknown command: ")
|
||||||
unknown_element.innerText = "shell: Unknown command: "
|
|
||||||
unknown_element.appendChild(bold(cmd_name))
|
unknown_element.appendChild(bold(cmd_name))
|
||||||
return unknown_element
|
return unknown_element
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { get_working_dir_name_full, set_working_dir, SetDirStatus } from "../fs/fn"
|
import { get_working_dir_name_full, set_working_dir, SetDirStatus } from "../fs/fn"
|
||||||
|
|
||||||
import create from "../../elements/create"
|
import history_cmd from "./builtin/history"
|
||||||
import history_cmd from "./history"
|
|
||||||
import stdout from "../../elements/stdout"
|
import stdout from "../../elements/stdout"
|
||||||
|
|
||||||
type Term = HTMLElement
|
type Term = HTMLElement
|
||||||
|
@ -1,27 +1,62 @@
|
|||||||
import { stdout_grid } from "../../elements/stdout";
|
import stdout, { stdout_grid } from "../../elements/stdout";
|
||||||
import type { Term } from "./list";
|
import { bold } from "../color";
|
||||||
|
import type { Args, Term } from "./list";
|
||||||
|
|
||||||
|
type SubCommandClosure = (term: Term, args: Args) => void
|
||||||
interface SubCommandAction {
|
interface SubCommandAction {
|
||||||
inner: (term: Term) => void,
|
inner: SubCommandClosure,
|
||||||
description: string
|
description: string,
|
||||||
}
|
}
|
||||||
interface SubCommand {
|
interface SubCommands {
|
||||||
[index: string]: SubCommandAction
|
[index: string]: SubCommandAction,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function subcmd(help_description?: string): SubCommand {
|
const SubCommand = class {
|
||||||
const subcommand = {} as SubCommand
|
public data: SubCommands //data? less goo!
|
||||||
subcommand.help = {} as SubCommandAction
|
|
||||||
subcommand.help.inner = function(term: Term) {
|
constructor(description: string) {
|
||||||
|
this.data = {}
|
||||||
|
this.data.help = {} as SubCommandAction
|
||||||
|
this.data.help.description = "Display help info"
|
||||||
|
this.data.help.inner = (term: Term, _args: Args) => {
|
||||||
const descriptions: string[] = []
|
const descriptions: string[] = []
|
||||||
Object.values(subcommand).forEach(v => descriptions.push(v.description))
|
Object.values(this.data).forEach(sub_cmd => descriptions.push(sub_cmd.description))
|
||||||
term.appendChild(stdout_grid(Object.keys(subcommand), descriptions))
|
|
||||||
}
|
|
||||||
subcommand.help.description = help_description ? help_description : "Show the help page"
|
|
||||||
|
|
||||||
return subcommand
|
term.appendChild(stdout(description))
|
||||||
|
term.appendChild(stdout_grid(Object.keys(this.data), descriptions))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public process(term: Term, args: Args) {
|
||||||
|
const subc = args[1]
|
||||||
|
if (subc) {
|
||||||
|
const subc_f = this.data[subc]
|
||||||
|
if (subc_f) {
|
||||||
|
subc_f.inner(term, args)
|
||||||
|
} else {
|
||||||
|
term.appendChild(SubCommand.unknown(subc))
|
||||||
|
this.data.help.inner(term, args)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.data.help.inner(term, args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public add(name: string, description: string, f: SubCommandClosure) {
|
||||||
|
this.data[name] = {} as SubCommandAction
|
||||||
|
this.data[name].description = description
|
||||||
|
this.data[name].inner = f
|
||||||
|
}
|
||||||
|
|
||||||
|
public static unknown(subcmd_name: string) {
|
||||||
|
const subcmd_unknown = stdout("Unknown sub-command: ")
|
||||||
|
subcmd_unknown.appendChild(bold(subcmd_name))
|
||||||
|
return subcmd_unknown
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default SubCommand
|
||||||
|
|
||||||
export {
|
export {
|
||||||
type SubCommand,
|
type SubCommand,
|
||||||
type SubCommandAction
|
type SubCommandAction
|
||||||
|
Reference in New Issue
Block a user