Compare commits
3 Commits
e468155bb1
...
history
Author | SHA1 | Date | |
---|---|---|---|
c5692b1b7f | |||
b5f279691c | |||
4663aca074 |
22
src/components/client/shell/command/builtin/history.ts
Normal file
22
src/components/client/shell/command/builtin/history.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import type { Args, Term } from "../list";
|
||||||
|
|
||||||
|
import stdout from "../../../elements/stdout";
|
||||||
|
import SubCommand from "../subcommand";
|
||||||
|
import history from "../../history";
|
||||||
|
|
||||||
|
const history_command = new SubCommand("Show and manipulate command history")
|
||||||
|
|
||||||
|
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 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) {
|
||||||
|
history_command.process(term, args)
|
||||||
|
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,24 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -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) {
|
||||||
const descriptions: string[] = []
|
this.data = {}
|
||||||
Object.values(subcommand).forEach(v => descriptions.push(v.description))
|
this.data.help = {} as SubCommandAction
|
||||||
term.appendChild(stdout_grid(Object.keys(subcommand), descriptions))
|
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 {
|
export {
|
||||||
type SubCommand,
|
type SubCommand,
|
||||||
type SubCommandAction
|
type SubCommandAction
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
{
|
{
|
||||||
"extends": "astro/tsconfigs/strict",
|
"extends": "astro/tsconfigs/strict",
|
||||||
"include": [".astro/types.d.ts", "**/*"],
|
"include": [".astro/types.d.ts", "**/*"],
|
||||||
"exclude": ["dist"],
|
"exclude": ["dist"],
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"noImplicitAny": true
|
"noImplicitAny": true,
|
||||||
}
|
"noUnusedLocals": true,
|
||||||
|
"allowUnusedLabels": false
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user