git.lucas.co / cce-compositor
Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git

commitaae5652456e111c417e4fc8ac7c36ddebbebba8c
parentfcdebdcd6b
authorIsaac Freund <[email protected]>
date2020-07-15 13:02
code: simplify direction parsing

 river/command.zig                | 13 ++-----------
 river/command/focus_output.zig   |  6 +++---
 river/command/focus_view.zig     | 10 +++++-----
 river/command/send_to_output.zig |  6 +++---
 4 files changed, 13 insertions(+), 22 deletions(-)

diff --git a/river/command.zig b/river/command.zig
index 00be119..d8e33ae 100644
--- a/river/command.zig
+++ b/river/command.zig
@@ -20,17 +20,8 @@ const std = @import("std");
 const Seat = @import("Seat.zig");
 
 pub const Direction = enum {
-    Next,
-    Prev,
-
-    pub fn parse(str: []const u8) error{InvalidDirection}!Direction {
-        return if (std.mem.eql(u8, str, "next"))
-            Direction.Next
-        else if (std.mem.eql(u8, str, "previous"))
-            Direction.Prev
-        else
-            error.InvalidDirection;
-    }
+    next,
+    previous,
 };
 
 // TODO: this could be replaced with a comptime hashmap
diff --git a/river/command/focus_output.zig b/river/command/focus_output.zig
index 23a68e9..b2520ec 100644
--- a/river/command/focus_output.zig
+++ b/river/command/focus_output.zig
@@ -33,7 +33,7 @@ pub fn focusOutput(
     if (args.len < 2) return Error.NotEnoughArguments;
     if (args.len > 2) return Error.TooManyArguments;
 
-    const direction = try Direction.parse(args[1]);
+    const direction = std.meta.stringToEnum(Direction, args[1]) orelse return Error.InvalidDirection;
     const root = &seat.input_manager.server.root;
 
     // If the noop output is focused, there are no other outputs to switch to
@@ -45,8 +45,8 @@ pub fn focusOutput(
     // Focus the next/prev output in the list if there is one, else wrap
     const focused_node = @fieldParentPtr(std.TailQueue(Output).Node, "data", seat.focused_output);
     seat.focusOutput(switch (direction) {
-        .Next => if (focused_node.next) |node| &node.data else &root.outputs.first.?.data,
-        .Prev => if (focused_node.prev) |node| &node.data else &root.outputs.last.?.data,
+        .next => if (focused_node.next) |node| &node.data else &root.outputs.first.?.data,
+        .previous => if (focused_node.prev) |node| &node.data else &root.outputs.last.?.data,
     });
 
     seat.focus(null);
diff --git a/river/command/focus_view.zig b/river/command/focus_view.zig
index 2c66045..44fd214 100644
--- a/river/command/focus_view.zig
+++ b/river/command/focus_view.zig
@@ -34,7 +34,7 @@ pub fn focusView(
     if (args.len < 2) return Error.NotEnoughArguments;
     if (args.len > 2) return Error.TooManyArguments;
 
-    const direction = try Direction.parse(args[1]);
+    const direction = std.meta.stringToEnum(Direction, args[1]) orelse return Error.InvalidDirection;
     const output = seat.focused_output;
 
     if (seat.focused_view) |current_focus| {
@@ -44,8 +44,8 @@ pub fn focusView(
         // If there is a currently focused view, focus the next visible view in the stack.
         const focused_node = @fieldParentPtr(ViewStack(View).Node, "view", current_focus);
         var it = switch (direction) {
-            .Next => ViewStack(View).iterator(focused_node, output.current.tags),
-            .Prev => ViewStack(View).reverseIterator(focused_node, output.current.tags),
+            .next => ViewStack(View).iterator(focused_node, output.current.tags),
+            .previous => ViewStack(View).reverseIterator(focused_node, output.current.tags),
         };
 
         // Skip past the focused node
@@ -60,8 +60,8 @@ pub fn focusView(
     // There is either no currently focused view or the last visible view in the
     // stack is focused and we need to wrap.
     var it = switch (direction) {
-        .Next => ViewStack(View).iterator(output.views.first, output.current.tags),
-        .Prev => ViewStack(View).reverseIterator(output.views.last, output.current.tags),
+        .next => ViewStack(View).iterator(output.views.first, output.current.tags),
+        .previous => ViewStack(View).reverseIterator(output.views.last, output.current.tags),
     };
 
     seat.focus(if (it.next()) |node| &node.view else null);
diff --git a/river/command/send_to_output.zig b/river/command/send_to_output.zig
index f536e57..c0e3ad1 100644
--- a/river/command/send_to_output.zig
+++ b/river/command/send_to_output.zig
@@ -33,7 +33,7 @@ pub fn sendToOutput(
     if (args.len < 2) return Error.NotEnoughArguments;
     if (args.len > 2) return Error.TooManyArguments;
 
-    const direction = try Direction.parse(args[1]);
+    const direction = std.meta.stringToEnum(Direction, args[1]) orelse return Error.InvalidDirection;
     const root = &seat.input_manager.server.root;
 
     if (seat.focused_view) |view| {
@@ -46,8 +46,8 @@ pub fn sendToOutput(
         // Send to the next/prev output in the list if there is one, else wrap
         const current_node = @fieldParentPtr(std.TailQueue(Output).Node, "data", view.output);
         const destination_output = switch (direction) {
-            .Next => if (current_node.next) |node| &node.data else &root.outputs.first.?.data,
-            .Prev => if (current_node.prev) |node| &node.data else &root.outputs.last.?.data,
+            .next => if (current_node.next) |node| &node.data else &root.outputs.first.?.data,
+            .previous => if (current_node.prev) |node| &node.data else &root.outputs.last.?.data,
         };
 
         // Move the view to the target output