src/global/functions/remove_word.rs
48 lines · 1131 bytes
1use tui_additions::widgets::TextField;
2
3pub fn remove_word(field: &mut TextField) {
4 if field.content[0..field.cursor].is_empty() {
5 return;
6 }
7
8 let before = field.content[..field.cursor]
9 .split_whitespace()
10 .collect::<Vec<_>>();
11 let before = if before.is_empty() {
12 String::new()
13 } else {
14 before[0..before.len() - 1].join(" ")
15 };
16
17 field.content = format!("{}{}", before, &field.content[field.cursor..]);
18 field.cursor = before.len();
19}
20
21pub fn previous_word(field: &mut TextField) {
22 let bytes = field.content.as_bytes();
23 while field.cursor != 0 {
24 field.cursor -= 1;
25 if bytes[field.cursor].is_ascii_whitespace() {
26 return;
27 }
28 }
29
30 field.cursor = 0;
31}
32
33pub fn next_word(field: &mut TextField) {
34 let bytes = field.content.as_bytes();
35 for (i, c) in bytes
36 .iter()
37 .enumerate()
38 .take(field.content.len())
39 .skip(field.cursor + 1)
40 {
41 if c.is_ascii_whitespace() {
42 field.cursor = i;
43 return;
44 }
45 }
46
47 field.cursor = field.content.len();
48}