Skip to content

Commit ce2e584

Browse files
committed
fixed bugs in multi line pr
1 parent a51fb26 commit ce2e584

File tree

1 file changed

+85
-42
lines changed

1 file changed

+85
-42
lines changed

src/components/textinput.rs

+85-42
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ pub struct TextInputComponent {
4949
}
5050

5151
impl TextInputComponent {
52-
///
5352
pub fn new(
5453
theme: SharedTheme,
5554
key_config: SharedKeyConfig,
@@ -103,19 +102,28 @@ impl TextInputComponent {
103102
fn insert_new_line(&mut self) {
104103
const BORDER_SIZE: usize = 1;
105104

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+
106116
self.msg.insert(self.cursor_position, '\n');
107117
self.incr_cursor();
108118
self.scroll_max += 1;
109119

110120
// if the text box height increased,
111-
// componsate by scrolling up one
121+
// compensate by scrolling up one
112122
if self.scroll_max
113123
< (self.frame_height.get())
114124
.saturating_sub(BORDER_SIZE * 2)
115-
//&& self.scroll_max >= 3
116125
{
117126
self.scroll_top = self.scroll_top.saturating_sub(1);
118-
//self.cur_line = self.cur_line.saturating_sub(1);
119127
}
120128
}
121129

@@ -167,24 +175,46 @@ impl TextInputComponent {
167175
/// Move the cursor up a line.
168176
/// Only for multi-line textinputs
169177
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+
172184
for (i, c) in self.msg.chars().enumerate() {
173185
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;
176191
}
177192

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+
}
179200
break;
180201
}
181202
}
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);
188218
}
189219

190220
while !self.msg.is_char_boundary(self.cursor_position) {
@@ -199,51 +229,64 @@ impl TextInputComponent {
199229
/// Move the cursor down a line.
200230
/// Only for multi-line textinputs
201231
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;
207237

208238
for (i, c) in self.msg.chars().enumerate() {
209239
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;
215247
}
216248
}
217249

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;
224264
}
225265
}
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);
231266

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)
234279
{
235-
self.cursor_position += 1;
280+
self.cursor_position = middle_line_end + 1;
236281
}
237282

238283
if self.cursor_position < self.msg.len() {
239284
while !self.msg.is_char_boundary(self.cursor_position) {
240285
self.cursor_position += 1;
241286
}
242-
} else {
243-
self.cursor_position = self.msg.len().saturating_sub(1);
244287
}
245288

246-
if self.cur_line < self.scroll_max.saturating_sub(2) {
289+
if self.cur_line < self.scroll_max {
247290
self.cur_line += 1;
248291
if self.cur_line
249292
> self.scroll_top

0 commit comments

Comments
 (0)