Skip to content

Commit 1ebc41c

Browse files
chrisbbreuerclaude
andcommitted
fix: correct Zig 0.15.1 API usage (std.fs.File.stdin/stdout/stderr)
Use std.fs.File.stdin()/stdout()/stderr() instead of std.io.getStdIn()/getStdOut()/getStdErr(). Fix ProgressBar.zig formatting. Use bufPrint + writeAll pattern for output. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8fe912b commit 1ebc41c

File tree

5 files changed

+87
-60
lines changed

5 files changed

+87
-60
lines changed

src/cli/Help.zig

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,70 +11,80 @@ pub fn init(allocator: std.mem.Allocator) Help {
1111

1212
pub fn generate(self: *Help, command: *Command, cli_name: []const u8, version: []const u8) !void {
1313
_ = self;
14-
const stdout = std.io.getStdOut().writer();
14+
const stdout = std.fs.File.stdout();
1515

1616
// Header
17-
try stdout.print("\n{s} v{s}\n", .{ cli_name, version });
18-
try stdout.print("{s}\n\n", .{command.description});
17+
var buf: [4096]u8 = undefined;
18+
var line = try std.fmt.bufPrint(&buf, "\n{s} v{s}\n", .{ cli_name, version });
19+
try stdout.writeAll(line);
20+
line = try std.fmt.bufPrint(&buf, "{s}\n\n", .{command.description});
21+
try stdout.writeAll(line);
1922

2023
// Usage
21-
try stdout.print("USAGE:\n", .{});
22-
try stdout.print(" {s}", .{command.name});
24+
try stdout.writeAll("USAGE:\n");
25+
line = try std.fmt.bufPrint(&buf, " {s}", .{command.name});
26+
try stdout.writeAll(line);
2327

2428
if (command.options.items.len > 0) {
25-
try stdout.print(" [OPTIONS]", .{});
29+
try stdout.writeAll(" [OPTIONS]");
2630
}
2731

2832
if (command.subcommands.items.len > 0) {
29-
try stdout.print(" <COMMAND>", .{});
33+
try stdout.writeAll(" <COMMAND>");
3034
}
3135

3236
for (command.arguments.items) |arg| {
3337
if (arg.required) {
34-
try stdout.print(" <{s}>", .{arg.name});
38+
line = try std.fmt.bufPrint(&buf, " <{s}>", .{arg.name});
39+
try stdout.writeAll(line);
3540
} else {
36-
try stdout.print(" [{s}]", .{arg.name});
41+
line = try std.fmt.bufPrint(&buf, " [{s}]", .{arg.name});
42+
try stdout.writeAll(line);
3743
}
3844
if (arg.variadic) {
39-
try stdout.print("...", .{});
45+
try stdout.writeAll("...");
4046
}
4147
}
42-
try stdout.print("\n\n", .{});
48+
try stdout.writeAll("\n\n");
4349

4450
// Arguments
4551
if (command.arguments.items.len > 0) {
46-
try stdout.print("ARGUMENTS:\n", .{});
52+
try stdout.writeAll("ARGUMENTS:\n");
4753
for (command.arguments.items) |arg| {
48-
try stdout.print(" <{s}>", .{arg.name});
54+
line = try std.fmt.bufPrint(&buf, " <{s}>", .{arg.name});
55+
try stdout.writeAll(line);
4956
if (arg.variadic) {
50-
try stdout.print("...", .{});
57+
try stdout.writeAll("...");
5158
}
5259
const padding = 20 -| (arg.name.len + 2 + if (arg.variadic) @as(usize, 3) else @as(usize, 0));
53-
for (0..padding) |_| try stdout.writeByte(' ');
54-
try stdout.print("{s}", .{arg.description});
60+
for (0..padding) |_| try stdout.writeAll(" ");
61+
line = try std.fmt.bufPrint(&buf, "{s}", .{arg.description});
62+
try stdout.writeAll(line);
5563
if (!arg.required) {
56-
try stdout.print(" (optional)", .{});
64+
try stdout.writeAll(" (optional)");
5765
}
58-
try stdout.print("\n", .{});
66+
try stdout.writeAll("\n");
5967
}
60-
try stdout.print("\n", .{});
68+
try stdout.writeAll("\n");
6169
}
6270

6371
// Options
6472
if (command.options.items.len > 0) {
65-
try stdout.print("OPTIONS:\n", .{});
73+
try stdout.writeAll("OPTIONS:\n");
6674
for (command.options.items) |opt| {
6775
var length: usize = 0;
6876

6977
if (opt.short) |s| {
70-
try stdout.print(" -{c}, ", .{s});
78+
line = try std.fmt.bufPrint(&buf, " -{c}, ", .{s});
79+
try stdout.writeAll(line);
7180
length += 6;
7281
} else {
73-
try stdout.print(" ", .{});
82+
try stdout.writeAll(" ");
7483
length += 6;
7584
}
7685

77-
try stdout.print("--{s}", .{opt.long});
86+
line = try std.fmt.bufPrint(&buf, "--{s}", .{opt.long});
87+
try stdout.writeAll(line);
7888
length += opt.long.len + 2;
7989

8090
if (opt.option_type != .bool) {
@@ -84,40 +94,46 @@ pub fn generate(self: *Help, command: *Command, cli_name: []const u8, version: [
8494
.float => "<FLOAT>",
8595
.bool => "",
8696
};
87-
try stdout.print(" {s}", .{type_name});
97+
line = try std.fmt.bufPrint(&buf, " {s}", .{type_name});
98+
try stdout.writeAll(line);
8899
length += type_name.len + 1;
89100
}
90101

91102
const padding = 30 -| length;
92-
for (0..padding) |_| try stdout.writeByte(' ');
103+
for (0..padding) |_| try stdout.writeAll(" ");
93104

94-
try stdout.print("{s}", .{opt.description});
105+
line = try std.fmt.bufPrint(&buf, "{s}", .{opt.description});
106+
try stdout.writeAll(line);
95107

96108
if (opt.required) {
97-
try stdout.print(" (required)", .{});
109+
try stdout.writeAll(" (required)");
98110
} else if (opt.default_value) |default| {
99-
try stdout.print(" (default: {s})", .{default});
111+
line = try std.fmt.bufPrint(&buf, " (default: {s})", .{default});
112+
try stdout.writeAll(line);
100113
}
101114

102-
try stdout.print("\n", .{});
115+
try stdout.writeAll("\n");
103116
}
104117

105-
try stdout.print(" -h, --help", .{});
106-
for (0..20) |_| try stdout.writeByte(' ');
107-
try stdout.print("Print help\n", .{});
108-
try stdout.print("\n", .{});
118+
try stdout.writeAll(" -h, --help");
119+
for (0..20) |_| try stdout.writeAll(" ");
120+
try stdout.writeAll("Print help\n");
121+
try stdout.writeAll("\n");
109122
}
110123

111124
// Commands
112125
if (command.subcommands.items.len > 0) {
113-
try stdout.print("COMMANDS:\n", .{});
126+
try stdout.writeAll("COMMANDS:\n");
114127
for (command.subcommands.items) |subcmd| {
115-
try stdout.print(" {s}", .{subcmd.name});
128+
line = try std.fmt.bufPrint(&buf, " {s}", .{subcmd.name});
129+
try stdout.writeAll(line);
116130
const padding = 20 -| subcmd.name.len;
117-
for (0..padding) |_| try stdout.writeByte(' ');
118-
try stdout.print("{s}\n", .{subcmd.description});
131+
for (0..padding) |_| try stdout.writeAll(" ");
132+
line = try std.fmt.bufPrint(&buf, "{s}\n", .{subcmd.description});
133+
try stdout.writeAll(line);
119134
}
120-
try stdout.print("\n", .{});
121-
try stdout.print("Run '{s} <COMMAND> --help' for more information on a command.\n\n", .{command.name});
135+
try stdout.writeAll("\n");
136+
line = try std.fmt.bufPrint(&buf, "Run '{s} <COMMAND> --help' for more information on a command.\n\n", .{command.name});
137+
try stdout.writeAll(line);
122138
}
123139
}

src/cli/Middleware.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ pub const MiddlewareChain = struct {
9292

9393
/// Logging middleware
9494
pub fn loggingMiddleware(ctx: *MiddlewareContext) !bool {
95-
const stdout = std.io.getStdOut().writer();
96-
try stdout.print("[LOG] Executing command: {s}\n", .{ctx.command.name});
95+
const stdout = std.fs.File.stdout();
96+
var buf: [256]u8 = undefined;
97+
const line = try std.fmt.bufPrint(&buf, "[LOG] Executing command: {s}\n", .{ctx.command.name});
98+
try stdout.writeAll(line);
9799
return true;
98100
}
99101

@@ -113,8 +115,10 @@ pub fn validationMiddleware(ctx: *MiddlewareContext) !bool {
113115
// Check if all required options are present
114116
for (ctx.command.options.items) |opt| {
115117
if (opt.required and !ctx.parse_context.hasOption(opt.name)) {
116-
const stderr = std.io.getStdErr().writer();
117-
try stderr.print("Error: Missing required option '--{s}'\n", .{opt.long});
118+
const stderr = std.fs.File.stderr();
119+
var buf: [256]u8 = undefined;
120+
const line = try std.fmt.bufPrint(&buf, "Error: Missing required option '--{s}'\n", .{opt.long});
121+
try stderr.writeAll(line);
118122
return false;
119123
}
120124
}

src/prompt/ProgressBar.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ const Ansi = @import("Ansi.zig");
55
const ProgressBar = @This();
66

77
pub const ProgressBarStyle = enum {
8-
bar, // [=====> ]
9-
blocks, // ████████░░░
10-
dots, // ⣿⣿⣿⣿⣿⣀⣀⣀⣀⣀
11-
ascii, // [###> ]
8+
bar, // [=====> ]
9+
blocks, // ████████░░░
10+
dots, // ⣿⣿⣿⣿⣿⣀⣀⣀⣀⣀
11+
ascii, // [###> ]
1212
};
1313

1414
terminal: Terminal,

src/prompt/Style.zig

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,45 +176,52 @@ pub const Style = struct {
176176
// Build ANSI code sequence
177177
var codes: std.ArrayList(u8) = .{};
178178
defer codes.deinit(self.allocator);
179+
var tmp_buf: [16]u8 = undefined;
179180

180181
var first = true;
181182

182183
// Styles
183184
if (self.bold_enabled) {
184185
if (!first) try codes.append(self.allocator, ';');
185-
try codes.print(self.allocator, "{d}", .{Ansi.Style.bold.toCode()});
186+
const s = try std.fmt.bufPrint(&tmp_buf, "{d}", .{Ansi.Style.bold.toCode()});
187+
try codes.appendSlice(self.allocator, s);
186188
first = false;
187189
}
188190

189191
if (self.dim_enabled) {
190192
if (!first) try codes.append(self.allocator, ';');
191-
try codes.print(self.allocator, "{d}", .{Ansi.Style.dim.toCode()});
193+
const s = try std.fmt.bufPrint(&tmp_buf, "{d}", .{Ansi.Style.dim.toCode()});
194+
try codes.appendSlice(self.allocator, s);
192195
first = false;
193196
}
194197

195198
if (self.italic_enabled) {
196199
if (!first) try codes.append(self.allocator, ';');
197-
try codes.print(self.allocator, "{d}", .{Ansi.Style.italic.toCode()});
200+
const s = try std.fmt.bufPrint(&tmp_buf, "{d}", .{Ansi.Style.italic.toCode()});
201+
try codes.appendSlice(self.allocator, s);
198202
first = false;
199203
}
200204

201205
if (self.underline_enabled) {
202206
if (!first) try codes.append(self.allocator, ';');
203-
try codes.print(self.allocator, "{d}", .{Ansi.Style.underline.toCode()});
207+
const s = try std.fmt.bufPrint(&tmp_buf, "{d}", .{Ansi.Style.underline.toCode()});
208+
try codes.appendSlice(self.allocator, s);
204209
first = false;
205210
}
206211

207212
// Foreground color
208213
if (self.color) |color| {
209214
if (!first) try codes.append(self.allocator, ';');
210-
try codes.print(self.allocator, "{d}", .{color.toCode()});
215+
const s = try std.fmt.bufPrint(&tmp_buf, "{d}", .{color.toCode()});
216+
try codes.appendSlice(self.allocator, s);
211217
first = false;
212218
}
213219

214220
// Background color
215221
if (self.bg_color) |bg| {
216222
if (!first) try codes.append(self.allocator, ';');
217-
try codes.print(self.allocator, "{d}", .{bg.toCode() + 10}); // BG is FG + 10
223+
const s = try std.fmt.bufPrint(&tmp_buf, "{d}", .{bg.toCode() + 10}); // BG is FG + 10
224+
try codes.appendSlice(self.allocator, s);
218225
first = false;
219226
}
220227

@@ -231,7 +238,7 @@ pub const Style = struct {
231238
const styled = try self.render();
232239
defer self.allocator.free(styled);
233240

234-
const stdout = std.io.getStdOut();
241+
const stdout = std.fs.File.stdout();
235242
try stdout.writeAll(styled);
236243
}
237244

@@ -240,7 +247,7 @@ pub const Style = struct {
240247
const styled = try self.render();
241248
defer self.allocator.free(styled);
242249

243-
const stdout = std.io.getStdOut();
250+
const stdout = std.fs.File.stdout();
244251
try stdout.writeAll(styled);
245252
try stdout.writeAll("\n");
246253
}

src/prompt/Terminal.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub const RawMode = struct {
1212
// Windows terminal setup would go here
1313
return RawMode{ .original_termios = {} };
1414
} else {
15-
const stdin = std.io.getStdIn();
15+
const stdin = std.fs.File.stdin();
1616
const original = try posix.tcgetattr(stdin.handle);
1717
var raw = original;
1818

@@ -35,7 +35,7 @@ pub const RawMode = struct {
3535
if (builtin.os.tag == .windows) {
3636
return;
3737
}
38-
const stdin = std.io.getStdIn();
38+
const stdin = std.fs.File.stdin();
3939
posix.tcsetattr(stdin.handle, .FLUSH, self.original_termios) catch {};
4040
}
4141
};
@@ -76,8 +76,8 @@ pub fn init() Terminal {
7676
const size = detectTerminalSize();
7777

7878
return .{
79-
.stdin = std.io.getStdIn(),
80-
.stdout = std.io.getStdOut(),
79+
.stdin = std.fs.File.stdin(),
80+
.stdout = std.fs.File.stdout(),
8181
.supports_unicode = supports_unicode,
8282
.supports_color = supports_color,
8383
.width = size.width,
@@ -132,7 +132,7 @@ fn detectTerminalSize() TerminalSize {
132132
}
133133

134134
// Try to get terminal size via ioctl
135-
const stdout = std.io.getStdOut();
135+
const stdout = std.fs.File.stdout();
136136
var ws: Winsize = undefined;
137137

138138
const result = posix.system.ioctl(stdout.handle, posix.T.IOCGWINSZ, @intFromPtr(&ws));

0 commit comments

Comments
 (0)