command: Use std.ComptimeStringMap to get impl. function from command string

This commit is contained in:
Leon Henrik Plickat 2021-08-12 02:07:41 +02:00 committed by Isaac Freund
parent 81d103d425
commit ab55ab8fc2

View file

@ -36,59 +36,58 @@ pub const Orientation = enum {
vertical, vertical,
}; };
// TODO: this could be replaced with a comptime hashmap
// zig fmt: off // zig fmt: off
const str_to_impl_fn = [_]struct { const command_impls = std.ComptimeStringMap(
name: []const u8, fn (*std.mem.Allocator, *Seat, []const [:0]const u8, *?[]const u8) Error!void,
impl: fn (*std.mem.Allocator, *Seat, []const [:0]const u8, *?[]const u8) Error!void, .{
}{ .{ "attach-mode", @import("command/attach_mode.zig").attachMode },
.{ .name = "attach-mode", .impl = @import("command/attach_mode.zig").attachMode }, .{ "background-color", @import("command/config.zig").backgroundColor },
.{ .name = "background-color", .impl = @import("command/config.zig").backgroundColor }, .{ "border-color-focused", @import("command/config.zig").borderColorFocused },
.{ .name = "border-color-focused", .impl = @import("command/config.zig").borderColorFocused }, .{ "border-color-unfocused", @import("command/config.zig").borderColorUnfocused },
.{ .name = "border-color-unfocused", .impl = @import("command/config.zig").borderColorUnfocused }, .{ "border-color-urgent", @import("command/config.zig").borderColorUrgent },
.{ .name = "border-color-urgent", .impl = @import("command/config.zig").borderColorUrgent }, .{ "border-width", @import("command/config.zig").borderWidth },
.{ .name = "border-width", .impl = @import("command/config.zig").borderWidth }, .{ "close", @import("command/close.zig").close },
.{ .name = "close", .impl = @import("command/close.zig").close }, .{ "csd-filter-add", @import("command/filter.zig").csdFilterAdd },
.{ .name = "csd-filter-add", .impl = @import("command/filter.zig").csdFilterAdd }, .{ "csd-filter-remove", @import("command/filter.zig").csdFilterRemove },
.{ .name = "csd-filter-remove", .impl = @import("command/filter.zig").csdFilterRemove }, .{ "declare-mode", @import("command/declare_mode.zig").declareMode },
.{ .name = "declare-mode", .impl = @import("command/declare_mode.zig").declareMode }, .{ "default-layout", @import("command/layout.zig").defaultLayout },
.{ .name = "default-layout", .impl = @import("command/layout.zig").defaultLayout }, .{ "enter-mode", @import("command/enter_mode.zig").enterMode },
.{ .name = "enter-mode", .impl = @import("command/enter_mode.zig").enterMode }, .{ "exit", @import("command/exit.zig").exit },
.{ .name = "exit", .impl = @import("command/exit.zig").exit }, .{ "float-filter-add", @import("command/filter.zig").floatFilterAdd },
.{ .name = "float-filter-add", .impl = @import("command/filter.zig").floatFilterAdd }, .{ "float-filter-remove", @import("command/filter.zig").floatFilterRemove },
.{ .name = "float-filter-remove", .impl = @import("command/filter.zig").floatFilterRemove }, .{ "focus-follows-cursor", @import("command/focus_follows_cursor.zig").focusFollowsCursor },
.{ .name = "focus-follows-cursor", .impl = @import("command/focus_follows_cursor.zig").focusFollowsCursor }, .{ "focus-output", @import("command/output.zig").focusOutput },
.{ .name = "focus-output", .impl = @import("command/output.zig").focusOutput }, .{ "focus-previous-tags", @import("command/tags.zig").focusPreviousTags },
.{ .name = "focus-view", .impl = @import("command/focus_view.zig").focusView }, .{ "focus-view", @import("command/focus_view.zig").focusView },
.{ .name = "input", .impl = @import("command/input.zig").input }, .{ "input", @import("command/input.zig").input },
.{ .name = "list-input-configs", .impl = @import("command/input.zig").listInputConfigs}, .{ "list-input-configs", @import("command/input.zig").listInputConfigs},
.{ .name = "list-inputs", .impl = @import("command/input.zig").listInputs }, .{ "list-inputs", @import("command/input.zig").listInputs },
.{ .name = "map", .impl = @import("command/map.zig").map }, .{ "map", @import("command/map.zig").map },
.{ .name = "map-pointer", .impl = @import("command/map.zig").mapPointer }, .{ "map-pointer", @import("command/map.zig").mapPointer },
.{ .name = "move", .impl = @import("command/move.zig").move }, .{ "move", @import("command/move.zig").move },
.{ .name = "output-layout", .impl = @import("command/layout.zig").outputLayout }, .{ "output-layout", @import("command/layout.zig").outputLayout },
.{ .name = "resize", .impl = @import("command/move.zig").resize }, .{ "resize", @import("command/move.zig").resize },
.{ .name = "send-layout-cmd", .impl = @import("command/layout.zig").sendLayoutCmd }, .{ "send-layout-cmd", @import("command/layout.zig").sendLayoutCmd },
.{ .name = "send-to-output", .impl = @import("command/output.zig").sendToOutput }, .{ "send-to-output", @import("command/output.zig").sendToOutput },
.{ .name = "set-cursor-warp", .impl = @import("command/config.zig").setCursorWarp }, .{ "send-to-previous-tags", @import("command/tags.zig").sendToPreviousTags },
.{ .name = "set-focused-tags", .impl = @import("command/tags.zig").setFocusedTags }, .{ "set-cursor-warp", @import("command/config.zig").setCursorWarp },
.{ .name = "set-repeat", .impl = @import("command/set_repeat.zig").setRepeat }, .{ "set-focused-tags", @import("command/tags.zig").setFocusedTags },
.{ .name = "set-view-tags", .impl = @import("command/tags.zig").setViewTags }, .{ "set-repeat", @import("command/set_repeat.zig").setRepeat },
.{ .name = "snap", .impl = @import("command/move.zig").snap }, .{ "set-view-tags", @import("command/tags.zig").setViewTags },
.{ .name = "spawn", .impl = @import("command/spawn.zig").spawn }, .{ "snap", @import("command/move.zig").snap },
.{ .name = "spawn-tagmask", .impl = @import("command/tags.zig").spawnTagmask }, .{ "spawn", @import("command/spawn.zig").spawn },
.{ .name = "swap", .impl = @import("command/swap.zig").swap}, .{ "spawn-tagmask", @import("command/tags.zig").spawnTagmask },
.{ .name = "toggle-float", .impl = @import("command/toggle_float.zig").toggleFloat }, .{ "swap", @import("command/swap.zig").swap},
.{ .name = "toggle-focused-tags", .impl = @import("command/tags.zig").toggleFocusedTags }, .{ "toggle-float", @import("command/toggle_float.zig").toggleFloat },
.{ .name = "toggle-fullscreen", .impl = @import("command/toggle_fullscreen.zig").toggleFullscreen }, .{ "toggle-focused-tags", @import("command/tags.zig").toggleFocusedTags },
.{ .name = "toggle-view-tags", .impl = @import("command/tags.zig").toggleViewTags }, .{ "toggle-fullscreen", @import("command/toggle_fullscreen.zig").toggleFullscreen },
.{ .name = "focus-previous-tags", .impl = @import("command/tags.zig").focusPreviousTags }, .{ "toggle-view-tags", @import("command/tags.zig").toggleViewTags },
.{ .name = "send-to-previous-tags", .impl = @import("command/tags.zig").sendToPreviousTags }, .{ "unmap", @import("command/map.zig").unmap },
.{ .name = "unmap", .impl = @import("command/map.zig").unmap }, .{ "unmap-pointer", @import("command/map.zig").unmapPointer },
.{ .name = "unmap-pointer", .impl = @import("command/map.zig").unmapPointer }, .{ "xcursor-theme", @import("command/xcursor_theme.zig").xcursorTheme },
.{ .name = "xcursor-theme", .impl = @import("command/xcursor_theme.zig").xcursorTheme }, .{ "zoom", @import("command/zoom.zig").zoom },
.{ .name = "zoom", .impl = @import("command/zoom.zig").zoom }, },
}; );
// zig fmt: on // zig fmt: on
pub const Error = error{ pub const Error = error{
@ -125,11 +124,7 @@ pub fn run(
) Error!void { ) Error!void {
std.debug.assert(out.* == null); std.debug.assert(out.* == null);
if (args.len == 0) return Error.NoCommand; if (args.len == 0) return Error.NoCommand;
const impl_fn = command_impls.get(args[0]) orelse return Error.UnknownCommand;
const impl_fn = for (str_to_impl_fn) |definition| {
if (std.mem.eql(u8, args[0], definition.name)) break definition.impl;
} else return Error.UnknownCommand;
try impl_fn(allocator, seat, args, out); try impl_fn(allocator, seat, args, out);
} }