refactor(tui): deduplicate cursor_cell_offset into text_width module
Four view modules (search, command_palette, file_history, trace) each had their own copy of cursor_cell_offset / text_cell_width for converting a byte-offset cursor position to a display-column offset. Phase 5 introduced a proper text_width module with these functions; this commit removes the duplicates and rewires all call sites to use crate::text_width. - search.rs: removed local text_cell_width + cursor_cell_offset definitions - command_palette.rs: removed local cursor_cell_offset definition - file_history.rs: replaced inline chars().count() cursor calc with import - trace.rs: replaced inline chars().count() cursor calc with import
This commit is contained in:
@@ -9,6 +9,7 @@ use ftui::render::drawing::{BorderChars, Draw};
|
||||
use ftui::render::frame::Frame;
|
||||
|
||||
use crate::state::command_palette::CommandPaletteState;
|
||||
use crate::text_width::cursor_cell_offset;
|
||||
|
||||
use super::{ACCENT, BG_SURFACE, BORDER, TEXT, TEXT_MUTED};
|
||||
|
||||
@@ -16,14 +17,6 @@ fn text_cell_width(text: &str) -> u16 {
|
||||
text.chars().count().min(u16::MAX as usize) as u16
|
||||
}
|
||||
|
||||
fn cursor_cell_offset(query: &str, cursor: usize) -> u16 {
|
||||
let mut idx = cursor.min(query.len());
|
||||
while idx > 0 && !query.is_char_boundary(idx) {
|
||||
idx -= 1;
|
||||
}
|
||||
text_cell_width(&query[..idx])
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// render_command_palette
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -21,8 +21,9 @@ use ftui::render::cell::{Cell, PackedRgba};
|
||||
use ftui::render::drawing::Draw;
|
||||
use ftui::render::frame::Frame;
|
||||
|
||||
use crate::state::file_history::{FileHistoryResult, FileHistoryState};
|
||||
use super::common::truncate_str;
|
||||
use crate::state::file_history::{FileHistoryResult, FileHistoryState};
|
||||
use crate::text_width::cursor_cell_offset;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Colors (Flexoki palette)
|
||||
@@ -137,8 +138,7 @@ fn render_path_input(frame: &mut Frame<'_>, state: &FileHistoryState, x: u16, y:
|
||||
|
||||
// Cursor indicator.
|
||||
if state.path_focused {
|
||||
let cursor_col = state.path_input[..state.path_cursor].chars().count() as u16;
|
||||
let cursor_x = after_label + cursor_col;
|
||||
let cursor_x = after_label + cursor_cell_offset(&state.path_input, state.path_cursor);
|
||||
if cursor_x < max_x {
|
||||
let cursor_cell = Cell {
|
||||
fg: PackedRgba::rgb(0x10, 0x0F, 0x0F), // dark bg
|
||||
|
||||
@@ -18,24 +18,11 @@
|
||||
use ftui::core::geometry::Rect;
|
||||
use ftui::render::cell::Cell;
|
||||
use ftui::render::drawing::Draw;
|
||||
|
||||
/// Count display-width columns for a string (char count, not byte count).
|
||||
fn text_cell_width(text: &str) -> u16 {
|
||||
text.chars().count().min(u16::MAX as usize) as u16
|
||||
}
|
||||
|
||||
/// Convert a byte-offset cursor position to a display-column offset.
|
||||
fn cursor_cell_offset(query: &str, cursor: usize) -> u16 {
|
||||
let mut idx = cursor.min(query.len());
|
||||
while idx > 0 && !query.is_char_boundary(idx) {
|
||||
idx -= 1;
|
||||
}
|
||||
text_cell_width(&query[..idx])
|
||||
}
|
||||
use ftui::render::frame::Frame;
|
||||
|
||||
use crate::message::EntityKind;
|
||||
use crate::state::search::SearchState;
|
||||
use crate::text_width::cursor_cell_offset;
|
||||
|
||||
use super::{ACCENT, BG_SURFACE, BORDER, TEXT, TEXT_MUTED};
|
||||
|
||||
|
||||
@@ -135,8 +135,7 @@ fn render_path_input(frame: &mut Frame<'_>, state: &TraceState, x: u16, y: u16,
|
||||
|
||||
// Cursor.
|
||||
if state.path_focused {
|
||||
let cursor_col = state.path_input[..state.path_cursor].chars().count() as u16;
|
||||
let cursor_x = after_label + cursor_col;
|
||||
let cursor_x = after_label + cursor_cell_offset(&state.path_input, state.path_cursor);
|
||||
if cursor_x < max_x {
|
||||
let cursor_cell = Cell {
|
||||
fg: PackedRgba::rgb(0x10, 0x0F, 0x0F),
|
||||
|
||||
Reference in New Issue
Block a user