ui: make word_break an iter
parent
b104a71544
commit
72d347eb6b
|
@ -770,45 +770,6 @@ eprintln!(" Invalid area with string {} and area {:?}", s, area);
|
|||
(x, y)
|
||||
}
|
||||
|
||||
pub fn word_break_string(mut s: &str, width: usize) -> Vec<&str> {
|
||||
let mut ret: Vec<&str> = Vec::with_capacity(16);
|
||||
loop {
|
||||
if s.is_empty() {
|
||||
break;
|
||||
}
|
||||
s = s.trim_start_matches(|c| c == ' ');
|
||||
if s.starts_with('\n') {
|
||||
ret.push(&s[0..0]);
|
||||
s = &s["\n".len()..];
|
||||
continue;
|
||||
}
|
||||
if let Some(next_idx) = s.as_bytes().iter().position(|&c| c == b'\n') {
|
||||
if next_idx <= width {
|
||||
ret.push(&s[..next_idx]);
|
||||
s = &s[next_idx + 1..];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let graphemes = s.graphemes_indices();
|
||||
if graphemes.len() > width {
|
||||
// use grapheme indices and find position of " " graphemes
|
||||
if let Some(next_idx) = graphemes[..width].iter().rposition(|(_, g)| *g == " ") {
|
||||
let next_idx = graphemes[next_idx].0;
|
||||
ret.push(&s[..next_idx]);
|
||||
s = &s[next_idx + 1..];
|
||||
} else {
|
||||
ret.push(&s[..width]);
|
||||
s = &s[width..];
|
||||
}
|
||||
} else {
|
||||
ret.push(s);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
/// Completely clear an `Area` with an empty char and the terminal's default colors.
|
||||
pub fn clear_area(grid: &mut CellBuffer, area: Area) {
|
||||
if !is_valid_area!(area) {
|
||||
|
|
|
@ -39,6 +39,58 @@ pub trait Graphemes: UnicodeSegmentation + CodePointsIter {
|
|||
|
||||
impl Graphemes for str {}
|
||||
|
||||
pub struct WordBreakIter<'s> {
|
||||
input: &'s str,
|
||||
width: usize,
|
||||
}
|
||||
impl<'s> Iterator for WordBreakIter<'s> {
|
||||
type Item = &'s str;
|
||||
|
||||
fn next(&mut self) -> Option<&'s str> {
|
||||
if self.input.is_empty() {
|
||||
return None;
|
||||
}
|
||||
self.input = self.input.trim_start_matches(|c| c == ' ');
|
||||
if self.input.starts_with('\n') {
|
||||
let ret = &self.input[0..];
|
||||
self.input = &self.input["\n".len()..];
|
||||
return Some(ret);
|
||||
}
|
||||
if let Some(next_idx) = self.input.as_bytes().iter().position(|&c| c == b'\n') {
|
||||
if next_idx <= self.width {
|
||||
let ret = &self.input[..next_idx];
|
||||
self.input = &self.input[next_idx + 1..];
|
||||
return Some(ret);
|
||||
}
|
||||
}
|
||||
let graphemes = UnicodeSegmentation::grapheme_indices(self.input, true)
|
||||
.take(self.width)
|
||||
.collect::<Vec<(usize, &str)>>();
|
||||
if graphemes.len() == self.width {
|
||||
// use grapheme indices and find position of " " graphemes
|
||||
if let Some(next_idx) = graphemes.iter().rposition(|(_, g)| *g == " ") {
|
||||
let next_idx = graphemes[next_idx].0;
|
||||
let ret = &self.input[..next_idx];
|
||||
self.input = &self.input[next_idx + 1..];
|
||||
return Some(&self.input[..next_idx]);
|
||||
} else {
|
||||
let ret = &self.input[..self.width];
|
||||
self.input = &self.input[self.width..];
|
||||
return Some(ret);
|
||||
}
|
||||
} else {
|
||||
let ret = self.input;
|
||||
self.input = &self.input[self.input.len() - 1..];
|
||||
return Some(ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn word_break_string(mut s: &str, width: usize) -> Vec<&str> {
|
||||
let iter = WordBreakIter { input: s, width };
|
||||
iter.collect()
|
||||
}
|
||||
|
||||
//#[derive(PartialEq)]
|
||||
//enum Property {
|
||||
// CR,
|
||||
|
|
Loading…
Reference in New Issue