diff --git a/commands.js b/commands.js index e9063e3..b9309da 100644 --- a/commands.js +++ b/commands.js @@ -9,96 +9,147 @@ function getActiveClient(app) { } export default { - "buffer": (app, args) => { - var name = args[0]; - for (var buf of app.state.buffers.values()) { - if (buf.name === name) { - app.switchBuffer(buf); - return; + "buffer": { + usage: "", + description: "Switch to a buffer", + execute: (app, args) => { + var name = args[0]; + for (var buf of app.state.buffers.values()) { + if (buf.name === name) { + app.switchBuffer(buf); + return; + } } - } - throw new Error("Unknown buffer"); + throw new Error("Unknown buffer"); + }, }, - "close": (app, args) => { - var activeBuffer = app.state.buffers.get(app.state.activeBuffer); - if (!activeBuffer || activeBuffer.type == BufferType.SERVER) { - throw new Error("Not in a user or channel buffer"); - } - app.close(activeBuffer.id); + "close": { + description: "Close the current buffer", + execute: (app, args) => { + var activeBuffer = app.state.buffers.get(app.state.activeBuffer); + if (!activeBuffer || activeBuffer.type == BufferType.SERVER) { + throw new Error("Not in a user or channel buffer"); + } + app.close(activeBuffer.id); + }, }, - "disconnect": (app, args) => { - app.disconnect(); + "disconnect": { + description: "Disconnect from the server", + execute: (app, args) => { + app.disconnect(); + }, }, - "help": (app, args) => { - app.openHelp(); + "help": { + description: "Show help menu", + execute: (app, args) => { + app.openHelp(); + }, }, - "join": (app, args) => { - var channel = args[0]; - if (!channel) { - throw new Error("Missing channel name"); - } - getActiveClient(app).send({ command: "JOIN", params: [channel] }); + "join": { + usage: "", + description: "Join a channel", + execute: (app, args) => { + var channel = args[0]; + if (!channel) { + throw new Error("Missing channel name"); + } + getActiveClient(app).send({ command: "JOIN", params: [channel] }); + }, }, - "me": (app, args) => { - var action = args.join(" "); - var activeBuffer = app.state.buffers.get(app.state.activeBuffer); - if (!activeBuffer) { - throw new Error("Not in a buffer"); - } - var text = `\x01ACTION ${action}\x01`; - app.privmsg(activeBuffer.name, text); + "me": { + usage: "", + description: "Send an action message to the current buffer", + execute: (app, args) => { + var action = args.join(" "); + var activeBuffer = app.state.buffers.get(app.state.activeBuffer); + if (!activeBuffer) { + throw new Error("Not in a buffer"); + } + var text = `\x01ACTION ${action}\x01`; + app.privmsg(activeBuffer.name, text); + }, }, - "msg": (app, args) => { - var target = args[0]; - var text = args.slice(1).join(" "); - getActiveClient(app).send({ command: "PRIVMSG", params: [target, text] }); + "msg": { + usage: " ", + description: "Send a message to a nickname or a channel", + execute: (app, args) => { + var target = args[0]; + var text = args.slice(1).join(" "); + getActiveClient(app).send({ command: "PRIVMSG", params: [target, text] }); + }, }, - "nick": (app, args) => { - var newNick = args[0]; - getActiveClient(app).send({ command: "NICK", params: [newNick] }); + "nick": { + usage: "", + description: "Change current nickname", + execute: (app, args) => { + var newNick = args[0]; + getActiveClient(app).send({ command: "NICK", params: [newNick] }); + }, }, - "notice": (app, args) => { - var target = args[0]; - var text = args.slice(1).join(" "); - getActiveClient(app).send({ command: "NOTICE", params: [target, text] }); + "notice": { + usage: " ", + description: "Send a notice to a nickname or a channel", + execute: (app, args) => { + var target = args[0]; + var text = args.slice(1).join(" "); + getActiveClient(app).send({ command: "NOTICE", params: [target, text] }); + }, }, - "part": (app, args) => { - var reason = args.join(" "); - var activeBuffer = app.state.buffers.get(app.state.activeBuffer); - if (!activeBuffer || !app.isChannel(activeBuffer.name)) { - throw new Error("Not in a channel"); - } - var params = [activeBuffer.name]; - if (reason) { - params.push(reason); - } - getActiveClient(app).send({ command: "PART", params }); + "part": { + usage: "[reason]", + description: "Leave a channel", + execute: (app, args) => { + var reason = args.join(" "); + var activeBuffer = app.state.buffers.get(app.state.activeBuffer); + if (!activeBuffer || !app.isChannel(activeBuffer.name)) { + throw new Error("Not in a channel"); + } + var params = [activeBuffer.name]; + if (reason) { + params.push(reason); + } + getActiveClient(app).send({ command: "PART", params }); + }, }, - "query": (app, args) => { - var nick = args[0]; - if (!nick) { - throw new Error("Missing nickname"); - } - app.open(nick); + "query": { + usage: "", + description: "Open a buffer to send messages to a nickname", + execute: (app, args) => { + var nick = args[0]; + if (!nick) { + throw new Error("Missing nickname"); + } + app.open(nick); + }, }, - "quit": (app, args) => { - if (window.localStorage) { - localStorage.removeItem("autoconnect"); - } - app.close({ name: SERVER_BUFFER }); + "quit": { + description: "Quit", + execute: (app, args) => { + if (window.localStorage) { + localStorage.removeItem("autoconnect"); + } + app.close({ name: SERVER_BUFFER }); + }, }, - "reconnect": (app, args) => { - app.reconnect(); + "reconnect": { + description: "Reconnect to the server", + execute: (app, args) => { + app.reconnect(); + }, }, - "topic": (app, args) => { - var activeBuffer = app.state.buffers.get(app.state.activeBuffer); - if (!activeBuffer || !app.isChannel(activeBuffer.name)) { - throw new Error("Not in a channel"); - } - var params = [activeBuffer.name]; - if (args.length > 0) { - params.push(args.join(" ")); - } - getActiveClient(app).send({ command: "TOPIC", params }); + "topic": { + usage: "", + description: "Change the topic of the current channel", + execute: (app, args) => { + var activeBuffer = app.state.buffers.get(app.state.activeBuffer); + if (!activeBuffer || !app.isChannel(activeBuffer.name)) { + throw new Error("Not in a channel"); + } + var params = [activeBuffer.name]; + if (args.length > 0) { + params.push(args.join(" ")); + } + getActiveClient(app).send({ command: "TOPIC", params }); + }, }, }; diff --git a/components/app.js b/components/app.js index d3856fe..c0280a8 100644 --- a/components/app.js +++ b/components/app.js @@ -789,7 +789,7 @@ export default class App extends Component { } try { - cmd(this, args); + cmd.execute(this, args); } catch (error) { console.error("Failed to execute command '" + name + "'", error); this.setState({ error }); diff --git a/components/help.js b/components/help.js index 4820547..7ccf822 100644 --- a/components/help.js +++ b/components/help.js @@ -1,5 +1,6 @@ import { html, Component } from "../lib/index.js"; import { keybindings } from "../keybindings.js"; +import commands from "../commands.js"; function KeyBindingsHelp() { var l = keybindings.map((binding) => { @@ -28,9 +29,30 @@ function KeyBindingsHelp() { return html`
${l}
`; } +function CommandsHelp() { + var l = Object.keys(commands).map((name) => { + var cmd = commands[name]; + + var usage = "/" + name; + if (cmd.usage) { + usage += " " + cmd.usage; + } + + return html` +
${usage}
+
${cmd.description}
+ `; + }); + + return html`
${l}
`; +} + export default function Help() { return html`

Key bindings

<${KeyBindingsHelp}/> + +

Commands

+ <${CommandsHelp}/> `; }