~gpanders/wk

2060a8f3e5412ef77f4cffc4565a6e544e239fa0 — Greg Anders 8 months ago cdfd931
Define zettel dir with eval block
1 files changed, 16 insertions(+), 23 deletions(-)

M src/main.zig
M src/main.zig => src/main.zig +16 -23
@@ 1,24 1,7 @@
const std = @import("std");
const stdout = std.io.getStdOut().outStream();
const stderr = std.io.getStdErr().outStream();

const cmd = @import("cmd.zig");

fn zettelDir(allocator: *std.mem.Allocator) ![]const u8 {
    var path: []u8 = undefined;
    if (std.os.getenv("ZETTEL_DIR")) |dir| {
        path = try std.mem.dupe(allocator, u8, dir);
    } else if (std.os.getenv("XDG_DATA_HOME")) |xdg_data_home| {
        path = try std.fs.path.join(allocator, &[_][]const u8{ xdg_data_home, "/zet" });
    } else if (std.os.getenv("HOME")) |home| {
        path = try std.fs.path.join(allocator, &[_][]const u8{ home, "/.local/share/zet" });
    } else unreachable; // $HOME should always be defined

    defer allocator.free(path);

    return try std.fs.path.resolve(allocator, &[_][]const u8{path});
}

pub fn main() anyerror!void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();


@@ 34,7 17,17 @@ pub fn main() anyerror!void {
        return;
    }

    const dir = try zettelDir(allocator);
    const dir = blk: {
        if (std.os.getenv("ZETTEL_DIR")) |dir| {
            break :blk try std.mem.dupe(allocator, u8, dir);
        } else if (std.os.getenv("XDG_DATA_HOME")) |xdg_data_home| {
            break :blk try std.fs.path.join(allocator, &[_][]const u8{ xdg_data_home, "zet" });
        } else if (std.os.getenv("HOME")) |home| {
            break :blk try std.fs.path.join(allocator, &[_][]const u8{ home, ".local", "share", "zet" });
        } else {
            unreachable; // $HOME should always be defined
        }
    };
    defer allocator.free(dir);

    try std.fs.cwd().makePath(dir);


@@ 45,17 38,17 @@ pub fn main() anyerror!void {

    cmd.handleCommand(allocator, command, args) catch |err| return switch (err) {
        error.UnknownCommand => {
            try stderr.print("Error: unknown command: {}\n", .{command});
            try stderr.print("Use \"zet help\" for usage.\n", .{});
            std.debug.warn("Error: unknown command: {}\n", .{command});
            std.debug.warn("Use \"zet help\" for usage.\n", .{});
            std.process.exit(1);
        },
        error.MissingRequiredArgument => {
            try stderr.print("Error: missing required argument\n\n", .{});
            try stderr.print("Use \"zet help\" for usage.\n", .{});
            std.debug.warn("Error: missing required argument\n\n", .{});
            std.debug.warn("Use \"zet help\" for usage.\n", .{});
            std.process.exit(1);
        },
        error.NoAvailableBrowser => {
            try stderr.print("Error: couldn't find a way to open compiled notes\n", .{});
            std.debug.warn("Error: couldn't find a way to open compiled notes\n", .{});
            std.process.exit(1);
        },
        error.NoMatches => {