use ratatui::{ layout::{Constraint, Direction, Layout, Rect}, style::{Color, Modifier, Style}, text::{Line, Span}, }; use crate::gentoo::package::Metadata; pub fn search_popup_rect(percent_x: u16, r: Rect) -> Rect { // Cut the given rectangle into three vertical pieces let popup_layout = Layout::default() .direction(Direction::Vertical) .constraints([Constraint::Length(10), Constraint::Length(3)]) .split(r); // Then cut the middle vertical piece into three width-wise pieces Layout::default() .direction(Direction::Horizontal) .constraints([ Constraint::Percentage((100 - percent_x) / 2), Constraint::Percentage(percent_x), Constraint::Percentage((100 - percent_x) / 2), ]) .split(popup_layout[1])[1] // Return the middle chunk } pub fn homepage_lines(metadata: &Metadata, bold_style: Style) -> Vec> { let mut lines = Vec::new(); lines.push(Line::from(vec![ Span::styled("Homepage: ", bold_style), Span::raw(""), ])); for url in metadata.homepage.iter().filter(|s| !s.is_empty()) { lines.push(Line::from(vec![ Span::raw(" - "), // indentation Span::styled( url, Style::default() .fg(Color::LightBlue) .add_modifier(Modifier::UNDERLINED), ), ])); } lines } pub fn human_size(bytes: usize) -> String { const UNITS: [&str; 5] = ["B", "KB", "MB", "GB", "TB"]; let mut size = bytes as f64; let mut unit = 0; while size >= 1024.0 && unit < UNITS.len() - 1 { size /= 1024.0; unit += 1; } if unit == 0 { format!("{} {}", bytes, UNITS[unit]) } else { format!("{:.1} {}", size, UNITS[unit]) } }