use ratatui::{
style::{Color, Modifier, Style},
text::Span,
};
pub(super) fn wrap_at_chars(content: &str, width: usize) -> Vec<&str> {
use unicode_width::UnicodeWidthChar;
if content.is_empty() || width == 0 {
return vec![content];
}
let mut chunks = Vec::new();
let mut chunk_start = 0usize;
let mut chunk_cells = 0usize;
for (idx, ch) in content.char_indices() {
let ch_cells = ch.width().unwrap_or(0);
if chunk_cells > 0 && chunk_cells + ch_cells > width {
chunks.push(&content[chunk_start..idx]);
chunk_start = idx;
chunk_cells = 0;
}
chunk_cells += ch_cells;
}
if chunk_start < content.len() {
chunks.push(&content[chunk_start..]);
}
if chunks.is_empty() {
chunks.push(content);
}
chunks
}
pub(super) fn eof_no_newline_span(bg: Option<Color>) -> Span<'static> {
let mut style = Style::default()
.fg(Color::Yellow)
.add_modifier(Modifier::BOLD);
if let Some(b) = bg {
style = style.bg(b);
}
Span::styled("∅", style)
}
pub(super) fn take_cells(s: &str, max_cells: usize) -> (String, usize) {
use unicode_width::UnicodeWidthChar;
let mut out = String::new();
let mut cells = 0usize;
for ch in s.chars() {
let w = ch.width().unwrap_or(0);
if cells + w > max_cells {
break;
}
out.push(ch);
cells += w;
}
(out, cells)
}