authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2022-10-13 14:46:25+02:00
committergravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2022-10-13 16:06:51+02:00
logcbe6872518bd67f05f87172367b22ad763241b21
treeb4a29c6f7073375429ebdb4f79d648756da86745
parente484e759698f3bbcf0d92f94a6addc9eea4afc73

refactor: max_width calculation

I think this may be helpful in the future when we might want to calculate this again at some other point. It also makes it more clear that the other two functions below it are only required for this calculation.

1 files changed, 16 insertions(+), 12 deletions(-)

lib/std/Progress.zig+16-12
...@@ -178,6 +178,22 @@ pub fn start(self: *Progress, name: []const u8, estimated_total_items: usize) *N...@@ -178,6 +178,22 @@ pub fn start(self: *Progress, name: []const u8, estimated_total_items: usize) *N
178 // we are in a "dumb" terminal like in acme or writing to a file178 // we are in a "dumb" terminal like in acme or writing to a file
179 self.terminal = stderr;179 self.terminal = stderr;
180 }180 }
181 self.calculateMaxWidth();
182 self.root = Node{
183 .context = self,
184 .parent = null,
185 .name = name,
186 .unprotected_estimated_total_items = estimated_total_items,
187 .unprotected_completed_items = 0,
188 };
189 self.columns_written = 0;
190 self.prev_refresh_timestamp = 0;
191 self.timer = time.Timer.start() catch null;
192 self.done = false;
193 return &self.root;
194}
195
196fn calculateMaxWidth(self: *Progress) void {
181 if (self.max_width == null) {197 if (self.max_width == null) {
182 if (self.terminal) |terminal| {198 if (self.terminal) |terminal| {
183 // choose an optimal width and account for progress output that could have been printed199 // choose an optimal width and account for progress output that could have been printed
...@@ -194,18 +210,6 @@ pub fn start(self: *Progress, name: []const u8, estimated_total_items: usize) *N...@@ -194,18 +210,6 @@ pub fn start(self: *Progress, name: []const u8, estimated_total_items: usize) *N
194 truncation_suffix.len, // make sure we can at least truncate210 truncation_suffix.len, // make sure we can at least truncate
195 self.output_buffer.len - 1,211 self.output_buffer.len - 1,
196 );212 );
197 self.root = Node{
198 .context = self,
199 .parent = null,
200 .name = name,
201 .unprotected_estimated_total_items = estimated_total_items,
202 .unprotected_completed_items = 0,
203 };
204 self.columns_written = 0;
205 self.prev_refresh_timestamp = 0;
206 self.timer = time.Timer.start() catch null;
207 self.done = false;
208 return &self.root;
209}213}
210214
211fn getTerminalWidth(self: Progress, file_handle: os.fd_t) !u16 {215fn getTerminalWidth(self: Progress, file_handle: os.fd_t) !u16 {