river: change order of colums in list-rules command
This will make it easier to extend the command later for new rules
This commit is contained in:
parent
9b5ea39580
commit
a98de941d0
@ -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
|
If multiple rules in a list match a given view the most specific
|
||||||
rule will be applied. For example with the following rules
|
rule will be applied. For example with the following rules
|
||||||
```
|
```
|
||||||
action app-id title
|
app-id title action
|
||||||
ssd foo bar
|
foo bar ssd
|
||||||
csd foo *
|
foo * csd
|
||||||
csd * bar
|
* bar csd
|
||||||
ssd * baz
|
* baz ssd
|
||||||
```
|
```
|
||||||
a view with app-id 'foo' and title 'bar' would get ssd despite matching
|
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
|
two csd rules as the first rule is most specific. Furthermore a view
|
||||||
|
@ -121,40 +121,35 @@ pub fn listRules(_: *Seat, args: []const [:0]const u8, out: *?[]const u8) Error!
|
|||||||
if (args.len > 2) return error.TooManyArguments;
|
if (args.len > 2) return error.TooManyArguments;
|
||||||
|
|
||||||
const list = std.meta.stringToEnum(enum { float, ssd }, args[1]) orelse return Error.UnknownOption;
|
const list = std.meta.stringToEnum(enum { float, ssd }, args[1]) orelse return Error.UnknownOption;
|
||||||
|
const max_glob_len = switch (list) {
|
||||||
const rules = switch (list) {
|
.float => server.config.float_rules.getMaxGlobLen(),
|
||||||
.float => server.config.float_rules.rules.items,
|
.ssd => server.config.ssd_rules.getMaxGlobLen(),
|
||||||
.ssd => server.config.ssd_rules.rules.items,
|
|
||||||
};
|
};
|
||||||
|
const app_id_column_max = 2 + @max("app-id".len, max_glob_len.app_id);
|
||||||
var action_column_max = "action".len;
|
const title_column_max = 2 + @max("title".len, max_glob_len.title);
|
||||||
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;
|
|
||||||
|
|
||||||
var buffer = std.ArrayList(u8).init(util.gpa);
|
var buffer = std.ArrayList(u8).init(util.gpa);
|
||||||
const writer = buffer.writer();
|
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 fmt.formatBuf("app-id", .{ .width = app_id_column_max, .alignment = .Left }, writer);
|
||||||
try writer.writeAll("title\n");
|
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| {
|
for (rules) |rule| {
|
||||||
const action = switch (list) {
|
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",
|
.float => if (rule.value) "float" else "no-float",
|
||||||
.ssd => if (rule.value) "ssd" else "csd",
|
.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});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
out.* = buffer.toOwnedSlice();
|
out.* = buffer.toOwnedSlice();
|
||||||
|
@ -22,6 +22,11 @@ const util = @import("util.zig");
|
|||||||
|
|
||||||
const View = @import("View.zig");
|
const View = @import("View.zig");
|
||||||
|
|
||||||
|
pub const MaxGlobLen = struct {
|
||||||
|
app_id: usize,
|
||||||
|
title: usize,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn RuleList(comptime T: type) type {
|
pub fn RuleList(comptime T: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
Loading…
Reference in New Issue
Block a user