Day 11: make things neat

Signed-off-by: Aaron Fischer <mail@aaron-fischer.net>
This commit is contained in:
Aaron Fischer 2024-12-11 23:42:07 +01:00
parent 428a249ccc
commit 9f14073d61
2 changed files with 21 additions and 6 deletions

View file

@ -40,7 +40,9 @@ pub fn main() !void {
if (eql(u8, firstArg, "list")) { if (eql(u8, firstArg, "list")) {
listEntries(); listEntries();
} else if (eql(u8, firstArg, "create")) { } else if (eql(u8, firstArg, "create")) {
try createNewEntry(); createNewEntry() catch |err| {
write(Target.Stderr, "error while creating new entry: {any}\n", .{err});
};
} else { } else {
write(Target.Stderr, "Unknown subcommand: {any}\n", .{firstArg}); write(Target.Stderr, "Unknown subcommand: {any}\n", .{firstArg});
} }
@ -48,7 +50,12 @@ pub fn main() !void {
fn createNewEntry() !void { fn createNewEntry() !void {
// Open the file in append mode // Open the file in append mode
const file = try std.fs.cwd().openFile("entries.log.gmi", .{ .mode = .read_write }); const file = std.fs.cwd().openFile("entries.log.gmi", .{
.mode = .read_write
}) catch |err| {
write(Target.Stderr, "unable to open file: {any}\n", .{err});
return;
};
defer file.close(); defer file.close();
try file.seekFromEnd(0); try file.seekFromEnd(0);
@ -65,13 +72,19 @@ fn createNewEntry() !void {
// Read the input content from STDIN and append it to the file // Read the input content from STDIN and append it to the file
var buf: [1024]u8 = undefined; var buf: [1024]u8 = undefined;
while (try stdin.readUntilDelimiterOrEof(&buf, '\n')) |line| {
try file.writeAll("\n");
try file.writeAll(date); try file.writeAll(date);
try file.writeAll(line);
while (stdin.readUntilDelimiterOrEof(&buf, '\n')) |line| {
try file.writeAll(line orelse break);
try file.writeAll("\n"); try file.writeAll("\n");
} else |err| {
write(Target.Stderr, "error while reading from stdin: {any}\n", .{err});
return;
} }
try file.writeAll("\n");
// Print some success message and the back link to STDOUT // Print some success message and the back link to STDOUT
try stdout.print("Entry added successfully\n", .{}); try stdout.print("Entry added successfully\n", .{});
try stdout.print("=> / Back to the log entries", .{}); try stdout.print("=> / Back to the log entries", .{});

View file

@ -1,5 +1,7 @@
# Server log # Server log
=> /create New entry
## 2024-12-04 23:34 UTC ## 2024-12-04 23:34 UTC
This is a simple entry, with multiple lines of text. This should showcase how this is intended. This is a simple entry, with multiple lines of text. This should showcase how this is intended.