Skip to content

Commit 429b005

Browse files
feat: add --match substring filtering for file names
1 parent df18672 commit 429b005

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/file.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ pub const File = struct {
103103

104104
inline fn shouldIncludeByName(name: []const u8, matches: []const []const u8) bool {
105105
for (matches) |m| {
106-
if (std.ascii.eqlIgnoreCase(name, m)) {
106+
// check if name contains m as a substring, ignoring case
107+
if (std.ascii.findIgnoreCase(name, m) != null) {
107108
return true;
108109
}
109110
}

src/files.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub const Files = struct {
9090
var fs = (try file.File.init(
9191
&entry,
9292
&dir,
93-
.{ .load_stat = load_stat, .show_hidden = opt.show_hidden, .only_dir = opt.only_dir, .only_file = opt.only_file, .exts = opt.exts },
93+
.{ .load_stat = load_stat, .show_hidden = opt.show_hidden, .only_dir = opt.only_dir, .only_file = opt.only_file, .exts = opt.exts, .matches = opt.matches },
9494
&username_inventory,
9595
&groupname_inventory,
9696
)) orelse continue;

src/main.zig

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const params_desc: []const u8 = blk: {
2020
\\-D, --no_dir Only show files, not directories. When used in conjunction with -d, neither is effective.
2121
\\-g, --git Show git status of files. Only effective when in long format.
2222
\\-e, --ext <str>... Filter by extension (e.g. --ext zig,md,ts).
23+
\\-m, --match <str>... Match file names/subtring (e.g. --match main,readme).
2324
\\<str>...
2425
\\
2526
;
@@ -67,8 +68,10 @@ pub fn main(init: std.process.Init.Minimal) !void {
6768
var recursion_level: i8 = 0; // 0 means infinite
6869
var git: bool = false;
6970
var exts: ?[]const []const u8 = null;
71+
var matches: ?[]const []const u8 = null;
7072

7173
var ext_list = try std.ArrayList([]const u8).initCapacity(allocator, 0);
74+
var match_list = try std.ArrayList([]const u8).initCapacity(allocator, 0);
7275

7376
var path: []const u8 = ".";
7477

@@ -144,6 +147,23 @@ pub fn main(init: std.process.Init.Minimal) !void {
144147
exts = ext_list.items;
145148
}
146149
}
150+
if (res.args.match.len > 0) {
151+
const match_args = res.args.match;
152+
for (match_args) |arg| {
153+
var token_it = std.mem.splitScalar(u8, arg, ',');
154+
while (token_it.next()) |token| {
155+
const trimmed = std.mem.trim(u8, token, " \t\r\n");
156+
if (trimmed.len == 0) {
157+
continue;
158+
}
159+
160+
try match_list.append(allocator, trimmed);
161+
}
162+
}
163+
if (match_list.items.len > 0) {
164+
matches = match_list.items;
165+
}
166+
}
147167

148168
// get file path from args
149169
if (res.positionals[0].len > 0) {
@@ -158,7 +178,7 @@ pub fn main(init: std.process.Init.Minimal) !void {
158178
allocator,
159179
io,
160180
dir,
161-
.{ .show_detail = show_detail, .show_hidden = show_hidden, .sort_type = sort_type, .recursive = recursive, .pure = pure, .only_dir = only_dir, .only_file = only_file, .recursion_level = recursion_level, .report = report, .show_git = git, .path = path, .exts = exts },
181+
.{ .show_detail = show_detail, .show_hidden = show_hidden, .sort_type = sort_type, .recursive = recursive, .pure = pure, .only_dir = only_dir, .only_file = only_file, .recursion_level = recursion_level, .report = report, .show_git = git, .path = path, .exts = exts, .matches = matches },
162182
);
163183
defer files.deinit();
164184

0 commit comments

Comments
 (0)