Compare commits
9 Commits
e9f75ce3b1
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
10323ce814 | ||
|
|
c1a931551b | ||
|
|
dfc016c348 | ||
|
|
19a3b877f9 | ||
|
|
5cb56dafd8 | ||
|
|
e99861c76d | ||
|
|
a43ef673b0 | ||
|
|
70f930eef7 | ||
|
|
463c53148b |
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -2358,6 +2358,7 @@ dependencies = [
|
||||
"ratatui",
|
||||
"serde_json",
|
||||
"tempfile",
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -223,6 +223,9 @@ pub fn compute_line_visual_height(
|
||||
}
|
||||
if json_format {
|
||||
let formatted = format_json_line(line_text);
|
||||
if formatted.len() > MAX_WRAP_INPUT_LEN {
|
||||
return 1;
|
||||
}
|
||||
compute_text_visual_height(&formatted, terminal_width)
|
||||
} else {
|
||||
compute_text_visual_height(line_text, terminal_width)
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
/// Maximum input length for wrap/format operations (10 MB).
|
||||
/// Lines exceeding this are returned as-is to avoid pathological cases.
|
||||
/// Callers should check against this constant before invoking `wrap_line_chars`
|
||||
/// to avoid pathological cases on oversized lines.
|
||||
pub const MAX_WRAP_INPUT_LEN: usize = 10 * 1024 * 1024;
|
||||
|
||||
/// Column spacing for tab stop alignment.
|
||||
const TAB_WIDTH: usize = 4;
|
||||
|
||||
/// Split a line into chunks of exactly `width` display columns.
|
||||
/// For a log viewer, we want character-level wrapping, not word-level.
|
||||
/// Uses `unicode-width` for correct CJK/emoji/zero-width handling.
|
||||
/// Tab characters expand to the next tab-stop boundary and split across
|
||||
/// rows when the expansion exceeds the remaining width.
|
||||
pub fn wrap_line_chars(line: &str, width: usize) -> Vec<String> {
|
||||
use unicode_width::UnicodeWidthChar;
|
||||
|
||||
@@ -18,9 +24,24 @@ pub fn wrap_line_chars(line: &str, width: usize) -> Vec<String> {
|
||||
let mut row = String::new();
|
||||
let mut col = 0;
|
||||
for ch in line.chars() {
|
||||
let w = if ch == '\t' {
|
||||
4
|
||||
} else if ch.is_control() {
|
||||
if ch == '\t' {
|
||||
let tab_stop = TAB_WIDTH - (col % TAB_WIDTH);
|
||||
let mut remaining = tab_stop;
|
||||
while remaining > 0 {
|
||||
let avail = width.saturating_sub(col);
|
||||
let fill = remaining.min(avail);
|
||||
for _ in 0..fill {
|
||||
row.push(' ');
|
||||
}
|
||||
col += fill;
|
||||
remaining -= fill;
|
||||
if col >= width {
|
||||
result.push(std::mem::take(&mut row));
|
||||
col = 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let w = if ch.is_control() {
|
||||
// Control characters (except tab): width 0, still pushed to preserve content.
|
||||
// Visible rendering is the caller's responsibility.
|
||||
0
|
||||
@@ -31,18 +52,14 @@ pub fn wrap_line_chars(line: &str, width: usize) -> Vec<String> {
|
||||
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);
|
||||
}
|
||||
@@ -107,7 +124,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_wrap_with_tab() {
|
||||
let result = wrap_line_chars("a\tb", 4);
|
||||
assert_eq!(result, vec!["a", " ", "b"]);
|
||||
assert_eq!(result, vec!["a ", "b"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -187,4 +204,31 @@ mod tests {
|
||||
let result = wrap_line_chars("你好", 1);
|
||||
assert_eq!(result, vec!["你", "好"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tab_narrow_width() {
|
||||
let result = wrap_line_chars("\t", 2);
|
||||
assert_eq!(result, vec![" ", " "]);
|
||||
let result = wrap_line_chars("\t", 1);
|
||||
assert_eq!(result, vec![" ", " ", " ", " "]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tab_stop_alignment() {
|
||||
assert_eq!(wrap_line_chars("a\tb", 8), vec!["a b"]);
|
||||
assert_eq!(wrap_line_chars("ab\t", 4), vec!["ab "]);
|
||||
assert_eq!(wrap_line_chars("abc\tb", 8), vec!["abc b"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tab_at_line_boundary() {
|
||||
let result = wrap_line_chars("a\tb", 4);
|
||||
assert_eq!(result, vec!["a ", "b"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tab_regression_ab_tab() {
|
||||
let result = wrap_line_chars("ab\t", 4);
|
||||
assert_eq!(result, vec!["ab "]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,13 +100,21 @@ fn detect_level_from_text(line: &str) -> Option<LogLevel> {
|
||||
)
|
||||
}
|
||||
|
||||
// ─── is_ident_char ─────────────────────────────────────────────────────────
|
||||
/// Whether a byte looks like an ASCII identifier continuation character
|
||||
/// (letter / digit / underscore). Log-level keywords must NOT be adjacent to
|
||||
/// such characters to count as a valid word boundary.
|
||||
fn is_ident_char(b: u8) -> bool {
|
||||
b.is_ascii_alphanumeric() || b == b'_'
|
||||
}
|
||||
|
||||
// ─── is_word_boundary ───────────────────────────────────────────────────────
|
||||
/// Check that the match at `start..start+len` is surrounded by non-alphabetic
|
||||
/// Check that the match at `start..start+len` is surrounded by non-identifier
|
||||
/// characters (or the string edge).
|
||||
fn is_word_boundary(text: &str, start: usize, len: usize) -> bool {
|
||||
let before_ok = start == 0 || !text.as_bytes()[start - 1].is_ascii_alphabetic();
|
||||
let before_ok = start == 0 || !is_ident_char(text.as_bytes()[start - 1]);
|
||||
let after_idx = start + len;
|
||||
let after_ok = after_idx >= text.len() || !text.as_bytes()[after_idx].is_ascii_alphabetic();
|
||||
let after_ok = after_idx >= text.len() || !is_ident_char(text.as_bytes()[after_idx]);
|
||||
before_ok && after_ok
|
||||
}
|
||||
|
||||
@@ -209,4 +217,36 @@ mod tests {
|
||||
let line = format!("{prefix} ERROR something");
|
||||
assert_eq!(detect_level(&line), Some(LogLevel::Error));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_boundary_rejects_trailing_digits() {
|
||||
assert_eq!(detect_level("ERROR123"), None);
|
||||
assert_eq!(detect_level("WARN2: bad"), None);
|
||||
assert_eq!(detect_level("ERR2"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_boundary_rejects_underscore() {
|
||||
assert_eq!(detect_level("INFO_foo"), None);
|
||||
assert_eq!(detect_level("DBG_value=5"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_boundary_rejects_leading_digits_and_underscore() {
|
||||
assert_eq!(detect_level("123ERROR: fail"), None);
|
||||
assert_eq!(detect_level("foo_ERROR: fail"), None);
|
||||
assert_eq!(detect_level("1WRN"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_boundary_accepts_valid_suffixes() {
|
||||
assert_eq!(detect_level("ERROR: fail"), Some(LogLevel::Error));
|
||||
assert_eq!(detect_level("[ERROR] fail"), Some(LogLevel::Error));
|
||||
assert_eq!(detect_level("ERROR fail"), Some(LogLevel::Error));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_boundary_camel_case_regression() {
|
||||
assert_eq!(detect_level("errorLevel"), None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ anyhow.workspace = true
|
||||
log-viewer-core.workspace = true
|
||||
serde_json.workspace = true
|
||||
crossbeam-channel.workspace = true
|
||||
unicode-width.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = { workspace = true }
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -88,9 +88,21 @@ pub fn render(frame: &mut ratatui::Frame, app: &mut App) {
|
||||
}
|
||||
|
||||
// ── Status bar ─────────────────────────────────────────────────
|
||||
let status_text = if app.mode == AppMode::Settings {
|
||||
" j/k:navigate ←/→:change 1-8:jump Enter:save Esc:cancel"
|
||||
} else if app.is_error() {
|
||||
if app.mode == AppMode::Settings {
|
||||
if let Some(ref err) = app.settings_error {
|
||||
frame.render_widget(
|
||||
Paragraph::new(err.as_str()).style(Style::default().fg(Color::Red)),
|
||||
outer[2],
|
||||
);
|
||||
} else {
|
||||
frame.render_widget(
|
||||
Paragraph::new(" j/k:navigate ←/→:change 1-8:jump Enter:save Esc:cancel"),
|
||||
outer[2],
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
let status_text = if app.is_error() {
|
||||
" Press q to quit"
|
||||
} else if app.is_loading() {
|
||||
let pct = app.loading_progress().map_or(0, |p| p as usize);
|
||||
@@ -130,8 +142,8 @@ pub fn render_settings(frame: &mut ratatui::Frame, app: &mut App, area: ratatui:
|
||||
|
||||
let popup_w = ((area.width as u32 * 4 / 5).max(40)).min(area.width as u32) as u16;
|
||||
let popup_h = ((area.height as u32 * 4 / 5).max(14)).min(area.height as u32) as u16;
|
||||
let popup_x = area.width.saturating_sub(popup_w) / 2;
|
||||
let popup_y = area.height.saturating_sub(popup_h) / 2;
|
||||
let popup_x = area.x.saturating_add(area.width.saturating_sub(popup_w) / 2);
|
||||
let popup_y = area.y.saturating_add(area.height.saturating_sub(popup_h) / 2);
|
||||
let popup = ratatui::layout::Rect::new(popup_x, popup_y, popup_w, popup_h);
|
||||
|
||||
let block = Block::new().borders(Borders::ALL).title(" Color Settings ");
|
||||
@@ -493,4 +505,74 @@ mod tests {
|
||||
let _ = std::fs::remove_file(&path);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
// ── Issue #31: Settings popup area offset tests ────────────────
|
||||
|
||||
/// Helper: enter settings mode and render to buffer.
|
||||
fn render_settings_to_buffer(app: &mut App, width: u16, height: u16) -> ratatui::buffer::Buffer {
|
||||
app.mode = crate::app::AppMode::Settings;
|
||||
app.settings_draft = app.color_config.clone();
|
||||
render_to_buffer(app, width, height)
|
||||
}
|
||||
|
||||
/// Find the top-left corner of the popup border by scanning for '┌'.
|
||||
fn find_popup_top_left(buf: &ratatui::buffer::Buffer, width: u16, height: u16) -> Option<(u16, u16)> {
|
||||
for row in 0..height {
|
||||
for col in 0..width {
|
||||
if buf.cell((col, row)).unwrap().symbol() == "┌" {
|
||||
return Some((col, row));
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_settings_popup_includes_area_offset() {
|
||||
// In an 80x24 frame with Layout [Length(1), Min(1), Length(1)]:
|
||||
// outer[0] = title bar -> y=0
|
||||
// outer[1] = content -> y=1, height=22
|
||||
// outer[2] = status bar -> y=23
|
||||
// The popup is centered within outer[1], so its y must be >= outer[1].y (which is 1).
|
||||
let mut app = App::new();
|
||||
let buf = render_settings_to_buffer(&mut app, 80, 24);
|
||||
|
||||
let (_px, py) = find_popup_top_left(&buf, 80, 24)
|
||||
.expect("popup border '┌' should be rendered");
|
||||
|
||||
// outer[1].y == 1; the popup is centered inside a 22-row area,
|
||||
// so popup_y must be at least 1 (not 0).
|
||||
assert!(
|
||||
py >= 1,
|
||||
"popup top row should account for area.y offset, got y={py} (expected >= 1)"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_settings_popup_horizontal_centering_uses_area_x() {
|
||||
// outer[1].x is 0 for this layout, so this mainly verifies the popup
|
||||
// is centered and not shifted left. A non-zero area.x layout would
|
||||
// need a different layout to trigger, but the formula is the same.
|
||||
let mut app = App::new();
|
||||
let buf = render_settings_to_buffer(&mut app, 80, 24);
|
||||
|
||||
let (px, _py) = find_popup_top_left(&buf, 80, 24)
|
||||
.expect("popup border '┌' should be rendered");
|
||||
|
||||
// popup_w = 80*4/5 = 64, centered: (80-64)/2 = 8
|
||||
assert_eq!(
|
||||
px, 8,
|
||||
"popup should start at x=8 (centered 64-wide popup in 80-col area)"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_settings_popup_small_frame_no_panic() {
|
||||
// Frame smaller than the min popup size (40x14) should not panic.
|
||||
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||
let mut app = App::new();
|
||||
let _buf = render_settings_to_buffer(&mut app, 30, 10);
|
||||
}));
|
||||
assert!(result.is_ok(), "rendering settings in a small frame should not panic");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user