1
0
Fork 0

fix: try to better count lines

This commit is contained in:
əlemi 2025-01-14 01:13:39 +01:00
parent 2280ec8512
commit 2dc8cce0e1
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -19,22 +19,38 @@ impl History {
}
fn down(&mut self, x: u16) {
if self.count < self.height { return };
let delta = self.count - (self.height/2);
if self.offset >= delta { return }
self.offset += std::cmp::min(x, delta - self.offset);
if self.count < self.height {
self.offset = 0;
return;
}
let overflow = self.count - self.height;
if self.offset >= overflow { return }
self.offset += std::cmp::min(x, overflow - self.offset);
}
fn recalculate(&mut self) {
self.count = 0;
for line in &self.lines {
let len = (format!("{line}").len() as u16 / self.width) + 1;
let len = lines_needed_with_wrap(&format!("{line}"), self.width as usize) + 1;
self.count += len;
}
self.down(self.count);
self.down(self.count); // make sure we're to the bottom
}
}
fn lines_needed_with_wrap(txt: &str, width: usize) -> u16 {
let mut lines = 1;
let mut curr = 0;
for word in txt.split_whitespace() {
if curr + word.len() > width {
lines += 1;
curr = 0;
}
curr += word.len() + 1;
}
lines
}
pub async fn run<T: ratatui::backend::Backend>(term: &mut Terminal<T>, args: crate::Args) -> Result<(), Box<dyn std::error::Error>> {
let mut chat = Chat::register(&args.server, args.token).await?;