Skip to content

Commit

Permalink
chore: Add currently available Windows code
Browse files Browse the repository at this point in the history
Zig 0.12.0 doesn't have the SetConsoleMode function implemented in the
standard library, but this PR - which was accepted recently
(ziglang/zig#18692) adds it. So, I just have to
wait (don't want to use Zig master)
  • Loading branch information
nrdave committed May 14, 2024
1 parent 1810fc0 commit 2673e1b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/terminal_mode_control.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ const builtin = @import("builtin");

const is_windows = builtin.os.tag == .windows;

const TermStatus = if (is_windows) std.windows.DWORD else std.posix.termios;
const TermStatus = if (is_windows) std.os.windows.DWORD else std.posix.termios;

pub fn enableRawMode(input: std.fs.File.Handle) !TermStatus {
if (is_windows) {
@compileError("Windows support not ready");
var orig_mode: TermStatus = undefined;
_ = std.os.windows.kernel32.GetConsoleMode(input, &orig_mode);
var t = orig_mode;
// This line clears the following flags
// 0x04: ENABLE_ECHO_INPUT
// 0x02: ENABLE_LINE_INPUT
// It also sets the ENABLE_VIRTUAL_TERMINAL_INPUT flag 0x200
t = t & ~(0x04 | 0x02) | 0x200;
@compileError("Windows Support not ready - waiting for Zig 0.13.0");
} else {
const orig_termios = try std.posix.tcgetattr(input);
var t = orig_termios;
Expand All @@ -21,7 +29,7 @@ pub fn enableRawMode(input: std.fs.File.Handle) !TermStatus {

pub fn restoreTermMode(input: std.fs.File.Handle, original_term_status: TermStatus) !void {
if (is_windows) {
@compileError("Windows support not ready");
@compileError("Windows Support not ready - waiting for Zig 0.13.0");
} else {
try std.posix.tcsetattr(input, std.posix.TCSA.FLUSH, original_term_status);
}
Expand Down

0 comments on commit 2673e1b

Please sign in to comment.