11module gui
22
3+ // cursor_left moves the cursor position one character to the left in the text.
4+ // It decrements the position by one, but ensures the result never goes below
5+ // zero, effectively preventing the cursor from moving before the start of the
6+ // text. Returns the new cursor position.
37fn cursor_left (pos int ) int {
48 return int_max (0 , pos - 1 )
59}
610
11+ // cursor_right moves the cursor position one character to the right in wrapped
12+ // text. It increments the position by one, but ensures the result never exceeds
13+ // the total character count of all text lines combined, effectively preventing
14+ // the cursor from moving beyond the end of the text. Returns the new cursor
15+ // position.
716fn cursor_right (strs []string , pos int ) int {
817 return int_min (count_chars (strs), pos + 1 )
918}
1019
1120// cursor_up moves the cursor position up one line in wrapped text, attempting
12- // to maintain the same horizontal pixel offset. It locates the current line,
13- // moves to the previous line, and uses the cursor_offset to find the corresponding
14- // column position. If the previous line is shorter than the calculated column,
15- // the cursor moves to the end of that line. Returns the original position if
16- // already at the first line.
21+ // to maintain the same horizontal pixel offset. It locates the current line by
22+ // iterating through the wrapped text lines, moves to the previous line, and
23+ // uses the cursor_offset to find the corresponding column position. If the
24+ // cursor_offset is invalid (less than zero, typically -1), it is recomputed
25+ // from the current cursor position. If the previous line is shorter than the
26+ // calculated column, the cursor moves to the end of that line. Returns the
27+ // original position if already at the first line.
1728fn cursor_up (shape Shape, cursor_pos int , cursor_offset f32 , window & Window) int {
1829 mut idx := 0
1930 mut offset := 0
@@ -39,7 +50,15 @@ fn cursor_up(shape Shape, cursor_pos int, cursor_offset f32, window &Window) int
3950 }
4051 }
4152
42- cursor_column := cursor_position_from_offset (shape.text_lines[p_idx], cursor_offset,
53+ // An offset of less than zero (usually -1) indicates that the offset was
54+ // invalidated, likely because of an edit operation like insert/delete.
55+ // Compute a new offset based on the current cursor position
56+ c_offset := match cursor_offset > = 0 {
57+ true { cursor_offset }
58+ else { offset_from_cursor_position (shape, cursor_pos, window) }
59+ }
60+
61+ cursor_column := cursor_position_from_offset (shape.text_lines[p_idx], c_offset,
4362 shape.text_style, window)
4463
4564 new_cursor_position := match true {
@@ -53,11 +72,14 @@ fn cursor_up(shape Shape, cursor_pos int, cursor_offset f32, window &Window) int
5372}
5473
5574// cursor_down moves the cursor position down one line in wrapped text, attempting
56- // to maintain the same horizontal pixel offset. It locates the current line,
57- // moves to the next line, and uses the cursor_offset to find the corresponding
58- // column position. If the next line is shorter than the calculated column,
59- // the cursor moves to the end of that line. Returns the original position if
60- // already at the last line.
75+ // to maintain the same horizontal pixel offset. It locates the current line by
76+ // iterating through the wrapped text lines, moves to the next line, and uses
77+ // the cursor_offset to find the corresponding column position. If the cursor_offset
78+ // is invalid (less than zero, typically -1), it is recomputed from the current
79+ // cursor position. If the next line is shorter than the calculated column, the
80+ // cursor position is adjusted: for lines ending with a newline, it moves to the
81+ // position before the newline; for the last line, it moves to the end of the line.
82+ // Returns the original position if already at the last line.
6183fn cursor_down (shape Shape, cursor_pos int , cursor_offset f32 , window & Window) int {
6284 mut idx := 0
6385 mut offset := 0
@@ -84,7 +106,15 @@ fn cursor_down(shape Shape, cursor_pos int, cursor_offset f32, window &Window) i
84106 }
85107 }
86108
87- cursor_column := cursor_position_from_offset (n_text, cursor_offset, shape.text_style,
109+ // An offset of less than zero (usually -1) indicates that the offset was
110+ // invalidated, likely because of an edit operation like insert/delete.
111+ // Compute a new offset based on the current cursor position
112+ c_offset := match cursor_offset > = 0 {
113+ true { cursor_offset }
114+ else { offset_from_cursor_position (shape, cursor_pos, window) }
115+ }
116+
117+ cursor_column := cursor_position_from_offset (n_text, c_offset, shape.text_style,
88118 window)
89119
90120 new_cursor_position := match true {
@@ -101,10 +131,16 @@ fn cursor_down(shape Shape, cursor_pos int, cursor_offset f32, window &Window) i
101131 return cursor_pos
102132}
103133
134+ // cursor_home moves the cursor to the beginning of the text by returning
135+ // position 0. This is equivalent to the "Home" key behavior, placing the
136+ // cursor at the start of the entire text content.
104137fn cursor_home () int {
105138 return 0
106139}
107140
141+ // cursor_end moves the cursor to the end of the text by returning the total
142+ // character count across all wrapped text lines. This is equivalent to the
143+ // "End" key behavior, placing the cursor at the end of the entire text content.
108144fn cursor_end (strs []string ) int {
109145 return count_chars (strs)
110146}
0 commit comments