From 9ca6068a177d314524a50e7dba81a954723398b5 Mon Sep 17 00:00:00 2001 From: extrawurst Date: Sun, 18 Dec 2022 20:07:18 +0100 Subject: [PATCH] fix crash in small window and branches fixes #1470 --- CHANGELOG.md | 1 + src/ui/mod.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afbd0db0..ad819965 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * fix key binding shown in bottom bar for `stash_open` ([#1454](https://github.com/extrawurst/gitui/issues/1454)) * `--bugreport` does not require param ([#1466](https://github.com/extrawurst/gitui/issues/1466)) * `edit`-file command shown on commits msg ([#1461](https://github.com/extrawurst/gitui/issues/1461)) +* crash on branches popup in small terminal ([#1470](https://github.com/extrawurst/gitui/issues/1470)) ## [0.22.1] - 2022-11-22 diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 8f11dc9b..72b74cfa 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -87,8 +87,18 @@ pub fn centered_rect( /// makes sure Rect `r` at least stays as big as min and not bigger than max pub fn rect_inside(min: Size, max: Size, r: Rect) -> Rect { - let new_width = r.width.clamp(min.width, max.width); - let new_height = r.height.clamp(min.height, max.height); + let new_width = if min.width > max.width { + max.width + } else { + r.width.clamp(min.width, max.width) + }; + + let new_height = if min.height > max.height { + max.width + } else { + r.height.clamp(min.height, max.height) + }; + let diff_width = new_width.saturating_sub(r.width); let diff_height = new_height.saturating_sub(r.height); @@ -142,3 +152,40 @@ pub fn common_nav( None } } + +#[cfg(test)] +mod test { + use super::{rect_inside, Size}; + use pretty_assertions::assert_eq; + use tui::layout::Rect; + + #[test] + fn test_small_rect_in_rect() { + let rect = rect_inside( + Size { + width: 2, + height: 2, + }, + Size { + width: 1, + height: 1, + }, + Rect { + x: 0, + y: 0, + width: 10, + height: 10, + }, + ); + + assert_eq!( + rect, + Rect { + x: 0, + y: 0, + width: 1, + height: 1 + } + ); + } +}