-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the Command system with a simple help command.
Progress towards #327
- Loading branch information
1 parent
36be82b
commit e084709
Showing
5 changed files
with
110 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const std = @import("std"); | ||
|
||
const main = @import("root"); | ||
const User = main.server.User; | ||
|
||
pub const Command = struct { | ||
exec: *const fn(args: []const u8, source: *User) void, | ||
name: []const u8, | ||
description: []const u8, | ||
usage: []const u8, | ||
}; | ||
|
||
pub var commands: std.StringHashMap(Command) = undefined; | ||
|
||
pub fn init() void { | ||
commands = std.StringHashMap(Command).init(main.globalAllocator.allocator); | ||
const commandList = @import("_list.zig"); | ||
inline for(@typeInfo(commandList).Struct.decls) |decl| { | ||
commands.put(decl.name, .{ | ||
.name = decl.name, | ||
.description = @field(commandList, decl.name).description, | ||
.usage = @field(commandList, decl.name).usage, | ||
.exec = &@field(commandList, decl.name).execute, | ||
}) catch unreachable; | ||
} | ||
} | ||
|
||
pub fn deinit() void { | ||
commands.deinit(); | ||
} | ||
|
||
pub fn execute(msg: []const u8, source: *User) void { | ||
const end = std.mem.indexOfScalar(u8, msg, ' ') orelse msg.len; | ||
const command = msg[0..end]; | ||
if(commands.get(command)) |cmd| { | ||
cmd.exec(msg[@min(end + 1, msg.len)..], source); | ||
} else { | ||
const result = std.fmt.allocPrint(main.stackAllocator.allocator, "#ff0000Unrecognized Command \"{s}\"", .{command}) catch unreachable; | ||
defer main.stackAllocator.free(result); | ||
source.sendMessage(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
|
||
pub const help = @import("help.zig"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const std = @import("std"); | ||
|
||
const main = @import("root"); | ||
const User = main.server.User; | ||
|
||
const command = @import("_command.zig"); | ||
|
||
pub const description = "Shows info about all the commands."; | ||
pub const usage = "/help\n/help <command>"; | ||
|
||
pub fn execute(args: []const u8, source: *User) void { | ||
var msg = main.List(u8).init(main.stackAllocator); | ||
defer msg.deinit(); | ||
msg.appendSlice("#ffff00"); | ||
if(args.len == 0) { | ||
var iterator = command.commands.valueIterator(); | ||
while(iterator.next()) |cmd| { | ||
msg.appendSlice(cmd.name); | ||
msg.appendSlice(": "); | ||
msg.appendSlice(cmd.description); | ||
msg.append('\n'); | ||
} | ||
} else { | ||
var split = std.mem.splitScalar(u8, args, ' '); | ||
while(split.next()) |arg| { | ||
if(command.commands.get(arg)) |cmd| { | ||
msg.appendSlice(cmd.name); | ||
msg.appendSlice(": "); | ||
msg.appendSlice(cmd.description); | ||
msg.append('\n'); | ||
msg.appendSlice(cmd.usage); | ||
msg.append('\n'); | ||
} else { | ||
msg.appendSlice("#ff0000Unrecognized Command \""); | ||
msg.appendSlice(arg); | ||
msg.appendSlice("\"#ffff00\n"); | ||
} | ||
} | ||
} | ||
if(msg.items[msg.items.len - 1] == '\n') _ = msg.pop(); | ||
source.sendMessage(msg.items); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters