Skip to content

Commit

Permalink
std.Progress: Fix Windows console API implementation
Browse files Browse the repository at this point in the history
3a3d218 unintentionally broke some of the Windows console API implementation.

- The 'marker' character was no longer being written at all
- The ANSI escape codes for syncing were being written unconditionally
  • Loading branch information
squeek502 authored and andrewrk committed May 29, 2024
1 parent 6b020c3 commit d750a78
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/std/Progress.zig
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,14 @@ fn updateThreadRun() void {
}
}

fn windowsApiWriteMarker() void {
// Write the marker that we will use to find the beginning of the progress when clearing.
// Note: This doesn't have to use WriteConsoleW, but doing so avoids dealing with the code page.
var num_chars_written: windows.DWORD = undefined;
const handle = global_progress.terminal.handle;
_ = windows.kernel32.WriteConsoleW(handle, &[_]u16{windows_api_start_marker}, 1, &num_chars_written, null);
}

fn windowsApiUpdateThreadRun() void {
var serialized_buffer: Serialized.Buffer = undefined;

Expand All @@ -483,6 +491,7 @@ fn windowsApiUpdateThreadRun() void {
const buffer = computeRedraw(&serialized_buffer);
if (stderr_mutex.tryLock()) {
defer stderr_mutex.unlock();
windowsApiWriteMarker();
write(buffer) catch return;
}
}
Expand All @@ -502,6 +511,7 @@ fn windowsApiUpdateThreadRun() void {
if (stderr_mutex.tryLock()) {
defer stderr_mutex.unlock();
clearWrittenWindowsApi() catch return;
windowsApiWriteMarker();
write(buffer) catch return;
}
}
Expand Down Expand Up @@ -1048,8 +1058,10 @@ fn computeRedraw(serialized_buffer: *Serialized.Buffer) []u8 {
var i: usize = 0;
const buf = global_progress.draw_buffer;

buf[i..][0..start_sync.len].* = start_sync.*;
i += start_sync.len;
if (global_progress.terminal_mode == .ansi_escape_codes) {
buf[i..][0..start_sync.len].* = start_sync.*;
i += start_sync.len;
}

switch (global_progress.terminal_mode) {
.off => unreachable,
Expand All @@ -1061,8 +1073,10 @@ fn computeRedraw(serialized_buffer: *Serialized.Buffer) []u8 {
const root_node_index: Node.Index = @enumFromInt(0);
i = computeNode(buf, i, serialized, children, root_node_index);

buf[i..][0..finish_sync.len].* = finish_sync.*;
i += finish_sync.len;
if (global_progress.terminal_mode == .ansi_escape_codes) {
buf[i..][0..finish_sync.len].* = finish_sync.*;
i += finish_sync.len;
}

return buf[0..i];
}
Expand Down

0 comments on commit d750a78

Please sign in to comment.