Skip to content

Commit 799206a

Browse files
committed
std.Progress: support progress bar escape codes
1 parent 869ef00 commit 799206a

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

lib/std/Progress.zig

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,9 @@ const save = "\x1b7";
678678
const restore = "\x1b8";
679679
const finish_sync = "\x1b[?2026l";
680680

681+
const progress_remove = "\x1b]9;4;0\x07";
682+
const progress_pulsing = "\x1b]9;4;3\x07";
683+
681684
const TreeSymbol = enum {
682685
/// ├─
683686
tee,
@@ -760,7 +763,7 @@ fn clearWrittenWithEscapeCodes() anyerror!void {
760763
if (noop_impl or !global_progress.need_clear) return;
761764

762765
global_progress.need_clear = false;
763-
try write(clear);
766+
try write(clear ++ progress_remove);
764767
}
765768

766769
/// U+25BA or ►
@@ -1203,6 +1206,20 @@ fn computeRedraw(serialized_buffer: *Serialized.Buffer) struct { []u8, usize } {
12031206
i, const nl_n = computeNode(buf, i, 0, serialized, children, root_node_index);
12041207

12051208
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+
12061223
if (nl_n > 0) {
12071224
buf[i] = '\r';
12081225
i += 1;

0 commit comments

Comments
 (0)