-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbasic.zig
More file actions
42 lines (32 loc) · 1.08 KB
/
basic.zig
File metadata and controls
42 lines (32 loc) · 1.08 KB
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
const std = @import("std");
const fzwatch = @import("fzwatch");
fn callback(context: ?*anyopaque, event: fzwatch.Event) void {
_ = context;
switch (event) {
.modified => std.debug.print("File was modified!\n", .{}),
}
}
fn watcherThread(watcher: *fzwatch.Watcher) !void {
try watcher.start(.{});
}
pub fn main() !void {
const allocator = std.heap.page_allocator;
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
if (args.len < 2) {
std.debug.print("Usage: {s} <file-to-watch>\n", .{args[0]});
return error.InvalidArgument;
}
const target_file = args[1];
var watcher = try fzwatch.Watcher.init(allocator);
defer watcher.deinit();
try watcher.addFile(target_file);
watcher.setCallback(callback, null);
const thread = try std.Thread.spawn(.{}, watcherThread, .{&watcher});
var i: usize = 0;
while (true) : (i += 1) {
std.debug.print("Working... (iteration {d})\n", .{i});
std.Thread.sleep(std.time.ns_per_s * 2);
}
thread.join();
}