~gpanders/wk

ref: 20be22bcd9adf5159a4c4993bbb5ddfa84f7dd80 wk/src/cmd/inbox.zig -rw-r--r-- 976 bytes
20be22bc — Greg Anders Add 'inbox' subcommand 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
30
31
32
33
34
35
36
37
const std = @import("std");
const ascii = std.ascii;
const fmt = std.fmt;
const mem = std.mem;
const os = std.os;
const warn = std.debug.warn;

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

pub const cmd = Command{
    .name = "inbox",
    .aliases = &[_][]const u8{ "i", "in" },
    .usage = "in|inbox",
    .desc = "Open inbox in your editor",
    .run = run,
};

pub fn run(allocator: *mem.Allocator, args: ?[]const []const u8) Command.Error!void {
    const argv = &[_][]const u8{
        os.getenv("EDITOR") orelse "vi",
        "inbox.md",
    };

    var proc = try std.ChildProcess.init(argv, allocator);
    defer proc.deinit();

    const term = try proc.spawnAndWait();
    switch (term) {
        .Exited => {},
        else => {
            warn("The following command terminated unexpectedly:\n", .{});
            for (argv) |arg| warn("{} ", .{arg});
            return error.CommandFailed;
        },
    }
}