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 { bold } from "../../color";
|
||||
|
||||
import stdout, { stdout_horizontal } from "../../../elements/stdout";
|
||||
import SubCommand, { type SubCommandAction } from "../subcommand";
|
||||
import stdout from "../../../elements/stdout";
|
||||
import SubCommand from "../subcommand";
|
||||
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_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
|
||||
history.file.inner = []
|
||||
term.appendChild(stdout(`Cleared ${entries} entries from the history.`))
|
||||
})
|
||||
|
||||
export default function history_cmd(term: Term, args: Args) {
|
||||
const subc = args[1]
|
||||
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)
|
||||
}
|
||||
}
|
||||
history_command.process(term, args)
|
||||
return true
|
||||
}
|
@ -2,8 +2,8 @@ import { bold } from "../color";
|
||||
import { to_args, trim } from "./parse";
|
||||
|
||||
import commands, { type Command } from "./list";
|
||||
import create from "../../elements/create";
|
||||
import history from "../history";
|
||||
import stdout from "../../elements/stdout";
|
||||
|
||||
type Term = HTMLElement
|
||||
|
||||
@ -18,8 +18,7 @@ function valid_command(term: Term, args: string[]) {
|
||||
}
|
||||
|
||||
function unknown_command(cmd_name: string) {
|
||||
const unknown_element = create("p")
|
||||
unknown_element.innerText = "shell: Unknown command: "
|
||||
const unknown_element = stdout("shell: Unknown command: ")
|
||||
unknown_element.appendChild(bold(cmd_name))
|
||||
return unknown_element
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
import { get_working_dir_name_full, set_working_dir, SetDirStatus } from "../fs/fn"
|
||||
|
||||
import create from "../../elements/create"
|
||||
import history_cmd from "./history"
|
||||
import history_cmd from "./builtin/history"
|
||||
import stdout from "../../elements/stdout"
|
||||
|
||||
type Term = HTMLElement
|
||||
|
@ -1,27 +1,62 @@
|
||||
import { stdout_grid } from "../../elements/stdout";
|
||||
import type { Term } from "./list";
|
||||
import stdout, { stdout_grid } from "../../elements/stdout";
|
||||
import { bold } from "../color";
|
||||
import type { Args, Term } from "./list";
|
||||
|
||||
type SubCommandClosure = (term: Term, args: Args) => void
|
||||
interface SubCommandAction {
|
||||
inner: (term: Term) => void,
|
||||
description: string
|
||||
inner: SubCommandClosure,
|
||||
description: string,
|
||||
}
|
||||
interface SubCommand {
|
||||
[index: string]: SubCommandAction
|
||||
interface SubCommands {
|
||||
[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))
|
||||
const SubCommand = class {
|
||||
public data: SubCommands //data? less goo!
|
||||
|
||||
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[] = []
|
||||
Object.values(this.data).forEach(sub_cmd => descriptions.push(sub_cmd.description))
|
||||
|
||||
term.appendChild(stdout(description))
|
||||
term.appendChild(stdout_grid(Object.keys(this.data), descriptions))
|
||||
}
|
||||
}
|
||||
subcommand.help.description = help_description ? help_description : "Show the help page"
|
||||
|
||||
return subcommand
|
||||
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 {
|
||||
type SubCommand,
|
||||
type SubCommandAction
|
||||
|
Reference in New Issue
Block a user