diff options
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/src/main.zig b/src/main.zig index d7cb0c4..f47bd36 100644 --- a/src/main.zig +++ b/src/main.zig @@ -144,17 +144,17 @@ const Cli = struct { _ = argv.skip(); // skip arg0 if (argv.inner.count == 1) { - try Cli.mdCmd(ctx); - try Cli.genCmd(ctx); + try Cli.mdCmd(ctx, null); + try Cli.genCmd(ctx, null); } while (argv.next()) |arg| { if (std.mem.eql(u8, arg, "help")) { Cli.helpCmd(ctx); } else if (std.mem.eql(u8, arg, "gen")) { - try Cli.genCmd(ctx); + try Cli.genCmd(ctx, &argv); } else if (std.mem.eql(u8, arg, "md")) { - try Cli.mdCmd(ctx); + try Cli.mdCmd(ctx, &argv); } else if (std.mem.eql(u8, arg, "watch")) { try Cli.watchCmd(ctx); } else if (std.mem.eql(u8, arg, "serve")) { @@ -165,25 +165,46 @@ const Cli = struct { } } + fn checkOpts(maybe_argv: ?*std.process.ArgIterator, opts: []const []const u8) bool { + if (maybe_argv) |argv| { + const arg = argv.next() orelse return false; + // check opts + for (opts) |opt| { + if (std.mem.eql(u8, arg, opt)) return true; + } + // rewind argv + argv.*.inner.index -= 1; + return false; + } else { + return false; + } + } + fn helpCmd(_: *Context) void { std.debug.print("usage: rbl [gen|md|watch|serve]\n\n", .{}); } - fn genCmd(ctx: *Context) !void { + fn genCmd(ctx: *Context, argv: ?*std.process.ArgIterator) !void { try index(ctx); try gen.generate(ctx); + + if (checkOpts(argv, &.{ "-c", "--commit" })) + try exe.commit(ctx, ctx.generated.items); } - fn mdCmd(ctx: *Context) !void { + fn mdCmd(ctx: *Context, argv: ?*std.process.ArgIterator) !void { try index(ctx); try exe.runPandoc(ctx); + + if (checkOpts(argv, &.{ "-c", "--commit" })) + try exe.commit(ctx, ctx.generated.items); } fn watchCmd(ctx: *Context) !void { const selfExe = try std.fs.selfExePathAlloc(ctx.allocator()); const command = try std.fmt.allocPrint(ctx.allocator(), \\find ./src/inc/ -type f -print | entr -nc -dd -- {s} md gen - , .{selfExe}); + , .{selfExe}); try exe.shell(ctx, command, null); } |