~gpanders/wk

55fa3e61b4c6bf191efd0e3e27d31aa124929eff — Greg Anders a month ago 3cb4407
Simplify 'show' command logic
2 files changed, 12 insertions(+), 14 deletions(-)

M src/cmd/show.zig
M src/tty.zig
M src/cmd/show.zig => src/cmd/show.zig +11 -13
@@ 18,27 18,25 @@ pub const cmd = Command{

pub fn run(allocator: *mem.Allocator, args: ?[]const []const u8) Command.Error!void {
    if (args) |patterns| {
        const writer = ColorWriter.new(std.io.getStdOut());
        const zettels = try getZettels(allocator);
        for (zettels) |zet| {
            if (!zet.match(patterns)) continue;

            const writer = ColorWriter.new(std.io.getStdOut());

            writer.setColor(.Blue).print("{}\n", .{zet.fname});
            writer.setColor(.Yellow).print("{}", .{zet.contents[0..3]});

            const metadata_end = blk: {
                if (mem.indexOfPos(u8, zet.contents, 3, "...")) |i| {
                    break :blk i;
                } else if (mem.indexOfPos(u8, zet.contents, 3, "---")) |i| {
                    break :blk i;
                } else unreachable;
            };
            var it = mem.split(zet.contents, "\n");

            // Print front matter fences in yellow
            writer.setColor(.Yellow).print("{}\n", .{it.next()});

            writer.print("{}", .{zet.contents[3..metadata_end]});
            while (it.next()) |line| {
                const fence = mem.eql(u8, line, "---");
                writer.setColor(if (fence) .Yellow else null).print("{}\n", .{line});
                if (fence) break;
            }

            writer.setColor(.Yellow).print("{}", .{zet.contents[metadata_end .. metadata_end + 3]});
            writer.print("{}\n", .{zet.contents[metadata_end + 3 ..]});
            writer.print("{}\n", .{it.rest()});
        }
    } else {
        return error.MissingRequiredArgument;

M src/tty.zig => src/tty.zig +1 -1
@@ 33,7 33,7 @@ pub const ColorWriter = struct {
        return .{ .file = file };
    }

    pub fn setColor(self: Self, color: Color) Self {
    pub fn setColor(self: Self, color: ?Color) Self {
        return .{ .color = color, .bold = self.bold, .italic = self.italic, .file = self.file };
    }