tweak help and commands

This commit is contained in:
Chris Wanstrath 2025-10-07 19:02:01 -07:00
parent 415cbd0ccd
commit ebfddbb278
3 changed files with 16 additions and 25 deletions

View File

@ -1,23 +1,19 @@
/// <reference lib="dom" /> // Print all the commands, or a subset.
// Print all the commands.
import { commands } from "@/js/commands" import { commands } from "@/commands"
export default function (prefix?: string) { export default async function (prefix?: string) {
let cmds = prefix ? matchingCommands(prefix) : commands let cmds = prefix ? await matchingCommands(prefix) : Object.keys(await commands())
return { html: "<div>" + Object.keys(cmds).map(cmd => `<a href="#help ${cmd}">${cmd}</a>`).join("") + "</div>" } return { html: "<div>" + cmds.map(cmd => `<a href="#help ${cmd}">${cmd}</a>`).join("") + "</div>" }
} }
function matchingCommands(cmd: string): string { async function matchingCommands(cmd: string): Promise<string[]> {
let matched: string[] = [] let matched: string[] = []
console.log(Object.keys(commands))
for (const command of Object.keys(commands)) { for (const command of Object.keys(await commands()))
console.log("cmd->", command)
if (command.startsWith(cmd)) matched.push(command) if (command.startsWith(cmd)) matched.push(command)
}
return matched
return matched.join(" ")
} }

View File

@ -2,13 +2,15 @@
// //
// (Hopefully.) // (Hopefully.)
import { commandPath, commands } from "@/commands" import { commandPath } from "@/commands"
import type { CommandOutput } from "@/shared/types"
import commands from "./commands"
export default async function (cmd: string): Promise<string> { export default async function (cmd: string): Promise<CommandOutput> {
if (!cmd) return "usage: help <command>" if (!cmd) return "usage: help <command>"
const path = commandPath(cmd) const path = commandPath(cmd)
if (!path) { return matchingCommands(cmd) } if (!path) return await commands(cmd)
const code = (await Bun.file(path).text()).split("\n") const code = (await Bun.file(path).text()).split("\n")
let docs = [] let docs = []
@ -26,12 +28,3 @@ export default async function (cmd: string): Promise<string> {
return docs.join("\n") return docs.join("\n")
} }
async function matchingCommands(cmd: string): Promise<string> {
let matched: string[] = []
for (const command of Object.keys(await commands())) {
if (command.startsWith(cmd)) matched.push(command)
}
return matched.join(" ")
}

View File

@ -1,3 +1,5 @@
// Print information about the current session.
import { sessionGet } from "@/session" import { sessionGet } from "@/session"
import { highlightToHTML } from "../lib/highlight" import { highlightToHTML } from "../lib/highlight"