Cleaner scroll handling

This commit is contained in:
2026-07-31 15:15:55 +02:00
parent 0f7d25627d
commit d779ac1a59
2 changed files with 58 additions and 43 deletions
+56 -39
View File
@@ -15,58 +15,75 @@ pub struct RenderScope {
draw_instructions: Vec<Instr>,
}
struct RenderTextResult {
text: String,
line_len: u16,
lines_len: u16,
}
impl RenderTextResult {
fn insert_newline(&mut self, height: u16) -> bool {
if self.lines_len >= height {
return true;
}
self.text.push('\n');
self.line_len = 0;
self.lines_len += 1;
false
}
fn insert(&mut self, c: char) {
self.text.push(c);
self.line_len += 1;
}
}
impl RenderScope {
pub fn draw_text<P: Into<Point>, T: Display>(&mut self, at: P, text: T) -> u16 {
let point: Point = at.into();
let lines = text
.to_string()
.lines()
.map(|line| {
let mut chars = line.chars().peekable();
let mut new_line = String::new();
let mut len = 0;
let text = text.to_string();
let mut chars = text.chars().peekable();
let mut result = RenderTextResult {
text: String::new(),
line_len: 0,
lines_len: 1,
};
while let Some(c) = chars.next() {
if c == '\x1b' && chars.peek() == Some(&'[') {
result.text.push(c);
while let Some(c) = chars.next() {
if c == '\x1b' && chars.peek() == Some(&'[') {
new_line.push(c);
result.text.push(c);
while let Some(c) = chars.next() {
new_line.push(c);
if c.is_ascii_alphabetic() {
break;
}
}
continue;
}
new_line.push(c);
len += 1;
if len >= self.rect.width as usize {
new_line.push('\n');
len = 0;
if c.is_ascii_alphabetic() {
break;
}
}
} else if c == '\n' {
if result.insert_newline(self.rect.height) {
break;
}
} else {
result.insert(c);
new_line
})
.collect::<Vec<_>>()
.join("\n");
let lines = lines
.lines()
.take((self.rect.height - point.y) as usize)
.collect::<Vec<_>>();
let lines_len = lines.len();
if result.line_len >= self.rect.width {
if result.insert_newline(self.rect.height) {
break;
}
}
}
}
self.draw_instructions
.push(Instr::DrawText(point, lines.join("\n")));
.push(Instr::DrawText(point, result.text));
lines_len as u16
result.lines_len
}
}