2024-12-01 21:18:39 +01:00
|
|
|
const std = @import("std");
|
2024-12-09 22:56:38 +01:00
|
|
|
const builtin = @import("builtin");
|
|
|
|
const eql = @import("std").mem.eql;
|
|
|
|
|
|
|
|
const stdout = std.io.getStdOut().writer();
|
2024-12-11 00:12:39 +01:00
|
|
|
const stderr = std.io.getStdErr().writer();
|
2024-12-10 16:20:42 +01:00
|
|
|
const stdin = std.io.getStdIn().reader();
|
2024-12-01 21:18:39 +01:00
|
|
|
|
2024-12-11 00:12:39 +01:00
|
|
|
const Target = enum {
|
|
|
|
Stdout,
|
|
|
|
Stderr,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn write(target: Target, comptime format: []const u8, args: anytype) void {
|
|
|
|
switch (target) {
|
|
|
|
Target.Stdout => stdout.print(format, args) catch |err| {
|
|
|
|
std.debug.panic("unable to write to output: {any}\n", .{err});
|
|
|
|
},
|
|
|
|
Target.Stderr => stderr.print(format, args) catch |err| {
|
|
|
|
std.debug.panic("unable to write to output: {any}\n", .{err});
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-01 21:18:39 +01:00
|
|
|
pub fn main() !void {
|
2024-12-09 22:56:38 +01:00
|
|
|
// Read the first argument to determine the subcommand
|
|
|
|
const allocator = std.heap.page_allocator;
|
2024-12-11 00:12:39 +01:00
|
|
|
var args = std.process.argsWithAllocator(allocator) catch |err| {
|
|
|
|
write(Target.Stderr, "out of memory: {any}\n", .{err});
|
|
|
|
};
|
2024-12-09 22:56:38 +01:00
|
|
|
defer args.deinit();
|
|
|
|
|
|
|
|
_ = args.next(); // Skip the first argument, which is the program name
|
|
|
|
|
|
|
|
const firstArg = args.next() orelse {
|
2024-12-11 00:12:39 +01:00
|
|
|
write(Target.Stderr, "no subcommand provided\n", .{});
|
2024-12-09 22:56:38 +01:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (eql(u8, firstArg, "list")) {
|
2024-12-11 00:12:39 +01:00
|
|
|
listEntries();
|
2024-12-09 22:56:38 +01:00
|
|
|
} else if (eql(u8, firstArg, "create")) {
|
|
|
|
try createNewEntry();
|
|
|
|
} else {
|
2024-12-11 00:12:39 +01:00
|
|
|
write(Target.Stderr, "Unknown subcommand: {any}\n", .{firstArg});
|
2024-12-01 21:18:39 +01:00
|
|
|
}
|
2024-12-09 22:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn createNewEntry() !void {
|
2024-12-10 16:20:42 +01:00
|
|
|
// 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", .{});
|
2024-12-09 22:56:38 +01:00
|
|
|
}
|
|
|
|
|
2024-12-11 00:12:39 +01:00
|
|
|
fn listEntries() void {
|
|
|
|
const file = std.fs.cwd().openFile("entries.log.gmi", .{}) catch |err| {
|
|
|
|
write(Target.Stderr, "unable to open file: {any}\n", .{err});
|
|
|
|
return;
|
|
|
|
};
|
2024-12-09 22:56:38 +01:00
|
|
|
defer file.close();
|
|
|
|
|
|
|
|
var bufReader = std.io.bufferedReader(file.reader());
|
|
|
|
var inStream = bufReader.reader();
|
|
|
|
|
|
|
|
var buf: [1024]u8 = undefined;
|
2024-12-11 00:12:39 +01:00
|
|
|
|
|
|
|
while (inStream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
|
|
|
|
write(Target.Stdout, "{s}\n", .{ line orelse break });
|
|
|
|
} else |err| {
|
|
|
|
write(Target.Stderr, "error while reading the file: {!}\n", .{err});
|
2024-12-09 22:56:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|