authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-05-27 10:04:00-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-05-27 20:56:48-07:00
log6145819c0ba00924b37bab78200aeab6306c1672
tree3a27b1ec8f3088736a810210518acd54bcf9ec48
parent52ffdec74b5854bc842107f40f9fa31b40cf5432

std.Progress: handle when terminal write buffer too small


1 files changed, 24 insertions(+), 13 deletions(-)

lib/std/Progress.zig+24-13
......@@ -875,15 +875,23 @@ fn computePrefix(
875875 if (serialized.parents[@intFromEnum(parent_index)] == .none) return i;
876876 i = computePrefix(buf, i, serialized, children, parent_index);
877877 if (children[@intFromEnum(parent_index)].sibling == .none) {
878 buf[i..][0..3].* = " ".*;
879 i += 3;
878 const prefix = " ";
879 const upper_bound_len = prefix.len + line_upper_bound_len;
880 if (i + upper_bound_len > buf.len) return buf.len;
881 buf[i..][0..prefix.len].* = prefix.*;
882 i += prefix.len;
880883 } else {
884 const upper_bound_len = tree_line.len + line_upper_bound_len;
885 if (i + upper_bound_len > buf.len) return buf.len;
881886 buf[i..][0..tree_line.len].* = tree_line.*;
882887 i += tree_line.len;
883888 }
884889 return i;
885890}
886891
892const line_upper_bound_len = @max(tree_tee.len, tree_langle.len) + "[4294967296/4294967296] ".len +
893 Node.max_name_len + finish_sync.len;
894
887895fn computeNode(
888896 buf: []u8,
889897 start_i: usize,
......@@ -894,6 +902,9 @@ fn computeNode(
894902 var i = start_i;
895903 i = computePrefix(buf, i, serialized, children, node_index);
896904
905 if (i + line_upper_bound_len > buf.len)
906 return start_i;
907
897908 const storage = &serialized.storage[@intFromEnum(node_index)];
898909 const estimated_total = storage.estimated_total_count;
899910 const completed_items = storage.completed_count;
......@@ -910,19 +921,19 @@ fn computeNode(
910921 }
911922 }
912923
913 if (name.len != 0 or estimated_total > 0) {
914 if (estimated_total > 0) {
915 i += (std.fmt.bufPrint(buf[i..], "[{d}/{d}] ", .{ completed_items, estimated_total }) catch &.{}).len;
916 } else if (completed_items != 0) {
917 i += (std.fmt.bufPrint(buf[i..], "[{d}] ", .{completed_items}) catch &.{}).len;
918 }
919 if (name.len != 0) {
920 i += (std.fmt.bufPrint(buf[i..], "{s}", .{name}) catch &.{}).len;
921 }
922 }
923
924924 const is_empty_root = @intFromEnum(node_index) == 0 and serialized.storage[0].name[0] == 0;
925925 if (!is_empty_root) {
926 if (name.len != 0 or estimated_total > 0) {
927 if (estimated_total > 0) {
928 i += (std.fmt.bufPrint(buf[i..], "[{d}/{d}] ", .{ completed_items, estimated_total }) catch &.{}).len;
929 } else if (completed_items != 0) {
930 i += (std.fmt.bufPrint(buf[i..], "[{d}] ", .{completed_items}) catch &.{}).len;
931 }
932 if (name.len != 0) {
933 i += (std.fmt.bufPrint(buf[i..], "{s}", .{name}) catch &.{}).len;
934 }
935 }
936
926937 i = @min(global_progress.cols + start_i, i);
927938 buf[i] = '\n';
928939 i += 1;