From 6a05d8680109d2a8ea954d0a08e39180b84ef467 Mon Sep 17 00:00:00 2001 From: meehl <12648828+meehl@users.noreply.github.com> Date: Thu, 14 May 2026 10:35:14 +0200 Subject: [PATCH] Adjust index to account for color escape sequences get_text_index_at_cursor() returns an index into the stripped string, but PoB expects an index into the original unstripped text. Accumulate the total length of all escape sequences preceding the cursor position and add it to the returned index. Fixes #45 --- src/api/rendering.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/api/rendering.rs b/src/api/rendering.rs index 7fbe670..1b5e565 100644 --- a/src/api/rendering.rs +++ b/src/api/rendering.rs @@ -445,14 +445,29 @@ fn get_index_at_cur( let font_type = font_type.parse::()?; let job = build_layout_job(&text, Srgba::WHITE, font_type, line_height, None); - let index = ctx.fonts().get_text_index_at_cursor( + let index_stripped = ctx.fonts().get_text_index_at_cursor( job, Point::new(cur_x, cur_y), ctx.window().scale_factor(), ); - // convert to lua's 1-based indexing - Ok(index + 1) + // build_layout_job() strips all color escape strings from the original string. The + // resulting [`LayoutJob`] is then passed to get_text_index_at_cursor() which returns an + // index into the **stripped* string. + // But PoB expects an index into the **original, unstripped** text. Therefore we need to add + // the length of all color escapes up until the cursor position to return the right value. + // + // TODO: avoid matching and iterating over string twice + let mut color_escapes_total_length = 0; + for capture in ESCAPE_STR_REGEX.find_iter(&text) { + if capture.start() - color_escapes_total_length > index_stripped { + break; + } + color_escapes_total_length += capture.len(); + } + + // add length of color escapes and convert to lua's 1-based indexing + Ok(index_stripped + color_escapes_total_length + 1) } pub static ESCAPE_STR_REGEX: LazyLock =