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(...@@ -875,15 +875,23 @@ fn computePrefix(
875 if (serialized.parents[@intFromEnum(parent_index)] == .none) return i;875 if (serialized.parents[@intFromEnum(parent_index)] == .none) return i;
876 i = computePrefix(buf, i, serialized, children, parent_index);876 i = computePrefix(buf, i, serialized, children, parent_index);
877 if (children[@intFromEnum(parent_index)].sibling == .none) {877 if (children[@intFromEnum(parent_index)].sibling == .none) {
878 buf[i..][0..3].* = " ".*;878 const prefix = " ";
879 i += 3;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;
880 } else {883 } else {
884 const upper_bound_len = tree_line.len + line_upper_bound_len;
885 if (i + upper_bound_len > buf.len) return buf.len;
881 buf[i..][0..tree_line.len].* = tree_line.*;886 buf[i..][0..tree_line.len].* = tree_line.*;
882 i += tree_line.len;887 i += tree_line.len;
883 }888 }
884 return i;889 return i;
885}890}
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
887fn computeNode(895fn computeNode(
888 buf: []u8,896 buf: []u8,
889 start_i: usize,897 start_i: usize,
...@@ -894,6 +902,9 @@ fn computeNode(...@@ -894,6 +902,9 @@ fn computeNode(
894 var i = start_i;902 var i = start_i;
895 i = computePrefix(buf, i, serialized, children, node_index);903 i = computePrefix(buf, i, serialized, children, node_index);
896904
905 if (i + line_upper_bound_len > buf.len)
906 return start_i;
907
897 const storage = &serialized.storage[@intFromEnum(node_index)];908 const storage = &serialized.storage[@intFromEnum(node_index)];
898 const estimated_total = storage.estimated_total_count;909 const estimated_total = storage.estimated_total_count;
899 const completed_items = storage.completed_count;910 const completed_items = storage.completed_count;
...@@ -910,19 +921,19 @@ fn computeNode(...@@ -910,19 +921,19 @@ fn computeNode(
910 }921 }
911 }922 }
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
924 const is_empty_root = @intFromEnum(node_index) == 0 and serialized.storage[0].name[0] == 0;924 const is_empty_root = @intFromEnum(node_index) == 0 and serialized.storage[0].name[0] == 0;
925 if (!is_empty_root) {925 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
926 i = @min(global_progress.cols + start_i, i);937 i = @min(global_progress.cols + start_i, i);
927 buf[i] = '\n';938 buf[i] = '\n';
928 i += 1;939 i += 1;