use ratatui::layout::Rect;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LayoutMode {
Compact,
Standard,
Wide,
}
impl LayoutMode {
pub fn from_area(area: Rect) -> Self {
if area.width < 80 || area.height < 20 {
LayoutMode::Compact
} else if area.width >= 120 && area.height >= 24 {
LayoutMode::Wide
} else {
LayoutMode::Standard
}
}
pub fn show_borders(self) -> bool {
!matches!(self, LayoutMode::Compact)
}
pub fn show_titles(self) -> bool {
!matches!(self, LayoutMode::Compact)
}
pub fn allow_sidebar(self) -> bool {
matches!(self, LayoutMode::Wide)
}
pub fn show_logs_panel(self) -> bool {
!matches!(self, LayoutMode::Compact)
}
pub fn footer_height(self) -> u16 {
0
}
pub fn show_footer(self) -> bool {
false
}
pub fn max_header_percent(self) -> f32 {
match self {
LayoutMode::Compact => 0.15,
LayoutMode::Standard => 0.25,
LayoutMode::Wide => 0.30,
}
}
pub fn sidebar_width_percent(self) -> u16 {
match self {
LayoutMode::Wide => 28,
_ => 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compact_mode() {
let area = Rect::new(0, 0, 60, 15);
assert_eq!(LayoutMode::from_area(area), LayoutMode::Compact);
}
#[test]
fn test_standard_mode() {
let area = Rect::new(0, 0, 100, 30);
assert_eq!(LayoutMode::from_area(area), LayoutMode::Standard);
}
#[test]
fn test_wide_mode() {
let area = Rect::new(0, 0, 150, 40);
assert_eq!(LayoutMode::from_area(area), LayoutMode::Wide);
}
#[test]
fn test_mode_properties() {
assert!(!LayoutMode::Compact.show_borders());
assert!(!LayoutMode::Compact.show_footer());
assert!(!LayoutMode::Compact.allow_sidebar());
assert_eq!(LayoutMode::Compact.footer_height(), 0);
assert!(LayoutMode::Standard.show_borders());
assert!(!LayoutMode::Standard.show_footer());
assert!(!LayoutMode::Standard.allow_sidebar());
assert_eq!(LayoutMode::Standard.footer_height(), 0);
assert!(LayoutMode::Wide.show_borders());
assert!(!LayoutMode::Wide.show_footer());
assert!(LayoutMode::Wide.allow_sidebar());
assert_eq!(LayoutMode::Wide.footer_height(), 0);
}
#[test]
fn test_boundary_conditions() {
let area_80 = Rect::new(0, 0, 80, 24);
assert_eq!(LayoutMode::from_area(area_80), LayoutMode::Standard);
let area_120 = Rect::new(0, 0, 120, 24);
assert_eq!(LayoutMode::from_area(area_120), LayoutMode::Wide);
let area_79 = Rect::new(0, 0, 79, 24);
assert_eq!(LayoutMode::from_area(area_79), LayoutMode::Compact);
let area_wide_short = Rect::new(0, 0, 150, 20);
assert_eq!(LayoutMode::from_area(area_wide_short), LayoutMode::Standard);
}
}