diff options
| author | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 08:26:10 +0000 |
|---|---|---|
| committer | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 08:26:10 +0000 |
| commit | 2cce22519a7cbade6d9e7ec2b244a672ec8bbed9 (patch) | |
| tree | 11177f4caa550b2405990b05a9648fcaf7d07bb3 /src/builtins.zig | |
| parent | b906f7b64b010cae0b793419b9aa0af9d038b653 (diff) | |
| download | shell-zig-2cce22519a7cbade6d9e7ec2b244a672ec8bbed9.tar.gz shell-zig-2cce22519a7cbade6d9e7ec2b244a672ec8bbed9.zip | |
codecrafters submit [skip ci]
Diffstat (limited to 'src/builtins.zig')
| -rw-r--r-- | src/builtins.zig | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/builtins.zig b/src/builtins.zig index 011a923..18e1840 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -116,8 +116,21 @@ pub fn executeType(allocator: std.mem.Allocator, stdout: anytype, args: ?[]const } } -pub fn executeHistory(stdout: anytype, history_list: []const []const u8) !void { - for (history_list, 1..) |cmd, idx| { +pub fn executeHistory(stdout: anytype, history_list: []const []const u8, args: ?[]const u8) !void { + var limit: usize = history_list.len; + + if (args) |a| { + const trimmed = std.mem.trim(u8, a, " "); + if (trimmed.len > 0) { + limit = std.fmt.parseInt(usize, trimmed, 10) catch { + try stdout.print("history: invalid argument\n", .{}); + return; + }; + } + } + + const start = if (limit < history_list.len) history_list.len - limit else 0; + for (history_list[start..], start + 1..) |cmd, idx| { try stdout.print(" {d} {s}\n", .{ idx, cmd }); } } |