Skip to content

ttytm/dmon-zig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dmon-zig

Cross-platform Zig module to monitor changes in directories. It utilizes the dmon C99 library.

Installation

# ~/<ProjectsPath>/your-awesome-projct
zig fetch --save https://github.com/ttytm/dmon-zig/archive/main.tar.gz
// your-awesome-projct/build.zig
const std = @import("std");

pub fn build(b: *std.Build) void {
	// ..
	const dmon_dep = b.dependency("dmon", .{});
	const exe = b.addExecutable(.{
		.name = "your-awesome-projct",
		// ..
	});
	exe.root_module.addImport("dmon", dmon_dep.module("dmon"));
	// ...
}

Usage Example

const std = @import("std");
const dmon = @import("dmon");
const print = std.debug.print;

const Context = struct {
	trigger_count: u32 = 0,
};

pub fn watchCb(
	comptime Ctx: type,
	_: dmon.WatchId,
	action: dmon.Action,
	root_dir: [*:0]const u8,
	file_path: [*:0]const u8,
	old_file_path: ?[*:0]const u8,
	context: *Ctx,
) void {
	print("Action: {}\n", .{action});
	print("Root: {s}\n", .{root_dir});
	print("File path: {s}\n", .{file_path});
	print("Old file path: {s}\n", .{old_file_path orelse ""});
	context.trigger_count += 1;
}

pub fn main() !void {
	dmon.init();
	defer dmon.deinit();

	const watch_path = "/home/user/Documents";
	const id = dmon.watch(Context, watch_path, watchCb, .{ .recursive = true }, &ctx);
	print("Starting to watch: {s}; Watcher ID: {d}\n", .{ watch_path, id });

	while (true) {
		if (ctx.trigger_count >= 3) break;
	}
}

For a simple local example watching the cwd: dmon-zig/examples/src/main.zig

# Perform a lightweight, filtered, yet complete clone
git clone --recursive --shallow-submodules --filter=blob:none \
  https://github.com/ttytm/dmon-zig && \
cd dmon-zig/examples
# dmon-zig/examples
zig build run