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