Skip to main content

cursor_visual_position

Function cursor_visual_position 

Source
pub fn cursor_visual_position(buffer: &str, cursor: usize) -> (u16, u16)
Expand description

Convert a buffer + byte cursor offset into a (col, row) inside the edit area (zero-based, where col counts columns from the first editable cell, not including the box border).

The input renderer pads non-first lines with two spaces in place of the indicator to keep columns visually aligned, so per-line content always starts at the same x. We therefore only have to count \ns for the row, and the display width of the preceding chars on the active line for the column. Using chars().count() undercounts CJK / emoji / fullwidth glyphs (each takes 2 columns in a terminal), which made the cursor land in the middle of the previous double-width char rather than after it.