From 6d954bd1b728b18529565a01c150494cc8c1e618 Mon Sep 17 00:00:00 2001 From: Grant Horner Date: Wed, 22 Apr 2026 12:13:42 -0400 Subject: [PATCH] cursor line wrapping --- edit.jai | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/edit.jai b/edit.jai index 093415f..13d7a8b 100644 --- a/edit.jai +++ b/edit.jai @@ -81,7 +81,7 @@ render_edit_buffer :: () { for line_buffer: visible_lines { Simp.set_shader_for_text(); - line_str := builder_to_string(*line_buffer, do_reset = false,, temp); + line_str := tbuilder_to_string(*line_buffer); prefix: string; suffix: string; @@ -102,17 +102,12 @@ render_edit_buffer :: () { if has_cursor_on_line { Simp.set_shader_for_color(); - start := ifx suffix.count == 0 then Left_Max else prefix_width + Left_Max; + start := ifx cursor_x == 0 then Left_Max else prefix_width + Left_Max; Simp.immediate_quad( cast(float) start, cast(float) top_of_line + line_height, cast(float) start + 1, cast(float) top_of_line, White); - - print("----------------\n"); - print("cursor_x: %\n", cursor_x); - print("Prefix width: %\n", prefix_width); - print("Suffix: %\n", suffix); Simp.set_shader_for_text(); Simp.prepare_text(my_font, suffix); Simp.draw_prepared_text(my_font, start, top_of_line, White); @@ -140,8 +135,6 @@ handle_window_resizes :: (window: Window_Type) { handle_arrow :: (key_code: Input.Key_Code) { visible_lines := get_visible_lines(); - max_chars :: 100; - if key_code == { case .ARROW_UP; cursor_y = ifx cursor_y == 0 @@ -152,14 +145,24 @@ handle_arrow :: (key_code: Input.Key_Code) { then visible_lines.count - 1 else cursor_y + 1; - case .ARROW_LEFT; - cursor_x = ifx cursor_x == 0 - then 0 - else cursor_x - 1; + case .ARROW_LEFT; + if cursor_x == 0 && cursor_y != 0 { + prev_line := visible_lines[cursor_y - 1]; + len := builder_string_length(*prev_line); + cursor_y = max(0, cursor_y - 1); + cursor_x = len; + } else if cursor_x != 0 { + cursor_x -= 1; + } case .ARROW_RIGHT; - cursor_x = ifx cursor_x == max_chars - then max_chars - else cursor_x + 1; + line := visible_lines[cursor_y]; + len := builder_string_length(*line); + if cursor_x == len { + cursor_x = 0; + cursor_y += 1; + } else { + cursor_x += 1; + } } } @@ -252,6 +255,11 @@ my_init_fonts :: () { assert(my_font != null); } +tbuilder_to_string :: (builder: *String_Builder) -> string { + s := builder_to_string(builder, do_reset = false,, temp); + return s; +} + #import "Basic"; #import "File"; #import "Math";