~gpanders/wk

3387af036a45b84637204c8e01749d2ca894bb13 — Greg Anders 8 months ago 7c612ec
Simplify error handling
M src/cmd.zig => src/cmd.zig +1 -1
@@ 111,7 111,7 @@ pub fn handleCommand(allocator: *std.mem.Allocator, command: []const u8, args: ?
            }
        },
        .List => {
            try list.run(allocator, args, zettels.items);
            list.run(allocator, args, zettels.items);
        },
        .Open => {
            const matches = if (args) |_|

M src/cmd/list.zig => src/cmd/list.zig +3 -3
@@ 5,7 5,7 @@ const Zettel = @import("../zettel.zig").Zettel;
pub const usage = "l|list [PATTERN [PATTERN ...]]";
pub const desc = "With no argument, list all notes. Otherwise list notes matching any of the given patterns.";

pub fn run(allocator: *std.mem.Allocator, patterns: ?[][]const u8, zettels: []const Zettel) !void {
pub fn run(allocator: *std.mem.Allocator, patterns: ?[][]const u8, zettels: []const Zettel) void {
    for (zettels) |zet| {
        if (patterns) |pats| {
            for (pats) |pat| {


@@ 13,11 13,11 @@ pub fn run(allocator: *std.mem.Allocator, patterns: ?[][]const u8, zettels: []co
                    std.ascii.indexOfIgnoreCase(zet.fname, pat) != null or
                    std.ascii.indexOfIgnoreCase(zet.title, pat) != null)
                {
                    try zet.print();
                    zet.print();
                }
            }
        } else {
            try zet.print();
            zet.print();
        }
    }
}

M src/cmd/open.zig => src/cmd/open.zig +3 -4
@@ 1,5 1,4 @@
const std = @import("std");
const stderr = std.io.getStdErr().outStream();

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



@@ 22,9 21,9 @@ pub fn run(allocator: *std.mem.Allocator, zettels: ?[]const Zettel) !void {
    switch (term) {
        .Exited => {},
        else => {
            try stderr.print("The following command terminated unexpectedly:\n", .{});
            for (args.items) |arg| try stderr.print("{} ", .{arg});
            return error.ExecFailed;
            std.debug.warn("The following command terminated unexpectedly:\n", .{});
            for (args.items) |arg| std.debug.warn("{} ", .{arg});
            return error.CommandFailed;
        },
    }
}

M src/cmd/preview.zig => src/cmd/preview.zig +19 -17
@@ 1,5 1,4 @@
const std = @import("std");
const stderr = std.io.getStdErr().outStream();

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


@@ 9,24 8,27 @@ pub const desc = "View notes as HTML.";

pub fn run(allocator: *std.mem.Allocator, zettels: []const Zettel) !void {
    const pandoc = util.which(allocator, "pandoc") orelse {
        try stderr.print("Couldn't find pandoc on PATH\n", .{});
        return;
        std.debug.warn("Couldn't find pandoc on PATH\n", .{});
        return error.CommandFailed;
    };

    const open_cmd = blk: {
        if (std.os.getenv("BROWSER")) |browser| {
            break :blk try std.mem.dupe(allocator, u8, browser);
        } else if (util.which(allocator, "xdg-open")) |xdg_open| {
            break :blk xdg_open;
        } else if (util.which(allocator, "open")) |open| {
            break :blk open;
        } else if (util.which(allocator, "firefox")) |firefox| {
            break :blk firefox;
        } else {
            return error.NoAvailableBrowser;
        }
    const open_cmd = if (std.os.getenv("BROWSER")) |browser|
        try std.mem.dupe(allocator, u8, browser)
    else if (util.which(allocator, "xdg-open")) |xdg_open|
        xdg_open
    else if (util.which(allocator, "open")) |open|
        open
    else if (util.which(allocator, "firefox")) |firefox|
        firefox
    else
        null;

    _ = open_cmd orelse {
        std.debug.warn("Error: couldn't find a way to open compiled notes\n", .{});
        return error.CommandFailed;
    };
    defer allocator.free(open_cmd);

    defer allocator.free(open_cmd.?);

    var output_files = std.ArrayList([]const u8).init(allocator);
    defer {


@@ 54,7 56,7 @@ pub fn run(allocator: *std.mem.Allocator, zettels: []const Zettel) !void {
            zet.fname,
        });

        _ = try util.execAndCheck(allocator, &[_][]const u8{ open_cmd, html_file });
        _ = try util.execAndCheck(allocator, &[_][]const u8{ open_cmd.?, html_file });
    }

    // Sleep so that the chosen `open_cmd` has time to open the file before it's deleted

M src/cmd/search.zig => src/cmd/search.zig +1 -1
@@ 36,6 36,6 @@ pub fn run(allocator: *std.mem.Allocator, patterns: [][]const u8, zettels: []con
    }

    for (matches.items) |match, i| {
        try match.print();
        match.print();
    }
}

M src/cmd/tags.zig => src/cmd/tags.zig +2 -2
@@ 12,7 12,7 @@ pub fn run(allocator: *std.mem.Allocator, tags: ?[][]const u8, zettels: []const 
            for (zet.tags) |t| {
                for (tags.?) |tag| {
                    if (std.ascii.eqlIgnoreCase(tag, t)) {
                        try zet.print();
                        zet.print();
                        continue :outer;
                    }
                }


@@ 35,7 35,7 @@ pub fn run(allocator: *std.mem.Allocator, tags: ?[][]const u8, zettels: []const 

        for (existing_tags.items) |t, i| {
            if (i == 0 or !std.mem.eql(u8, t, existing_tags.items[i - 1])) {
                try stdout.print("{}\n", .{t});
                stdout.print("{}\n", .{t}) catch continue;
            }
        }
    }

M src/main.zig => src/main.zig +1 -5
@@ 47,11 47,7 @@ pub fn main() anyerror!void {
            std.debug.warn("Use \"zet help\" for usage.\n", .{});
            std.process.exit(1);
        },
        error.NoAvailableBrowser => {
            std.debug.warn("Error: couldn't find a way to open compiled notes\n", .{});
            std.process.exit(1);
        },
        error.NoMatches => {
        error.CommandFailed, error.NoMatches => {
            std.process.exit(1);
        },
        else => return err,

M src/util.zig => src/util.zig +15 -16
@@ 1,5 1,4 @@
const std = @import("std");
const stderr = std.io.getStdErr().outStream();

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



@@ 37,18 36,18 @@ pub fn execAndCheck(allocator: *std.mem.Allocator, args: [][]const u8) !std.Chil

    switch (result.term) {
        .Exited => |code| if (code != 0) {
            try stderr.print("The following command failed with exit code {}:\n", .{code});
            for (args) |arg| try stderr.print("{} ", .{arg});
            try stderr.print("\n", .{});
            try stderr.print("{}\n", .{result.stderr});
            return error.ExecFailed;
            std.debug.warn("The following command failed with exit code {}:\n", .{code});
            for (args) |arg| std.debug.warn("{} ", .{arg});
            std.debug.warn("\n", .{});
            std.debug.warn("{}\n", .{result.stderr});
            return error.CommandFailed;
        },
        else => {
            try stderr.print("The following command terminated unexpectedly:\n", .{});
            for (args) |arg| try stderr.print("{} ", .{arg});
            try stderr.print("\n", .{});
            try stderr.print("{}\n", .{result.stderr});
            return error.ExecFailed;
            std.debug.warn("The following command terminated unexpectedly:\n", .{});
            for (args) |arg| std.debug.warn("{} ", .{arg});
            std.debug.warn("\n", .{});
            std.debug.warn("{}\n", .{result.stderr});
            return error.CommandFailed;
        },
    }



@@ 89,11 88,11 @@ pub fn commit(allocator: *std.mem.Allocator, zettels: []const Zettel, msg: []con
            _ = try execAndCheck(allocator, &[_][]const u8{ git, "commit", "-m", msg });
        },
        else => {
            try stderr.print("The following command terminated unexpectedly:\n", .{});
            for (diff_index_args) |arg| try stderr.print("{} ", .{arg});
            try stderr.print("\n", .{});
            try stderr.print("{}\n", .{result.stderr});
            return error.ExecFailed;
            std.debug.warn("The following command terminated unexpectedly:\n", .{});
            for (diff_index_args) |arg| std.debug.warn("{} ", .{arg});
            std.debug.warn("\n", .{});
            std.debug.warn("{}\n", .{result.stderr});
            return error.CommandFailed;
        },
    }
}

M src/zettel.zig => src/zettel.zig +3 -4
@@ 1,6 1,5 @@
const std = @import("std");
const stdout = std.io.getStdOut().outStream();
const stderr = std.io.getStdErr().outStream();
const c = @cImport(@cInclude("time.h"));

const ID_LENGTH = "20200106121000".len;


@@ 59,8 58,8 @@ pub const Zettel = struct {
        if (self.tags.len > 0) self.allocator.free(self.tags);
    }

    pub fn print(self: Zettel) !void {
        try stdout.print("{} {}\n", .{ self.id, self.title });
    pub fn print(self: Zettel) void {
        stdout.print("{} {}\n", .{ self.id, self.title }) catch return;
    }
};



@@ 99,7 98,7 @@ pub fn findZettels(allocator: *std.mem.Allocator, zettels: []const Zettel, keywo
        if (findZettel(zettels, keyword)) |zet| {
            try found.append(zet);
        } else {
            try stderr.print("No matches found for '{}'\n", .{keyword});
            std.debug.warn("No matches found for '{}'\n", .{keyword});
        }
    }