@@ -49,7 +49,6 @@ pub struct TextInputComponent {
49
49
}
50
50
51
51
impl TextInputComponent {
52
- ///
53
52
pub fn new (
54
53
theme : SharedTheme ,
55
54
key_config : SharedKeyConfig ,
@@ -103,19 +102,28 @@ impl TextInputComponent {
103
102
fn insert_new_line ( & mut self ) {
104
103
const BORDER_SIZE : usize = 1 ;
105
104
105
+ // if the last line is just a new line with no
106
+ // characters, and the cursor position is just before
107
+ //it (on the previous line), shift to the new line
108
+ if self . msg . ends_with ( '\n' )
109
+ && self . cursor_position
110
+ == self . msg . chars ( ) . count ( ) . saturating_sub ( 1 )
111
+ {
112
+ self . incr_cursor ( ) ;
113
+ return ;
114
+ }
115
+
106
116
self . msg . insert ( self . cursor_position , '\n' ) ;
107
117
self . incr_cursor ( ) ;
108
118
self . scroll_max += 1 ;
109
119
110
120
// if the text box height increased,
111
- // componsate by scrolling up one
121
+ // compensate by scrolling up one
112
122
if self . scroll_max
113
123
< ( self . frame_height . get ( ) )
114
124
. saturating_sub ( BORDER_SIZE * 2 )
115
- //&& self.scroll_max >= 3
116
125
{
117
126
self . scroll_top = self . scroll_top . saturating_sub ( 1 ) ;
118
- //self.cur_line = self.cur_line.saturating_sub(1);
119
127
}
120
128
}
121
129
@@ -167,24 +175,46 @@ impl TextInputComponent {
167
175
/// Move the cursor up a line.
168
176
/// Only for multi-line textinputs
169
177
fn line_up_cursor ( & mut self ) {
170
- let mut nearest_newline: usize = 0 ;
171
- let mut prev_line_newline_loc = 0 ;
178
+ let mut top_line_start: usize = 0 ;
179
+ let mut top_line_end: usize = 0 ;
180
+ let mut middle_line_start: usize = 0 ;
181
+ let mut middle_line_end: usize = 0 ;
182
+ let mut bottom_line_start: usize = 0 ;
183
+
172
184
for ( i, c) in self . msg . chars ( ) . enumerate ( ) {
173
185
if c == '\n' {
174
- prev_line_newline_loc = nearest_newline;
175
- nearest_newline = i;
186
+ top_line_start = middle_line_start;
187
+ top_line_end = middle_line_end;
188
+ middle_line_start = bottom_line_start;
189
+ middle_line_end = i. saturating_sub ( 1 ) ;
190
+ bottom_line_start = i;
176
191
}
177
192
178
- if i >= self . cursor_position {
193
+ if i == self . cursor_position {
194
+ //for when cursor position is on a new line just before text
195
+ if c == '\n' {
196
+ bottom_line_start = middle_line_start;
197
+ middle_line_start = top_line_start;
198
+ middle_line_end = top_line_end;
199
+ }
179
200
break ;
180
201
}
181
202
}
182
- self . cursor_position = ( prev_line_newline_loc
183
- + self . cursor_position )
184
- . saturating_sub ( nearest_newline) ;
185
- if prev_line_newline_loc == 0 {
186
- self . cursor_position = 0 ;
187
- //self.cursor_position.saturating_sub(1);
203
+
204
+ let mut cursor_position_in_line =
205
+ self . cursor_position . saturating_sub ( bottom_line_start) ;
206
+
207
+ //for when moving up to first line
208
+ if middle_line_start == 0 {
209
+ cursor_position_in_line =
210
+ cursor_position_in_line. saturating_sub ( 1 ) ;
211
+ }
212
+ self . cursor_position =
213
+ middle_line_start. saturating_add ( cursor_position_in_line) ;
214
+
215
+ //for when moving uo to a new line from a line with characters
216
+ if self . cursor_position > middle_line_end {
217
+ self . cursor_position = middle_line_end. saturating_add ( 1 ) ;
188
218
}
189
219
190
220
while !self . msg . is_char_boundary ( self . cursor_position ) {
@@ -199,51 +229,64 @@ impl TextInputComponent {
199
229
/// Move the cursor down a line.
200
230
/// Only for multi-line textinputs
201
231
fn line_down_cursor ( & mut self ) {
202
- //
203
- let mut nearest_newline : usize = 0 ;
204
- let mut prev_line_newline_loc = 0 ;
205
-
206
- let mut chars_not_printed = 0 ;
232
+ let mut top_line_start : usize = 0 ;
233
+ let mut middle_line_start : usize = 0 ;
234
+ let mut middle_line_end : usize = 0 ;
235
+ let mut bottom_line_start : usize = 0 ;
236
+ let mut drop_count : usize = 0 ;
207
237
208
238
for ( i, c) in self . msg . chars ( ) . enumerate ( ) {
209
239
if c == '\n' {
210
- chars_not_printed = 0 ;
211
- prev_line_newline_loc = nearest_newline;
212
- nearest_newline = i;
213
- if nearest_newline > self . cursor_position {
214
- break ;
240
+ top_line_start = middle_line_start;
241
+ middle_line_start = bottom_line_start;
242
+ middle_line_end = i. saturating_sub ( 1 ) ;
243
+ bottom_line_start = i;
244
+
245
+ if i >= self . cursor_position {
246
+ drop_count += 1 ;
215
247
}
216
248
}
217
249
218
- // To capture unicode multi-byte characters
219
- //chars_not_printed += c.len_utf8() - 1;
220
- if !self . msg . is_char_boundary ( i) {
221
- // self.msg.is_char_boundary(i) c.is_alphanumeric() {
222
- // unprintable
223
- chars_not_printed += 1 ;
250
+ if drop_count == 2 {
251
+ break ;
252
+ }
253
+
254
+ if i == self . msg . len ( ) . saturating_sub ( 1 ) && c != '\n' {
255
+ top_line_start = middle_line_start;
256
+ middle_line_start = bottom_line_start;
257
+ middle_line_end = i. saturating_sub ( 1 ) ;
258
+ } else if i == self . msg . len ( ) . saturating_sub ( 1 )
259
+ && c == '\n'
260
+ {
261
+ top_line_start = middle_line_start;
262
+ middle_line_start = bottom_line_start;
263
+ middle_line_end = bottom_line_start;
224
264
}
225
265
}
226
- self . cursor_position = self
227
- . cursor_position
228
- . saturating_sub ( prev_line_newline_loc)
229
- . saturating_add ( nearest_newline)
230
- . saturating_add ( chars_not_printed) ;
231
266
232
- if prev_line_newline_loc == 0
233
- && self . cursor_position < self . msg . len ( ) . saturating_sub ( 1 )
267
+ let mut cursor_position_in_line =
268
+ self . cursor_position . saturating_sub ( top_line_start) ;
269
+
270
+ if top_line_start == 0 {
271
+ cursor_position_in_line += 1 ;
272
+ }
273
+ self . cursor_position =
274
+ middle_line_start. saturating_add ( cursor_position_in_line) ;
275
+
276
+ if self . cursor_position > middle_line_end
277
+ && self . cursor_position
278
+ != middle_line_end. saturating_add ( 1 )
234
279
{
235
- self . cursor_position += 1 ;
280
+ self . cursor_position = middle_line_end + 1 ;
236
281
}
237
282
238
283
if self . cursor_position < self . msg . len ( ) {
239
284
while !self . msg . is_char_boundary ( self . cursor_position ) {
240
285
self . cursor_position += 1 ;
241
286
}
242
- } else {
243
- self . cursor_position = self . msg . len ( ) . saturating_sub ( 1 ) ;
244
287
}
245
288
246
- if self . cur_line < self . scroll_max . saturating_sub ( 2 ) {
289
+ if self . cur_line < self . scroll_max {
247
290
self . cur_line += 1 ;
248
291
if self . cur_line
249
292
> self . scroll_top
0 commit comments