Refactored the HorizontallyScrollableText struct to handle Unicode strings more uniformly and to not rely on byte boundaries but instead to rely on Unicode char boundaries

This commit is contained in:
2023-08-08 14:15:23 -06:00
parent 218d442694
commit ce12ebd301
7 changed files with 164 additions and 31 deletions
+39 -4
View File
@@ -270,14 +270,49 @@ impl HorizontallyScrollableText {
pub fn pop(&mut self) {
if *self.offset.borrow() < self.len() {
self.text.remove(self.len() - *self.offset.borrow() - 1);
let (index, _) = self
.text
.chars()
.enumerate()
.nth(self.len() - *self.offset.borrow() - 1)
.unwrap();
self.text = self
.text
.chars()
.enumerate()
.filter(|(idx, _)| *idx != index)
.map(|tuple| tuple.1)
.collect();
}
}
pub fn push(&mut self, character: char) {
self
.text
.insert(self.len() - *self.offset.borrow(), character);
if self.text.is_empty() {
self.text.push(character);
} else {
let index = self.len() - *self.offset.borrow();
if index == self.len() {
self.text.push(character);
} else {
let mut new_text = String::new();
self
.text
.chars()
.collect::<Vec<char>>()
.iter()
.enumerate()
.for_each(|(idx, &c)| {
if idx == index {
new_text.push(character);
}
new_text.push(c);
});
self.text = new_text;
}
}
}
}