feat: add --version

This commit is contained in:
CJ van den Berg 2026-03-14 18:01:32 +01:00
parent f68fdfe013
commit 8662a3f7fd
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 54 additions and 6 deletions

View file

@ -238,8 +238,8 @@ pub fn add_watch(self: *@This(), allocator: std.mem.Allocator, path: []const u8)
self.arm(allocator) catch return error.WatchFailed;
}
pub fn remove_watch(self: *@This(), allocator: std.mem.Allocator, path: []const u8) void {
pub fn remove_watch(self: *@This(), allocator: std.mem.Allocator, path: []const u8) error{WatchFailed}!void {
if (self.watches.fetchSwapRemove(path)) |entry| allocator.free(entry.key);
self.stop_stream(allocator);
self.arm(allocator) catch {};
self.arm(allocator) catch return error.WatchFailed;
}

View file

@ -1,6 +1,7 @@
const std = @import("std");
const builtin = @import("builtin");
const nightwatch = @import("nightwatch");
const build_options = @import("build_options");
const Watcher = switch (builtin.os.tag) {
.linux => nightwatch.Create(.polling),
@ -170,6 +171,27 @@ fn usage(out: std.fs.File) !void {
try writer.interface.flush();
}
fn version(out: std.fs.File) !void {
var buf: [4096]u8 = undefined;
var writer = out.writer(&buf);
try writer.interface.print(
\\nightwatch version {s}
\\using: {s}
\\
, .{
@embedFile("version"),
@typeName(get_nightwatch().Backend),
});
try writer.interface.flush();
}
fn get_nightwatch() type {
return switch (builtin.os.tag) {
.linux => nightwatch.Create(.polling),
else => nightwatch.Default,
};
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
@ -182,11 +204,17 @@ pub fn main() !void {
try usage(std.fs.File.stderr());
std.process.exit(1);
}
if (std.mem.eql(u8, args[1], "-h") or std.mem.eql(u8, args[1], "--help")) {
try usage(std.fs.File.stdout());
return;
}
if (std.mem.eql(u8, args[1], "--version")) {
try version(std.fs.File.stdout());
return;
}
var buf: [4096]u8 = undefined;
var stderr = std.fs.File.stderr().writer(&buf);
defer stderr.interface.flush() catch {};
@ -254,10 +282,7 @@ pub fn main() !void {
.ignore = ignore_list.items,
};
var watcher = switch (builtin.os.tag) {
.linux => try nightwatch.Create(.polling).init(allocator, &cli_handler.handler),
else => try nightwatch.Default.init(allocator, &cli_handler.handler),
};
var watcher = try get_nightwatch().init(allocator, &cli_handler.handler);
defer watcher.deinit();