From 06c8ebd310738d7d17d358dabfe29955abd3ffc5 Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Wed, 17 Jul 2024 16:19:11 +0200 Subject: [PATCH] feat(tui): Fixed preview height option (#2286) * Added "fixed" to preview strategy Uses max_preview_height as a fixed value to prevent the list from "jumping". * Added test for new setting --- crates/atuin-client/config.toml | 1 + crates/atuin-client/src/settings.rs | 4 ++ .../src/command/client/search/interactive.rs | 37 +++++++++++++++---- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/crates/atuin-client/config.toml b/crates/atuin-client/config.toml index 4ddd93f5..00343dbb 100644 --- a/crates/atuin-client/config.toml +++ b/crates/atuin-client/config.toml @@ -209,6 +209,7 @@ records = true ## possible values: auto, static ## auto: length of the selected command. ## static: length of the longest command stored in the history. +## fixed: use max_preview_height as fixed height. # strategy = "auto" [daemon] diff --git a/crates/atuin-client/src/settings.rs b/crates/atuin-client/src/settings.rs index b64418cd..a83f36bb 100644 --- a/crates/atuin-client/src/settings.rs +++ b/crates/atuin-client/src/settings.rs @@ -410,6 +410,10 @@ pub enum PreviewStrategy { // Preview height is calculated for the length of the longest command stored in the history. #[serde(rename = "static")] Static, + + // max_preview_height is used as fixed height. + #[serde(rename = "fixed")] + Fixed, } #[derive(Clone, Debug, Deserialize, Serialize)] diff --git a/crates/atuin/src/command/client/search/interactive.rs b/crates/atuin/src/command/client/search/interactive.rs index 825e978a..e7909af8 100644 --- a/crates/atuin/src/command/client/search/interactive.rs +++ b/crates/atuin/src/command/client/search/interactive.rs @@ -583,6 +583,8 @@ impl State { .sum(), ) }) + border_size * 2 + } else if settings.show_preview && settings.preview.strategy == PreviewStrategy::Fixed { + settings.max_preview_height + border_size * 2 } else if compact || tab_index == 1 { 0 } else { @@ -1244,6 +1246,15 @@ mod tests { ..Settings::utc() }; + let settings_preview_fixed = Settings { + preview: Preview { + strategy: PreviewStrategy::Fixed, + }, + show_preview: true, + max_preview_height: 15, + ..Settings::utc() + }; + let cmd_60: History = History::capture() .timestamp(time::OffsetDateTime::now_utc()) .command("for i in $(seq -w 10); do echo \"item number $i - abcd\"; done") @@ -1337,14 +1348,26 @@ mod tests { 1, 20, ); + // the longest command requires 10 lines, but we have a max preview height of 15 and a fixed preview strategy + let settings_preview_fixed = State::calc_preview_height( + &settings_preview_fixed, + &results, + 1 as usize, + 0 as usize, + false, + 1, + 20, + ); assert_eq!(no_preview, 1); - // 1*2 is the space for the border - assert_eq!(preview_h2, 2 + 1 * 2); - assert_eq!(preview_h3, 3 + 1 * 2); - assert_eq!(preview_one_line, 1 + 1 * 2); - assert_eq!(preview_limit_at_2, 2 + 1 * 2); - assert_eq!(preview_static_h3, 3 + 1 * 2); - assert_eq!(preview_static_limit_at_4, 4 + 1 * 2); + // 1 * 2 is the space for the border + let border_space = 1 * 2; + assert_eq!(preview_h2, 2 + border_space); + assert_eq!(preview_h3, 3 + border_space); + assert_eq!(preview_one_line, 1 + border_space); + assert_eq!(preview_limit_at_2, 2 + border_space); + assert_eq!(preview_static_h3, 3 + border_space); + assert_eq!(preview_static_limit_at_4, 4 + border_space); + assert_eq!(settings_preview_fixed, 15 + border_space); } }