authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-23 17:49:03-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-25 16:00:19-07:00
log799206a3ad68f1c4ddd7d65b04e17da1974a10ee
tree53e98c316b7b42346bd5787bce4dc1f52ae23a4d
parent869ef00602328f97ea8c88358310cbd34d4391ab

std.Progress: support progress bar escape codes


1 files changed, 18 insertions(+), 1 deletions(-)

lib/std/Progress.zig+18-1
...@@ -678,6 +678,9 @@ const save = "\x1b7";...@@ -678,6 +678,9 @@ const save = "\x1b7";
678const restore = "\x1b8";678const restore = "\x1b8";
679const finish_sync = "\x1b[?2026l";679const finish_sync = "\x1b[?2026l";
680680
681const progress_remove = "\x1b]9;4;0\x07";
682const progress_pulsing = "\x1b]9;4;3\x07";
683
681const TreeSymbol = enum {684const TreeSymbol = enum {
682 /// ├─685 /// ├─
683 tee,686 tee,
...@@ -760,7 +763,7 @@ fn clearWrittenWithEscapeCodes() anyerror!void {...@@ -760,7 +763,7 @@ fn clearWrittenWithEscapeCodes() anyerror!void {
760 if (noop_impl or !global_progress.need_clear) return;763 if (noop_impl or !global_progress.need_clear) return;
761764
762 global_progress.need_clear = false;765 global_progress.need_clear = false;
763 try write(clear);766 try write(clear ++ progress_remove);
764}767}
765768
766/// U+25BA or ►769/// U+25BA or ►
...@@ -1203,6 +1206,20 @@ fn computeRedraw(serialized_buffer: *Serialized.Buffer) struct { []u8, usize } {...@@ -1203,6 +1206,20 @@ fn computeRedraw(serialized_buffer: *Serialized.Buffer) struct { []u8, usize } {
1203 i, const nl_n = computeNode(buf, i, 0, serialized, children, root_node_index);1206 i, const nl_n = computeNode(buf, i, 0, serialized, children, root_node_index);
12041207
1205 if (global_progress.terminal_mode == .ansi_escape_codes) {1208 if (global_progress.terminal_mode == .ansi_escape_codes) {
1209 {
1210 // Set progress state https://conemu.github.io/en/AnsiEscapeCodes.html#ConEmu_specific_OSC
1211 const storage = &serialized.storage[@intFromEnum(root_node_index)];
1212 const estimated_total = storage.estimated_total_count;
1213 const completed_items = storage.completed_count;
1214 if (estimated_total == 0) {
1215 buf[i..][0..progress_pulsing.len].* = progress_pulsing.*;
1216 i += progress_pulsing.len;
1217 } else {
1218 const percent = completed_items * 100 / estimated_total;
1219 i += (std.fmt.bufPrint(buf[i..], "\x1b]9;4;1;{d}\x07", .{percent}) catch &.{}).len;
1220 }
1221 }
1222
1206 if (nl_n > 0) {1223 if (nl_n > 0) {
1207 buf[i] = '\r';1224 buf[i] = '\r';
1208 i += 1;1225 i += 1;