1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
// This file is part of river, a dynamic tiling wayland compositor.
//
// Copyright 2023 The River Developers
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const mem = std.mem;
const globber = @import("globber");
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();
const Rule = struct {
app_id_glob: []const u8,
title_glob: []const u8,
value: T,
};
/// Ordered from most specific to most general.
/// Ordered first by app-id generality then by title generality.
rules: std.ArrayListUnmanaged(Rule) = .{},
pub fn deinit(list: *Self) void {
for (list.rules.items) |rule| {
util.gpa.free(rule.app_id_glob);
util.gpa.free(rule.title_glob);
}
list.rules.deinit(util.gpa);
}
pub fn add(list: *Self, rule: Rule) error{OutOfMemory}!void {
const index = for (list.rules.items) |*existing, i| {
if (mem.eql(u8, rule.app_id_glob, existing.app_id_glob) and
mem.eql(u8, rule.title_glob, existing.title_glob))
{
existing.value = rule.value;
return;
}
switch (globber.order(rule.app_id_glob, existing.app_id_glob)) {
.lt => break i,
.eq => {
if (globber.order(rule.title_glob, existing.title_glob) == .lt) {
break i;
}
},
.gt => {},
}
} else list.rules.items.len;
const owned_app_id_glob = try util.gpa.dupe(u8, rule.app_id_glob);
errdefer util.gpa.free(owned_app_id_glob);
const owned_title_glob = try util.gpa.dupe(u8, rule.title_glob);
errdefer util.gpa.free(owned_title_glob);
try list.rules.insert(util.gpa, index, .{
.app_id_glob = owned_app_id_glob,
.title_glob = owned_title_glob,
.value = rule.value,
});
}
pub fn del(list: *Self, rule: struct { app_id_glob: []const u8, title_glob: []const u8 }) void {
for (list.rules.items) |existing, i| {
if (mem.eql(u8, rule.app_id_glob, existing.app_id_glob) and
mem.eql(u8, rule.title_glob, existing.title_glob))
{
util.gpa.free(existing.app_id_glob);
util.gpa.free(existing.title_glob);
_ = list.rules.orderedRemove(i);
return;
}
}
}
/// Returns the value of the most specific rule matching the view.
/// Returns null if no rule matches.
pub fn match(list: *Self, view: *View) ?T {
const app_id = mem.sliceTo(view.getAppId(), 0) orelse "";
const title = mem.sliceTo(view.getTitle(), 0) orelse "";
for (list.rules.items) |rule| {
if (globber.match(app_id, rule.app_id_glob) and
globber.match(title, rule.title_glob))
{
return rule.value;
}
}
return null;
}
/// Returns the length of the longest globs.
pub fn getMaxGlobLen(self: *const Self) MaxGlobLen {
var app_id_len: usize = 0;
var title_len: usize = 0;
for (self.rules.items) |rule| {
app_id_len = @max(app_id_len, rule.app_id_glob.len);
title_len = @max(title_len, rule.title_glob.len);
}
return .{
.app_id = app_id_len,
.title = title_len,
};
}
};
}
|