diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2025-08-25 20:05:19 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2025-08-25 20:05:19 +0200 |
commit | 74c8e2baf22c62b8015daf1b86e58fa4bf738bb4 (patch) | |
tree | 3257f5ea40641e07515e1d2b56105b7728a3b274 /src/cmd/lib | |
parent | 41a94a7f354a72af807e25607de65ee98f3d1b63 (diff) |
Update to Zig 0.15.1.
Diffstat (limited to 'src/cmd/lib')
-rw-r--r-- | src/cmd/lib/flags.zig | 30 | ||||
-rw-r--r-- | src/cmd/lib/types.zig | 63 |
2 files changed, 48 insertions, 45 deletions
diff --git a/src/cmd/lib/flags.zig b/src/cmd/lib/flags.zig index 637ea83..266ddfe 100644 --- a/src/cmd/lib/flags.zig +++ b/src/cmd/lib/flags.zig @@ -155,34 +155,34 @@ pub const CommandFlags = struct { } pub fn getHelp(self: *const CommandFlags, command_name: []const u8, allocator: Allocator) ![]const u8 { - var help = ArrayList(u8).init(allocator); - defer help.deinit(); + var help = ArrayList(u8){}; + defer help.deinit(allocator); - try help.appendSlice(command_name); - try help.appendSlice(" - Available flags:\n\n"); + try help.appendSlice(allocator, command_name); + try help.appendSlice(allocator, " - Available flags:\n\n"); for (self.flags) |flag_def| { - try help.appendSlice(" /"); - try help.appendSlice(flag_def.name); + try help.appendSlice(allocator, " /"); + try help.appendSlice(allocator, flag_def.name); if (flag_def.aliases.len > 0) { - try help.appendSlice(" ("); + try help.appendSlice(allocator, " ("); for (flag_def.aliases, 0..) |alias, i| { - if (i > 0) try help.appendSlice(", "); - try help.appendSlice(alias); + if (i > 0) try help.appendSlice(allocator, ", "); + try help.appendSlice(allocator, alias); } - try help.appendSlice(")"); + try help.appendSlice(allocator, ")"); } switch (flag_def.flag_type) { - .String => try help.appendSlice(":value"), - .Number => try help.appendSlice(":number"), + .String => try help.appendSlice(allocator, ":value"), + .Number => try help.appendSlice(allocator, ":number"), .Boolean => {}, } - try help.appendSlice(" - "); - try help.appendSlice(flag_def.description); - try help.appendSlice("\n"); + try help.appendSlice(allocator, " - "); + try help.appendSlice(allocator, flag_def.description); + try help.appendSlice(allocator, "\n"); } return help.toOwnedSlice(); diff --git a/src/cmd/lib/types.zig b/src/cmd/lib/types.zig index 57d370d..2dfb0fa 100644 --- a/src/cmd/lib/types.zig +++ b/src/cmd/lib/types.zig @@ -12,19 +12,21 @@ pub const CommandStatus = union(enum) { pub const OutputCapture = struct { buffer: ArrayList(u8), + allocator: Allocator, pub fn init(allocator: Allocator) OutputCapture { return OutputCapture{ - .buffer = ArrayList(u8).init(allocator), + .buffer = ArrayList(u8){}, + .allocator = allocator, }; } pub fn deinit(self: *OutputCapture) void { - self.buffer.deinit(); + self.buffer.deinit(self.allocator); } pub fn write(self: *OutputCapture, data: []const u8) !void { - try self.buffer.appendSlice(data); + try self.buffer.appendSlice(self.allocator, data); } pub fn getContents(self: *const OutputCapture) []const u8 { @@ -35,7 +37,11 @@ pub const OutputCapture = struct { pub const StdoutOutputCapture = struct { pub fn write(self: *StdoutOutputCapture, data: []const u8) !void { _ = self; - try std.io.getStdOut().writeAll(data); + var stdout_buffer: [1024]u8 = undefined; + var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); + const stdout = &stdout_writer.interface; + try stdout.writeAll(data); + try stdout.flush(); } }; @@ -73,41 +79,32 @@ pub const InputSource = struct { }; pub const StdinInputReader = struct { - pub fn readLine(self: *StdinInputReader, allocator: Allocator) !?[]const u8 { + pub fn readLine(self: *StdinInputReader, buf: []u8) !?[]const u8 { _ = self; - const stdin = std.io.getStdIn().reader(); + const stdin = std.fs.File.stdin(); // Read line from stdin with proper DOS handling - const line = stdin.readUntilDelimiterOrEofAlloc(allocator, '\n', 4096) catch |err| switch (err) { + var reader = stdin.readerStreaming(buf); + const input = std.io.Reader.takeDelimiterExclusive(&reader.interface, '\n') catch |err| switch (err) { + error.EndOfStream => return null, error.StreamTooLong => { - // Line too long, skip to next line - while (true) { - const ch = stdin.readByte() catch return null; - if (ch == '\n') break; - } - return try allocator.dupe(u8, ""); + // Line too long - return empty string + return ""; }, else => return null, }; - if (line) |input| { - // Check for Ctrl+Z (EOF marker) - ASCII 26 - if (input.len == 1 and input[0] == 26) { - allocator.free(input); - return null; // EOF - } - - // Remove trailing \r if present (DOS line endings) - if (input.len > 0 and input[input.len - 1] == '\r') { - const trimmed = try allocator.dupe(u8, input[0 .. input.len - 1]); - allocator.free(input); - return trimmed; - } else { - return input; - } - } else { + // Check for Ctrl+Z (EOF marker) - ASCII 26 + if (input.len == 1 and input[0] == 26) { return null; // EOF } + + // Remove trailing \r if present (DOS line endings) + if (input.len > 0 and input[input.len - 1] == '\r') { + return input[0 .. input.len - 1]; + } else { + return input; + } } }; @@ -118,7 +115,13 @@ pub const InputReader = union(enum) { pub fn readLine(self: *InputReader, allocator: Allocator) !?[]const u8 { switch (self.*) { .source => |source| return source.readLine(allocator), - .stdin => |stdin| return stdin.readLine(allocator), + .stdin => |stdin| { + var buf: [4096]u8 = undefined; + if (try stdin.readLine(&buf)) |line| { + return try allocator.dupe(u8, line); + } + return null; + }, } } }; |