~gpanders/wk

91f55c5bf204f5c67c61d5ffa37bcd4ef9e63a9e — Greg Anders 2 months ago d395caa
Commit modified files after 'open' command
1 files changed, 23 insertions(+), 1 deletions(-)

M src/cmd/open.zig
M src/cmd/open.zig => src/cmd/open.zig +23 -1
@@ 1,4 1,5 @@
const std = @import("std");
const fmt = std.fmt;
const mem = std.mem;
const os = std.os;
const warn = std.debug.warn;


@@ 7,6 8,7 @@ const Command = @import("../cmd.zig").Command;
const Zettel = @import("../zettel.zig").Zettel;
const findZettels = @import("../zettel.zig").findZettels;
const getZettels = @import("../zettel.zig").getZettels;
const commit = @import("../util.zig").commit;

pub const cmd = Command{
    .name = "open",


@@ 17,8 19,9 @@ pub const cmd = Command{
};

pub fn run(allocator: *mem.Allocator, args: ?[]const []const u8) Command.Error!void {
    const zettels = try getZettels(allocator);
    const matches = if (args) |_|
        try findZettels(allocator, try getZettels(allocator), args.?)
        try findZettels(allocator, zettels, args.?)
    else
        null;



@@ 41,4 44,23 @@ pub fn run(allocator: *mem.Allocator, args: ?[]const []const u8) Command.Error!v
            return error.CommandFailed;
        },
    }

    var modified = std.ArrayList(*const Zettel).init(allocator);

    for (zettels) |*zet| {
        try zet.writeBacklinks();
        if (try zet.modified()) try modified.append(zet);
    }

    var lines = try std.ArrayList([]const u8).initCapacity(allocator, modified.items.len + 2);
    var files = try std.ArrayList([]const u8).initCapacity(allocator, modified.items.len);

    lines.appendAssumeCapacity(try fmt.allocPrint(allocator, "Updated {} files\n", .{modified.items.len}));
    for (modified.items) |mod| {
        lines.appendAssumeCapacity(try fmt.allocPrint(allocator, "- [[{}]] {}", .{ mod.id, mod.title }));
        files.appendAssumeCapacity(mod.fname);
    }

    const msg = try mem.join(allocator, "\n", lines.items);
    try commit(allocator, files.items, msg);
}