Add help section for commands

This commit is contained in:
Simon Ser 2021-03-08 17:25:00 +01:00
parent 17a2d48b2e
commit 78f22fce4e
3 changed files with 151 additions and 78 deletions

View file

@ -9,96 +9,147 @@ function getActiveClient(app) {
} }
export default { export default {
"buffer": (app, args) => { "buffer": {
var name = args[0]; usage: "<name>",
for (var buf of app.state.buffers.values()) { description: "Switch to a buffer",
if (buf.name === name) { execute: (app, args) => {
app.switchBuffer(buf); var name = args[0];
return; 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) => { "close": {
var activeBuffer = app.state.buffers.get(app.state.activeBuffer); description: "Close the current buffer",
if (!activeBuffer || activeBuffer.type == BufferType.SERVER) { execute: (app, args) => {
throw new Error("Not in a user or channel buffer"); var activeBuffer = app.state.buffers.get(app.state.activeBuffer);
} if (!activeBuffer || activeBuffer.type == BufferType.SERVER) {
app.close(activeBuffer.id); throw new Error("Not in a user or channel buffer");
}
app.close(activeBuffer.id);
},
}, },
"disconnect": (app, args) => { "disconnect": {
app.disconnect(); description: "Disconnect from the server",
execute: (app, args) => {
app.disconnect();
},
}, },
"help": (app, args) => { "help": {
app.openHelp(); description: "Show help menu",
execute: (app, args) => {
app.openHelp();
},
}, },
"join": (app, args) => { "join": {
var channel = args[0]; usage: "<name>",
if (!channel) { description: "Join a channel",
throw new Error("Missing channel name"); execute: (app, args) => {
} var channel = args[0];
getActiveClient(app).send({ command: "JOIN", params: [channel] }); if (!channel) {
throw new Error("Missing channel name");
}
getActiveClient(app).send({ command: "JOIN", params: [channel] });
},
}, },
"me": (app, args) => { "me": {
var action = args.join(" "); usage: "<action>",
var activeBuffer = app.state.buffers.get(app.state.activeBuffer); description: "Send an action message to the current buffer",
if (!activeBuffer) { execute: (app, args) => {
throw new Error("Not in a buffer"); var action = args.join(" ");
} var activeBuffer = app.state.buffers.get(app.state.activeBuffer);
var text = `\x01ACTION ${action}\x01`; if (!activeBuffer) {
app.privmsg(activeBuffer.name, text); throw new Error("Not in a buffer");
}
var text = `\x01ACTION ${action}\x01`;
app.privmsg(activeBuffer.name, text);
},
}, },
"msg": (app, args) => { "msg": {
var target = args[0]; usage: "<target> <message>",
var text = args.slice(1).join(" "); description: "Send a message to a nickname or a channel",
getActiveClient(app).send({ command: "PRIVMSG", params: [target, text] }); execute: (app, args) => {
var target = args[0];
var text = args.slice(1).join(" ");
getActiveClient(app).send({ command: "PRIVMSG", params: [target, text] });
},
}, },
"nick": (app, args) => { "nick": {
var newNick = args[0]; usage: "<nick>",
getActiveClient(app).send({ command: "NICK", params: [newNick] }); description: "Change current nickname",
execute: (app, args) => {
var newNick = args[0];
getActiveClient(app).send({ command: "NICK", params: [newNick] });
},
}, },
"notice": (app, args) => { "notice": {
var target = args[0]; usage: "<target> <message>",
var text = args.slice(1).join(" "); description: "Send a notice to a nickname or a channel",
getActiveClient(app).send({ command: "NOTICE", params: [target, text] }); execute: (app, args) => {
var target = args[0];
var text = args.slice(1).join(" ");
getActiveClient(app).send({ command: "NOTICE", params: [target, text] });
},
}, },
"part": (app, args) => { "part": {
var reason = args.join(" "); usage: "[reason]",
var activeBuffer = app.state.buffers.get(app.state.activeBuffer); description: "Leave a channel",
if (!activeBuffer || !app.isChannel(activeBuffer.name)) { execute: (app, args) => {
throw new Error("Not in a channel"); var reason = args.join(" ");
} var activeBuffer = app.state.buffers.get(app.state.activeBuffer);
var params = [activeBuffer.name]; if (!activeBuffer || !app.isChannel(activeBuffer.name)) {
if (reason) { throw new Error("Not in a channel");
params.push(reason); }
} var params = [activeBuffer.name];
getActiveClient(app).send({ command: "PART", params }); if (reason) {
params.push(reason);
}
getActiveClient(app).send({ command: "PART", params });
},
}, },
"query": (app, args) => { "query": {
var nick = args[0]; usage: "<nick>",
if (!nick) { description: "Open a buffer to send messages to a nickname",
throw new Error("Missing nickname"); execute: (app, args) => {
} var nick = args[0];
app.open(nick); if (!nick) {
throw new Error("Missing nickname");
}
app.open(nick);
},
}, },
"quit": (app, args) => { "quit": {
if (window.localStorage) { description: "Quit",
localStorage.removeItem("autoconnect"); execute: (app, args) => {
} if (window.localStorage) {
app.close({ name: SERVER_BUFFER }); localStorage.removeItem("autoconnect");
}
app.close({ name: SERVER_BUFFER });
},
}, },
"reconnect": (app, args) => { "reconnect": {
app.reconnect(); description: "Reconnect to the server",
execute: (app, args) => {
app.reconnect();
},
}, },
"topic": (app, args) => { "topic": {
var activeBuffer = app.state.buffers.get(app.state.activeBuffer); usage: "<topic>",
if (!activeBuffer || !app.isChannel(activeBuffer.name)) { description: "Change the topic of the current channel",
throw new Error("Not in a channel"); execute: (app, args) => {
} var activeBuffer = app.state.buffers.get(app.state.activeBuffer);
var params = [activeBuffer.name]; if (!activeBuffer || !app.isChannel(activeBuffer.name)) {
if (args.length > 0) { throw new Error("Not in a channel");
params.push(args.join(" ")); }
} var params = [activeBuffer.name];
getActiveClient(app).send({ command: "TOPIC", params }); if (args.length > 0) {
params.push(args.join(" "));
}
getActiveClient(app).send({ command: "TOPIC", params });
},
}, },
}; };

View file

@ -789,7 +789,7 @@ export default class App extends Component {
} }
try { try {
cmd(this, args); cmd.execute(this, args);
} catch (error) { } catch (error) {
console.error("Failed to execute command '" + name + "'", error); console.error("Failed to execute command '" + name + "'", error);
this.setState({ error }); this.setState({ error });

View file

@ -1,5 +1,6 @@
import { html, Component } from "../lib/index.js"; import { html, Component } from "../lib/index.js";
import { keybindings } from "../keybindings.js"; import { keybindings } from "../keybindings.js";
import commands from "../commands.js";
function KeyBindingsHelp() { function KeyBindingsHelp() {
var l = keybindings.map((binding) => { var l = keybindings.map((binding) => {
@ -28,9 +29,30 @@ function KeyBindingsHelp() {
return html`<dl>${l}</dl>`; return html`<dl>${l}</dl>`;
} }
function CommandsHelp() {
var l = Object.keys(commands).map((name) => {
var cmd = commands[name];
var usage = "/" + name;
if (cmd.usage) {
usage += " " + cmd.usage;
}
return html`
<dt><strong><code>${usage}</code></strong></dt>
<dd>${cmd.description}</dd>
`;
});
return html`<dl>${l}</dl>`;
}
export default function Help() { export default function Help() {
return html` return html`
<h3>Key bindings</h3> <h3>Key bindings</h3>
<${KeyBindingsHelp}/> <${KeyBindingsHelp}/>
<h3>Commands</h3>
<${CommandsHelp}/>
`; `;
} }