use unicode_segmentation::UnicodeSegmentation;
use super::{
types::Reflow,
wcwidth::{wcwidth, CodePointsIter},
};
pub trait TextProcessing: UnicodeSegmentation + CodePointsIter {
fn split_graphemes(&self) -> Vec<&str> {
UnicodeSegmentation::graphemes(self, true).collect::<Vec<&str>>()
}
fn graphemes_indices(&self) -> Vec<(usize, &str)> {
UnicodeSegmentation::grapheme_indices(self, true).collect::<Vec<(usize, &str)>>()
}
fn next_grapheme(&self) -> Option<(usize, &str)> {
UnicodeSegmentation::grapheme_indices(self, true).next()
}
fn last_grapheme(&self) -> Option<(usize, &str)> {
UnicodeSegmentation::grapheme_indices(self, true).next_back()
}
fn grapheme_width(&self) -> usize {
let mut count = 0;
for c in self.code_points() {
count += wcwidth(c).unwrap_or(0);
}
count
}
fn grapheme_len(&self) -> usize {
self.split_graphemes().len()
}
fn split_lines(&self, width: usize) -> Vec<String>;
fn split_lines_reflow(&self, reflow: Reflow, width: Option<usize>) -> Vec<String>;
}
impl TextProcessing for str {
fn split_lines(&self, width: usize) -> Vec<String> {
if width == 0 {
return vec![];
}
super::line_break::linear(self, width)
}
fn split_lines_reflow(&self, reflow: Reflow, width: Option<usize>) -> Vec<String> {
if width == Some(0) {
return vec![];
}
super::line_break::split_lines_reflow(self, reflow, width)
}
}