use ratatui::layout::Rect;
use hefesto_widgets::{ChoosePopup, ChoosePopupState, PopupSize};
pub const CHOOSE_DEFAULT_MAX_H: u16 = 60;
pub fn choose_default_height(items_len: usize) -> u16 {
((items_len as u16) + 4).min(CHOOSE_DEFAULT_MAX_H)
}
pub fn choose_rect(popup: &ChoosePopup<'_>, items_len: usize, area: Rect) -> Rect {
fixed_rect(popup, choose_default_height(items_len), area)
}
fn fixed_rect(popup: &ChoosePopup<'_>, h: u16, area: Rect) -> Rect {
let clone = popup.clone().height(PopupSize::Fixed(h));
clone.resolve_rect(area, &ChoosePopupState::default())
}
pub fn spin_default_height(
has_cmd: bool,
output_lines_len: usize,
max_output_rows: u16,
show_footer: bool,
) -> u16 {
let mut content_h = 2u16;
if has_cmd {
content_h += 1;
}
if output_lines_len > 0 {
let clamped = (output_lines_len as u16).min(max_output_rows);
content_h += clamped + 1; }
if show_footer {
content_h += 2; }
content_h + 2 }
pub fn tree_default_height(visible_count: usize, has_header: bool) -> u16 {
let header_height: u16 = if has_header { 2 } else { 0 };
let tree_height = (visible_count as u16) + 2 + header_height;
tree_height.max(5)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn choose_default_height_few_items() {
assert_eq!(choose_default_height(4), 8); }
#[test]
fn choose_default_height_caps_at_max() {
let many = CHOOSE_DEFAULT_MAX_H as usize - 4 + 10;
assert_eq!(choose_default_height(many), CHOOSE_DEFAULT_MAX_H);
}
#[test]
fn choose_default_height_no_items() {
assert_eq!(choose_default_height(0), 4);
}
#[test]
fn spin_default_height_minimal() {
assert_eq!(spin_default_height(false, 0, 10, false), 4);
}
#[test]
fn spin_default_height_with_cmd() {
assert_eq!(spin_default_height(true, 0, 10, false), 5);
}
#[test]
fn spin_default_height_with_output() {
assert_eq!(spin_default_height(false, 5, 10, false), 10); }
#[test]
fn spin_default_height_output_clamped() {
assert_eq!(spin_default_height(false, 50, 10, false), 15); }
#[test]
fn spin_default_height_with_footer() {
assert_eq!(spin_default_height(false, 0, 10, true), 6); }
#[test]
fn spin_default_height_all() {
assert_eq!(spin_default_height(true, 3, 10, true), 11); }
#[test]
fn tree_default_height_no_header() {
assert_eq!(tree_default_height(3, false), 5); }
#[test]
fn tree_default_height_with_header() {
assert_eq!(tree_default_height(3, true), 7); }
#[test]
fn tree_default_height_empty_tree() {
assert_eq!(tree_default_height(0, false), 5); }
#[test]
fn tree_default_height_many_items() {
assert_eq!(tree_default_height(20, true), 24); }
}