diff options
| author | pablu <me@pablu.de> | 2026-06-02 18:49:48 +0200 |
|---|---|---|
| committer | pablu <me@pablu.de> | 2026-06-02 18:49:48 +0200 |
| commit | b547e6758c635682c94794e5c13070a73c918203 (patch) | |
| tree | 2ca030a7acbe436a304e6b87dd93deb0b2351909 /src/widgets/helpers.rs | |
| parent | afe649e0efc31fcd33a828e54bd6313cd37f1784 (diff) | |
wip: refactor architecture
Diffstat (limited to 'src/widgets/helpers.rs')
| -rw-r--r-- | src/widgets/helpers.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/widgets/helpers.rs b/src/widgets/helpers.rs new file mode 100644 index 0000000..0bde326 --- /dev/null +++ b/src/widgets/helpers.rs @@ -0,0 +1,65 @@ +use ratatui::{ + layout::{Constraint, Direction, Layout, Rect}, + style::{Color, Modifier, Style}, + text::{Line, Span}, +}; + +use crate::gentoo::InstalledPackage; + +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(pkg: &InstalledPackage, bold_style: Style) -> Vec<Line<'_>> { + let mut lines = Vec::new(); + lines.push(Line::from(vec![ + Span::styled("Homepage: ", bold_style), + Span::raw(""), + ])); + + for url in pkg.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]) + } +} |
