Skip to content

Commit 5a58028

Browse files
committed
fix(interactive): Add cross-platform stdin reading support for Windows
- Implement Windows-specific stdin reading using Windows API - Add conditional compilation for Windows and Unix-like systems - Handle file reading differently for Windows and POSIX systems - Ensure consistent input reading behavior across platforms - Add error handling for Windows file reading operations
1 parent 3987c80 commit 5a58028

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/interactive.zig

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,42 @@ fn freeConfig(allocator: std.mem.Allocator, config: *const InteractiveConfig) vo
407407
}
408408

409409
fn readUserInput(allocator: std.mem.Allocator) ![]u8 {
410-
var buffer: [1024]u8 = undefined;
411-
const bytes_read = try std.posix.read(@as(std.posix.fd_t, 0), buffer[0..]);
412-
413-
if (bytes_read > 0) {
414-
// Remove trailing newline if present
415-
const end = if (buffer[bytes_read - 1] == '\n') bytes_read - 1 else bytes_read;
416-
return try allocator.dupe(u8, buffer[0..end]);
410+
const builtin = @import("builtin");
411+
412+
if (builtin.os.tag == .windows) {
413+
// Windows-specific implementation
414+
const stdin_handle = std.os.windows.GetStdHandle(std.os.windows.STD_INPUT_HANDLE) catch return error.StdinUnavailable;
415+
var buffer: [1024]u8 = undefined;
416+
var bytes_read: std.os.windows.DWORD = undefined;
417+
418+
const success = std.os.windows.kernel32.ReadFile(
419+
stdin_handle,
420+
buffer.ptr,
421+
buffer.len,
422+
&bytes_read,
423+
null,
424+
);
425+
426+
if (success == 0) return error.ReadFailed;
427+
428+
if (bytes_read > 0) {
429+
// Remove trailing newline if present
430+
const end = if (buffer[bytes_read - 1] == '\n') bytes_read - 1 else bytes_read;
431+
return try allocator.dupe(u8, buffer[0..end]);
432+
} else {
433+
return try allocator.dupe(u8, "");
434+
}
417435
} else {
418-
return try allocator.dupe(u8, "");
436+
// Unix-like systems
437+
var buffer: [1024]u8 = undefined;
438+
const bytes_read = try std.posix.read(@as(std.posix.fd_t, 0), buffer[0..]);
439+
440+
if (bytes_read > 0) {
441+
// Remove trailing newline if present
442+
const end = if (buffer[bytes_read - 1] == '\n') bytes_read - 1 else bytes_read;
443+
return try allocator.dupe(u8, buffer[0..end]);
444+
} else {
445+
return try allocator.dupe(u8, "");
446+
}
419447
}
420448
}

0 commit comments

Comments
 (0)