use ratatui::prelude::*;
use ratatui::widgets::{Block, Borders, Paragraph, Wrap};
pub(super) fn wrapped_line_count(text: &str, width: usize) -> usize {
if width == 0 {
return 1;
}
let mut rows = 1usize;
let mut col = 0usize;
for (i, word) in text.split(' ').enumerate() {
let w = word.chars().count();
let need = if i == 0 { w } else { col + 1 + w };
if need <= width {
col = need;
} else {
rows += 1;
col = w;
}
while col > width {
rows += 1;
col -= width;
}
}
rows
}
pub(super) fn titled(name: &str, count: usize) -> Block<'static> {
Block::default()
.borders(Borders::ALL)
.title(format!(" {name} ({count}) "))
}
pub(super) fn empty(f: &mut Frame, area: Rect, name: &str, msg: &str) {
let para = Paragraph::new(msg)
.block(titled(name, 0))
.style(Style::default().add_modifier(Modifier::DIM))
.wrap(Wrap { trim: false });
f.render_widget(para, area);
}
pub(super) fn centered(area: Rect, pct_x: u16, height: u16) -> Rect {
let w = area.width * pct_x / 100;
let h = height.min(area.height);
Rect {
x: area.x + area.width.saturating_sub(w) / 2,
y: area.y + area.height.saturating_sub(h) / 2,
width: w,
height: h,
}
}
pub(super) fn shell_join_display(argv: &[String]) -> String {
let shown: Vec<String> = argv
.iter()
.map(|a| {
let c = crate::ini::collapse_tilde(a);
match c.strip_prefix("~/") {
Some(rest) if !needs_quoting(rest) => c,
_ => shell_join(std::slice::from_ref(&c)),
}
})
.collect();
shown.join(" ")
}
fn needs_quoting(s: &str) -> bool {
s.is_empty()
|| s.chars()
.any(|c| c.is_whitespace() || "\"'\\$`;&|<>()*?[]{}!#~".contains(c))
}
pub(super) fn shell_join(argv: &[String]) -> String {
argv.iter()
.map(|a| {
if needs_quoting(a) {
format!("'{}'", a.replace('\'', r"'\''"))
} else {
a.clone()
}
})
.collect::<Vec<_>>()
.join(" ")
}