From 88504d6043d6e498abb0f6f30412c078546672b0 Mon Sep 17 00:00:00 2001 From: AramJonghu Date: Sat, 23 May 2026 23:33:42 +0200 Subject: [PATCH] some setup for subcommands: do nothing right now, experimenting with pointers --- build.zig | 30 +++++------------------------- src/daemon.zig | 3 +++ src/db.zig | 2 ++ src/main.zig | 20 ++++++++++++++++++++ src/notify.zig | 1 + src/reminder.zig | 8 ++++++++ src/root.zig | 18 ------------------ src/subcommands.zig | 32 ++++++++++++++++++++++++++++++++ 8 files changed, 71 insertions(+), 43 deletions(-) create mode 100644 src/daemon.zig create mode 100644 src/db.zig create mode 100644 src/notify.zig create mode 100644 src/reminder.zig delete mode 100644 src/root.zig create mode 100644 src/subcommands.zig diff --git a/build.zig b/build.zig index 1fa46b1..79a0f97 100644 --- a/build.zig +++ b/build.zig @@ -3,10 +3,6 @@ const std = @import("std"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const mod = b.addModule("nfi", .{ - .root_source_file = b.path("src/root.zig"), - .target = target, - }); const exe = b.addExecutable(.{ .name = "nfi", @@ -14,38 +10,22 @@ pub fn build(b: *std.Build) void { .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, - .imports = &.{ - .{ .name = "nfi", .module = mod }, - }, }), }); b.installArtifact(exe); - const run_step = b.step("run", "Run the app"); - const run_cmd = b.addRunArtifact(exe); - run_step.dependOn(&run_cmd.step); - run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| run_cmd.addArgs(args); - if (b.args) |args| { - run_cmd.addArgs(args); - } - - const mod_tests = b.addTest(.{ - .root_module = mod, - }); - - const run_mod_tests = b.addRunArtifact(mod_tests); + const run_step = b.step("run", "Run nfi"); + run_step.dependOn(&run_cmd.step); const exe_tests = b.addTest(.{ .root_module = exe.root_module, }); - - const run_exe_tests = b.addRunArtifact(exe_tests); - + const run_tests = b.addRunArtifact(exe_tests); const test_step = b.step("test", "Run tests"); - test_step.dependOn(&run_mod_tests.step); - test_step.dependOn(&run_exe_tests.step); + test_step.dependOn(&run_tests.step); } diff --git a/src/daemon.zig b/src/daemon.zig new file mode 100644 index 0000000..0987040 --- /dev/null +++ b/src/daemon.zig @@ -0,0 +1,3 @@ +const std = @import("std"); +const db = @import("db.zig"); +const notify = @import("notify.zig"); diff --git a/src/db.zig b/src/db.zig new file mode 100644 index 0000000..4c380f7 --- /dev/null +++ b/src/db.zig @@ -0,0 +1,2 @@ +const std = @import("std"); +const Reminder = @import("reminder.zig").Reminder; diff --git a/src/main.zig b/src/main.zig index e69de29..018c70d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -0,0 +1,20 @@ +const std = @import("std"); +const sub = @import("subcommands.zig"); + +fn eql(a: []const u8, b: []const u8) bool { + return std.mem.eql(u8, a, b); +} + +pub fn main(init: std.process.Init.Minimal) !void { + var arg_iter = try std.process.Args.Iterator.initAllocator(init.args, std.heap.page_allocator); + defer arg_iter.deinit(); + + _ = arg_iter.next(); + const subcommand = (arg_iter.next()) orelse return; + + for (sub.subcommands) |cmd| { + if (std.mem.eql(u8, subcommand, cmd.name)) { + return cmd.handler(&arg_iter); + } + } +} diff --git a/src/notify.zig b/src/notify.zig new file mode 100644 index 0000000..95a0b68 --- /dev/null +++ b/src/notify.zig @@ -0,0 +1 @@ +const std = @import("std"); diff --git a/src/reminder.zig b/src/reminder.zig new file mode 100644 index 0000000..b46e3ec --- /dev/null +++ b/src/reminder.zig @@ -0,0 +1,8 @@ +pub const Reminder = struct { + id: u64, + title: []const u8, + due_at: i64, + done: bool, + snoozed: bool, + created_at: i64, +}; diff --git a/src/root.zig b/src/root.zig deleted file mode 100644 index 5a71250..0000000 --- a/src/root.zig +++ /dev/null @@ -1,18 +0,0 @@ -//! By convention, root.zig is the root source file when making a package. -const std = @import("std"); -const Io = std.Io; - -/// This is a documentation comment to explain the `printAnotherMessage` function below. -/// -/// Accepting an `Io.Writer` instance is a handy way to write reusable code. -pub fn printAnotherMessage(writer: *Io.Writer) Io.Writer.Error!void { - try writer.print("Run `zig build test` to run the tests.\n", .{}); -} - -pub fn add(a: i32, b: i32) i32 { - return a + b; -} - -test "basic add functionality" { - try std.testing.expect(add(3, 7) == 10); -} diff --git a/src/subcommands.zig b/src/subcommands.zig new file mode 100644 index 0000000..57a4acc --- /dev/null +++ b/src/subcommands.zig @@ -0,0 +1,32 @@ +const std = @import("std"); + +pub const Handler = *const fn (*std.process.Args.Iterator) anyerror!void; + +pub const Subcommand = struct { + name: []const u8, + handler: Handler, +}; + +pub const subcommands = [_]Subcommand{ + .{ .name = "add", .handler = handleAdd }, + .{ .name = "remove", .handler = handleRemove }, + .{ .name = "done", .handler = handleDone }, + .{ .name = "list", .handler = handleList }, +}; + +pub fn handleAdd(args: *std.process.Args.Iterator) !void { + _ = args; + std.debug.print("add!", .{}); +} + +pub fn handleRemove(args: *std.process.Args.Iterator) !void { + _ = args; +} + +pub fn handleDone(args: *std.process.Args.Iterator) !void { + _ = args; +} + +pub fn handleList(args: *std.process.Args.Iterator) !void { + _ = args; +}