fix(tui): use char-level wrapping instead of word-level for log lines

This commit is contained in:
dailz
2026-04-11 09:02:01 +08:00
parent 3c042e20a1
commit 8a664c02c8

View File

@@ -3,6 +3,45 @@ use std::time::Instant;
use log_viewer_core::io::file_reader::FileReader; use log_viewer_core::io::file_reader::FileReader;
/// Split a line into chunks of exactly `width` characters (display columns).
/// For a log viewer, we want character-level wrapping, not word-level.
fn wrap_line_chars(line: &str, width: usize) -> Vec<String> {
if width == 0 {
return vec![String::new()];
}
if line.is_empty() {
return vec![String::new()];
}
let mut result = Vec::new();
let mut row = String::new();
let mut col = 0;
for ch in line.chars() {
let w = if ch == '\t' { 4 } else { 1 };
if col + w > width && !row.is_empty() {
result.push(std::mem::take(&mut row));
col = 0;
}
if ch == '\t' {
row.push_str(" ");
col += 4;
} else {
row.push(ch);
col += w;
}
if col >= width {
result.push(std::mem::take(&mut row));
col = 0;
}
}
if !row.is_empty() {
result.push(row);
}
if result.is_empty() {
result.push(String::new());
}
result
}
pub struct App { pub struct App {
pub should_quit: bool, pub should_quit: bool,
@@ -77,8 +116,7 @@ impl App {
for i in 0..line_count { for i in 0..line_count {
let line = self.get_line(i).unwrap_or(""); let line = self.get_line(i).unwrap_or("");
let wrapped: Vec<std::borrow::Cow<'_, str>> = textwrap::wrap(line, width); let wrapped = wrap_line_chars(line, width);
let wrapped: Vec<String> = wrapped.into_iter().map(|c| c.into_owned()).collect();
let height = wrapped.len().max(1); let height = wrapped.len().max(1);
self.wrap_cache.push(wrapped); self.wrap_cache.push(wrapped);
self.visual_heights.push(height); self.visual_heights.push(height);