~gpanders/wk

ref: 785887cd46b9ff998c33e47d190ba1d97343220e wk/src/cmd/backlinks.zig -rw-r--r-- 938 bytes
785887cd — Greg Anders Refactor backlinks logic 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const std = @import("std");
const fmt = std.fmt;
const mem = std.mem;
const stdout = std.io.getStdOut().outStream();

const Command = @import("../cmd.zig").Command;
const Zettel = @import("../zettel.zig").Zettel;
const getZettels = @import("../zettel.zig").getZettels;
const updateBacklinks = @import("../zettel.zig").updateBacklinks;

pub const cmd = Command{
    .name = "backlinks",
    .aliases = &[_][]const u8{"bl"},
    .usage = "bl|backlinks [NOTE [NOTE ..]]",
    .desc = "Add backlinks to given notes (all notes if no argument given).",
    .run = run,
};

pub fn run(allocator: *mem.Allocator, args: ?[]const []const u8) Command.Error!void {
    const zettels = try getZettels(allocator);
    try updateBacklinks(allocator, zettels);

    var nmodified: u32 = 0;
    for (zettels) |zet| {
        if (try zet.modified()) nmodified += 1;
    }

    stdout.print("Updated backlinks in {} notes.\n", .{nmodified}) catch return;
}