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

commit6cd771b580d1262d7a9e9e217a8bfea3b59e7c41
parente13c5e69f3
authorLeon Henrik Plickat <[email protected]>
date2023-06-27 02:01
river: change order of colums in list-rules command

This will make it easier to extend the command later for new rules

 doc/riverctl.1.scd     | 10 +++++-----
 river/command/rule.zig | 51 +++++++++++++++++++++++---------------------------
 river/rule_list.zig    |  5 +++++
 3 files changed, 33 insertions(+), 33 deletions(-)

diff --git a/doc/riverctl.1.scd b/doc/riverctl.1.scd
index 7218a6c..7f1fcc3 100644
--- a/doc/riverctl.1.scd
+++ b/doc/riverctl.1.scd
@@ -281,11 +281,11 @@ matches everything while _\*\*_ and the empty string are invalid.
 	If multiple rules in a list match a given view the most specific
 	rule will be applied. For example with the following rules
 	```
-	action  app-id  title
-	ssd     foo     bar
-	csd     foo     *
-	csd     *       bar
-	ssd     *       baz
+	app-id  title  action
+	foo     bar    ssd
+	foo     *      csd
+	*       bar    csd
+	*       baz    ssd
 	```
 	a view with app-id 'foo' and title 'bar' would get ssd despite matching
 	two csd rules as the first rule is most specific. Furthermore a view
diff --git a/river/command/rule.zig b/river/command/rule.zig
index 807b55e..965014b 100644
--- a/river/command/rule.zig
+++ b/river/command/rule.zig
@@ -121,40 +121,35 @@ pub fn listRules(_: *Seat, args: []const [:0]const u8, out: *?[]const u8) Error!
     if (args.len > 2) return error.TooManyArguments;
 
     const list = std.meta.stringToEnum(enum { float, ssd }, args[1]) orelse return Error.UnknownOption;
-
-    const rules = switch (list) {
-        .float => server.config.float_rules.rules.items,
-        .ssd => server.config.ssd_rules.rules.items,
+    const max_glob_len = switch (list) {
+        .float => server.config.float_rules.getMaxGlobLen(),
+        .ssd => server.config.ssd_rules.getMaxGlobLen(),
     };
-
-    var action_column_max = "action".len;
-    var app_id_column_max = "app-id".len;
-    for (rules) |rule| {
-        const action = switch (list) {
-            .float => if (rule.value) "float" else "no-float",
-            .ssd => if (rule.value) "ssd" else "csd",
-        };
-        action_column_max = @max(action_column_max, action.len);
-        app_id_column_max = @max(app_id_column_max, rule.app_id_glob.len);
-    }
-    action_column_max += 2;
-    app_id_column_max += 2;
+    const app_id_column_max = 2 + @max("app-id".len, max_glob_len.app_id);
+    const title_column_max = 2 + @max("title".len, max_glob_len.title);
 
     var buffer = std.ArrayList(u8).init(util.gpa);
     const writer = buffer.writer();
 
-    try fmt.formatBuf("action", .{ .width = action_column_max, .alignment = .Left }, writer);
+    try fmt.formatBuf("title", .{ .width = title_column_max, .alignment = .Left }, writer);
     try fmt.formatBuf("app-id", .{ .width = app_id_column_max, .alignment = .Left }, writer);
-    try writer.writeAll("title\n");
-
-    for (rules) |rule| {
-        const action = switch (list) {
-            .float => if (rule.value) "float" else "no-float",
-            .ssd => if (rule.value) "ssd" else "csd",
-        };
-        try fmt.formatBuf(action, .{ .width = action_column_max, .alignment = .Left }, writer);
-        try fmt.formatBuf(rule.app_id_glob, .{ .width = app_id_column_max, .alignment = .Left }, writer);
-        try writer.print("{s}\n", .{rule.title_glob});
+    try writer.writeAll("action\n");
+
+    switch (list) {
+        .float, .ssd => {
+            const rules = switch (list) {
+                .float => server.config.float_rules.rules.items,
+                .ssd => server.config.ssd_rules.rules.items,
+            };
+            for (rules) |rule| {
+                try fmt.formatBuf(rule.title_glob, .{ .width = title_column_max, .alignment = .Left }, writer);
+                try fmt.formatBuf(rule.app_id_glob, .{ .width = app_id_column_max, .alignment = .Left }, writer);
+                try writer.print("{s}\n", .{switch (list) {
+                    .float => if (rule.value) "float" else "no-float",
+                    .ssd => if (rule.value) "ssd" else "csd",
+                }});
+            }
+        },
     }
 
     out.* = buffer.toOwnedSlice();
diff --git a/river/rule_list.zig b/river/rule_list.zig
index 325dcc3..de1fa41 100644
--- a/river/rule_list.zig
+++ b/river/rule_list.zig
@@ -22,6 +22,11 @@ const util = @import("util.zig");
 
 const View = @import("View.zig");
 
+pub const MaxGlobLen = struct {
+    app_id: usize,
+    title: usize,
+};
+
 pub fn RuleList(comptime T: type) type {
     return struct {
         const Self = @This();