~gpanders/wk

8f07e73a7820168cc7f70fa0101e3661a6ad1bcb — Greg Anders 8 months ago 5ce1b9c
Add command descriptions to usage output
M src/cmd.zig => src/cmd.zig +13 -1
@@ 19,7 19,7 @@ pub const Command = enum {
    Show,
    Tags,

    pub fn usage(self: Command) []const u8 {
    pub fn usage(comptime self: Command) []const u8 {
        return switch (self) {
            .List => list.usage,
            .New => new.usage,


@@ 30,6 30,18 @@ pub const Command = enum {
            .Tags => tags.usage,
        };
    }

    pub fn desc(comptime self: Command) []const u8 {
        return switch (self) {
            .List => list.desc,
            .New => new.desc,
            .Open => open.desc,
            .Preview => preview.desc,
            .Search => search.desc,
            .Show => show.desc,
            .Tags => tags.desc,
        };
    }
};

pub fn handleCommand(command: []const u8, arg: ?[]const u8, allocator: *std.mem.Allocator) !void {

M src/cmd/list.zig => src/cmd/list.zig +1 -0
@@ 4,6 4,7 @@ const stdout = std.io.getStdOut().outStream();
const Zettel = @import("../zettel.zig").Zettel;

pub const usage = "l|list [pattern]";
pub const desc = "With no argument, list all notes. Otherwise list notes matching the given pattern";

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

M src/cmd/new.zig => src/cmd/new.zig +1 -0
@@ 6,6 6,7 @@ const Zettel = @import("../zettel.zig").Zettel;
const util = @import("../util.zig");

pub const usage = "n|new <title>";
pub const desc = "Create a new note with the given title";

pub fn run(allocator: *std.mem.Allocator, title: []const u8) !void {
    const new_zettel = try Zettel.new(allocator, title);

M src/cmd/open.zig => src/cmd/open.zig +1 -0
@@ 3,6 3,7 @@ const std = @import("std");
const zettel = @import("../zettel.zig");

pub const usage = "o|open [name]";
pub const desc = "Open the given note in your $EDITOR";

pub fn run(allocator: *std.mem.Allocator, zet: ?zettel.Zettel) !void {
    var args = std.ArrayList([]const u8).init(allocator);

M src/cmd/preview.zig => src/cmd/preview.zig +1 -0
@@ 5,6 5,7 @@ const Zettel = @import("../zettel.zig").Zettel;
const util = @import("../util.zig");

pub const usage = "p|preview <name>";
pub const desc = "View a note as HTML";

pub fn run(allocator: *std.mem.Allocator, zet: Zettel) !void {
    const pandoc = util.expandPath(allocator, "pandoc") orelse {

M src/cmd/search.zig => src/cmd/search.zig +1 -0
@@ 4,6 4,7 @@ const stdout = std.io.getStdOut().outStream();
const Zettel = @import("../zettel.zig").Zettel;

pub const usage = "s|search <pattern>";
pub const desc = "Search notes for the given pattern";

pub fn run(allocator: *std.mem.Allocator, pattern: []const u8, zettels: []const Zettel) !void {
    var matches = std.ArrayList(Zettel).init(allocator);

M src/cmd/show.zig => src/cmd/show.zig +1 -0
@@ 5,6 5,7 @@ const stderr = std.io.getStdErr().outStream();
const Zettel = @import("../zettel.zig").Zettel;

pub const usage = "sh|show <name>";
pub const desc = "Display contents of a note to stdout";

pub fn run(allocator: *std.mem.Allocator, zet: Zettel) !void {
    var file = try std.fs.cwd().openFile(zet.fname, .{ .read = true });

M src/cmd/tags.zig => src/cmd/tags.zig +1 -0
@@ 4,6 4,7 @@ const stdout = std.io.getStdOut().outStream();
const Zettel = @import("../zettel.zig").Zettel;

pub const usage = "t|tags [tag]";
pub const desc = "With no argument, list all tags found in notes. Otherwise list all notes containing the given tag";

pub fn run(allocator: *std.mem.Allocator, tag: ?[]const u8, zettels: []const Zettel) !void {
    if (tag == null) {

M src/main.zig => src/main.zig +3 -1
@@ 9,7 9,9 @@ fn printUsage(exe: []const u8) void {
    stdout.print("\n", .{}) catch return;
    stdout.print("Available commands:\n", .{}) catch return;
    inline for (@typeInfo(cmd.Command).Enum.fields) |command| {
        stdout.print("\t{}\n", .{@field(cmd.Command, command.name).usage()}) catch return;
        comptime var usage = @field(cmd.Command, command.name).usage();
        stdout.print("    {}", .{usage}) catch return;
        stdout.print("{}{}\n", .{ " " ** (24 - usage.len), @field(cmd.Command, command.name).desc() }) catch return;
    }
}