From 379cdbd946dfb035e61f96653db618cba4906864 Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Fri, 5 Dec 2025 05:36:01 -0300 Subject: codecrafters submit [skip ci] --- src/builtins.zig | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/builtins.zig') diff --git a/src/builtins.zig b/src/builtins.zig index 18e1840..a0d844b 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -122,6 +122,15 @@ pub fn executeHistory(stdout: anytype, history_list: []const []const u8, args: ? if (args) |a| { const trimmed = std.mem.trim(u8, a, " "); if (trimmed.len > 0) { + // Check if it's a -r flag for reading from file + if (std.mem.startsWith(u8, trimmed, "-r ")) { + const file_path = std.mem.trim(u8, trimmed[3..], " "); + if (file_path.len == 0) { + try stdout.print("history: -r requires a file path\n", .{}); + } + return; + } + limit = std.fmt.parseInt(usize, trimmed, 10) catch { try stdout.print("history: invalid argument\n", .{}); return; @@ -134,3 +143,28 @@ pub fn executeHistory(stdout: anytype, history_list: []const []const u8, args: ? try stdout.print(" {d} {s}\n", .{ idx, cmd }); } } + +pub fn executeHistoryRead(allocator: std.mem.Allocator, stdout: anytype, file_path: []const u8, history_list: *std.ArrayList([]const u8)) !void { + const file = std.fs.cwd().openFile(file_path, .{}) catch { + try stdout.print("history: cannot open {s}: error\n", .{file_path}); + return; + }; + defer file.close(); + + const file_size = try file.getEndPos(); + const buffer = try allocator.alloc(u8, file_size); + defer allocator.free(buffer); + + const bytes_read = try file.readAll(buffer); + if (bytes_read == 0) return; + + // Parse lines from file + var line_iter = std.mem.splitScalar(u8, buffer[0..bytes_read], '\n'); + while (line_iter.next()) |line| { + const trimmed = std.mem.trim(u8, line, " \r"); + if (trimmed.len > 0) { + const line_copy = try allocator.dupe(u8, trimmed); + try history_list.append(allocator, line_copy); + } + } +} -- cgit v1.2.3