fix: try to better count lines
This commit is contained in:
parent
2280ec8512
commit
2dc8cce0e1
1 changed files with 22 additions and 6 deletions
28
src/app.rs
28
src/app.rs
|
@ -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?;
|
||||
|
|
Loading…
Add table
Reference in a new issue