From c03808537ac4de87cb85fec0db64ff6cb1ba6ebb Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Sun, 11 Sep 2022 17:37:42 -0700 Subject: [PATCH] Be defensive against extremely narrow (<10) terminal widths Previously this would overflow. --- src/display/side_by_side.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/display/side_by_side.rs b/src/display/side_by_side.rs index 0a6c6bdae..f3be740b8 100644 --- a/src/display/side_by_side.rs +++ b/src/display/side_by_side.rs @@ -203,9 +203,23 @@ impl SourceDimensions { let rhs_line_nums_width = format_line_num(rhs_max_line).len(); let lhs_total_width = (terminal_width - SPACER.len()) / 2; - let lhs_content_width = lhs_total_width - lhs_line_nums_width; - let rhs_content_width = - terminal_width - lhs_total_width - SPACER.len() - rhs_line_nums_width; + + let lhs_content_width = if lhs_line_nums_width < lhs_total_width { + lhs_total_width - lhs_line_nums_width + } else { + // The terminal is so narrow that even the column numbers + // display doesn't fit. Ensure we show a non-zero number + // of content columns anyway. + 1 + }; + + let rhs_content_width = max( + 1, + terminal_width as isize + - lhs_total_width as isize + - SPACER.len() as isize + - rhs_line_nums_width as isize, + ) as usize; Self { lhs_content_width,