From 578ad17a5394615af9f50467c8156196beb3980a Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Tue, 10 Dec 2024 16:20:42 +0100 Subject: [PATCH] Day 10: Add creating entries Signed-off-by: Aaron Fischer --- src/main.zig | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main.zig b/src/main.zig index e889f6b..a072ac4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,6 +3,7 @@ const builtin = @import("builtin"); const eql = @import("std").mem.eql; const stdout = std.io.getStdOut().writer(); +const stdin = std.io.getStdIn().reader(); pub fn main() !void { // Read the first argument to determine the subcommand @@ -27,7 +28,34 @@ pub fn main() !void { } fn createNewEntry() !void { - // TODO + // Open the file in append mode + const file = try std.fs.cwd().openFile("entries.log.gmi", .{ .mode = .read_write }); + defer file.close(); + try file.seekFromEnd(0); + + //const date = try std.time.Time.nowUTC().format("yyyy-MM-dd HH:mm UTC"); + const es = std.time.epoch.EpochSeconds{ .secs = @intCast(std.time.timestamp()) }; + const ed = std.time.epoch.EpochDay{ .day = es.getEpochDay().day }; + var buf2: [256]u8 = undefined; + const date = try std.fmt.bufPrint(&buf2, "## {d}-{d:0>2}-{d:0>2} {d:0>2}:{d:0>2} UTC\n", .{ + ed.calculateYearDay().year, + std.time.epoch.YearAndDay.calculateMonthDay(ed.calculateYearDay()).month.numeric(), + std.time.epoch.YearAndDay.calculateMonthDay(ed.calculateYearDay()).day_index, + es.getDaySeconds().getHoursIntoDay(), + es.getDaySeconds().getMinutesIntoHour(),}); + + // Read the input content from STDIN and append it to the file + var buf: [1024]u8 = undefined; + while (try stdin.readUntilDelimiterOrEof(&buf, '\n')) |line| { + try file.writeAll("\n"); + try file.writeAll(date); + try file.writeAll(line); + try file.writeAll("\n"); + } + + // Print some success message and the back link to STDOUT + try stdout.print("Entry added successfully\n", .{}); + try stdout.print("=> / Back to the log entries", .{}); } fn listEntries() !void {