M src/main.zig => src/main.zig +45 -39
@@ 1,6 1,6 @@
const std = @import("std");
-const util = @import("./util.zig");
-const Note = @import("./note.zig").Note;
+const util = @import("util.zig");
+const Note = @import("note.zig").Note;
const fs = std.fs;
const mem = std.mem;
@@ 20,8 20,8 @@ const help_text =
\\
;
-// Read a note from disk, filtering out any lines
-// inserted by the indexing process.
+/// Read a note from disk, filtering out any lines
+/// inserted by the indexing process.
fn readBody(entry_name: []const u8, contents: *util.CharBuffer) !void {
const file = try fs.cwd().openFile(entry_name, .{});
defer file.close();
@@ 42,12 42,11 @@ fn readBody(entry_name: []const u8, contents: *util.CharBuffer) !void {
}
return err;
};
- if (maybe_line) |line| {
- if (line.len > util.ref_prefix.len and mem.eql(
- u8,
- util.ref_prefix,
- line[0..util.ref_prefix.len],
- )) {
+ if (maybe_line == null) {
+ break;
+ } else |line| {
+ const line_prefix = line[0..util.ref_prefix.len];
+ if (line.len > util.ref_prefix.len and mem.eql(u8, util.ref_prefix, line_prefix)) {
// Filter out a ref line.
continue;
}
@@ 59,48 58,28 @@ fn readBody(entry_name: []const u8, contents: *util.CharBuffer) !void {
started_body = true;
if (line.len == 0) {
- // If we are in the note body and encounter an empty line, fill
- // the newline buffer.
+ // If we are in the note body and encounter an empty line,
+ // fill the newline buffer.
newline_counter += 1;
} else {
- // If we encounter a non-empty line, we're still in the note
- // body, so be sure to flush the newline buffer.
+ // If we encounter a non-empty line, we're still in the
+ // note body, so be sure to flush the newline buffer.
try contents.appendNTimes('\n', newline_counter);
newline_counter = 0;
try contents.appendSlice(line);
try contents.append('\n');
}
- } else {
- break;
}
}
}
-pub fn main() !void {
- var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
- defer arena.deinit();
- const allocator = arena.allocator();
-
- // Process CLI args.
- var maybe_directory_name: ?[]const u8 = null;
- var args = try std.process.argsWithAllocator(allocator);
- // Skip the program name.
- _ = args.skip();
- while (args.next()) |arg| {
- if (mem.eql(u8, arg, "-d")) {
- debug_mode = true;
- } else if (mem.eql(u8, arg, "-h")) {
- std.debug.print(help_text, .{});
- std.os.exit(0);
- } else if (maybe_directory_name == null) {
- maybe_directory_name = arg;
- }
- }
- args.deinit();
-
- const directory_name = maybe_directory_name orelse ".";
+fn handleHelp() void {
+ std.debug.print(help_text, .{});
+ std.os.exit(0);
+}
+pub fn handle(directory_name: []const u8, allocator: mem.Allocator) !void {
var notes = std.ArrayList(Note).init(allocator);
defer notes.deinit();
@@ 157,3 136,30 @@ pub fn main() !void {
try fs.cwd().writeFile(path, buf.items);
}
}
+
+pub fn main() !void {
+ var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
+ defer arena.deinit();
+ const allocator = arena.allocator();
+
+ // Process CLI args.
+ var maybe_directory_name: ?[]const u8 = null;
+ var args = try std.process.argsWithAllocator(allocator);
+
+ // Skip the program name.
+ _ = args.skip();
+ while (args.next()) |arg| {
+ if (mem.eql(u8, arg, "-h")) {
+ handleHelp();
+ }
+
+ if (mem.eql(u8, arg, "-d")) {
+ debug_mode = true;
+ } else if (maybe_directory_name == null) {
+ maybe_directory_name = arg;
+ }
+ }
+ args.deinit();
+
+ try handle(maybe_directory_name orelse ".", allocator);
+}
M src/note.zig => src/note.zig +11 -10
@@ 1,12 1,12 @@
const std = @import("std");
-const util = @import("./util.zig");
+const util = @import("util.zig");
const mem = std.mem;
const fmt = std.fmt;
const EscapeError = error{ TerminalPunctuation, OutOfMemory };
-// Process a string and escape any delimiters for reference creation.
+/// Process a string and escape any delimiters for reference creation.
fn escapeAndLower(s: []const u8, allocator: mem.Allocator) !util.CharBuffer {
// Check for illegal characters. Right now the only illegal character is a
// trailing punctuation char, which confuses vim, so this is a bit
@@ 53,6 53,7 @@ fn handleEscapeError(err: EscapeError) void {
},
}
}
+
const BufferAndBraces = struct {
buffer: util.CharBuffer,
offset: usize,
@@ 87,7 88,7 @@ const BufferAndBraces = struct {
const begin_braces = '{';
const end_braces = '}';
-// Create a reference line and add it to the note.
+/// Create a reference line and add it to the note.
fn appendRef(buf: *util.CharBuffer, referent: []const u8) !void {
const capacity = util.ref_prefix.len + 1 + referent.len + 1;
try buf.ensureUnusedCapacity(capacity);
@@ 123,8 124,8 @@ pub const Note = struct {
self.references.deinit();
}
- // Iterate through the rest of the notes populate self.backlinks with names
- // of notes containing "{<self.name>}"
+ /// Iterate through the rest of the notes populate self.backlinks with
+ /// names of notes containing "{<self.name>}"
pub fn populateBacklinks(
self: *@This(),
notes: std.ArrayList(Note),
@@ 151,8 152,8 @@ pub const Note = struct {
}
}
- // Iterate through the note body and populate self.references with subslices
- // capturing "{<reference>}"
+ /// Iterate through the note body and populate self.references with
+ /// subslices capturing "{<reference>}"
pub fn populateReferences(self: *@This(), allocator: mem.Allocator) !void {
var brace_context: ?BufferAndBraces = null;
@@ 208,8 209,8 @@ pub const Note = struct {
}
}
- // Backlinks: if any other note body refers to this one, add a reference to
- // that body's name.
+ /// Backlinks: if any other note body refers to this one, add a reference
+ /// to that body's name.
fn appendBacklinks(self: *@This(), buf: *util.CharBuffer) !void {
var has_a_backlink = false;
@@ 228,7 229,7 @@ pub const Note = struct {
try buf.appendSlice(self.body.items);
}
- // References: gather every reference in the body of the note.
+ /// References: gather every reference in the body of the note.
pub fn appendReferences(
self: *@This(),
buf: *util.CharBuffer,