From bd306945b66f40e82edb763da5474a1e548cab8c Mon Sep 17 00:00:00 2001 From: IchHabeKeineNamen <36535895+Banyc@users.noreply.github.com> Date: Thu, 25 May 2023 07:07:44 +0800 Subject: [PATCH] fix: prompt position on resize (#578) * fix: prompt position on resize * fix: u16 overflow * fix: prompt position when terminal grows in height * fix: exclude blank area from prompt height fix: resume the early return to exclude a case where the terminal got smaller but the prompt is still visible * fix: prevent any line overwriting at the cost of duplicate prompts * fix: prevent width change from eating previous output --- src/painting/painter.rs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/painting/painter.rs b/src/painting/painter.rs index 617b598..d2bbb03 100644 --- a/src/painting/painter.rs +++ b/src/painting/painter.rs @@ -405,22 +405,27 @@ impl Painter { let prev_prompt_row = self.prompt_start_row; self.terminal_size = (width, height); - // TODO properly adjusting prompt_origin on resizing while lines > 1 - if prev_prompt_row >= (height - 1) { - // Terminal is shrinking up - // FIXME: use actual prompt size at some point - // Note: you can't just subtract the offset from the origin, - // as we could be shrinking so fast that the offset we read back from - // crossterm is past where it would have been. - self.prompt_start_row = height - 2; - } else if prev_terminal_size.1 < height { - // Terminal is growing down, so move the prompt down the same amount to make space - // for history that's on the screen - // Note: if the terminal doesn't have sufficient history, this will leave a trail - // of previous prompts currently. - self.prompt_start_row = prev_prompt_row + (height - prev_terminal_size.1); + if prev_prompt_row < height + && height <= prev_terminal_size.1 + && width == prev_terminal_size.0 + { + // The terminal got smaller in height but the start of the prompt is still visible + // The width didn't change + return; } + + // Either: + // - The terminal got larger in height + // - Note: if the terminal doesn't have sufficient history, this will leave a trail + // of previous prompts currently. + // - Note: if the the prompt contains multiple lines, this will leave a trail of + // previous prompts currently. + // - The terminal got smaller in height and the whole prompt is no longer visible + // - Note: if the the prompt contains multiple lines, this will leave a trail of + // previous prompts currently. + // - The width changed + self.prompt_start_row = height.saturating_sub(1); } /// Writes `line` to the terminal with a following carriage return and newline