iced_widget/text_input/
value.rs
1use unicode_segmentation::UnicodeSegmentation;
2
3#[derive(Debug, Clone)]
8pub struct Value {
9 graphemes: Vec<String>,
10}
11
12impl Value {
13 pub fn new(string: &str) -> Self {
15 let graphemes = UnicodeSegmentation::graphemes(string, true)
16 .map(String::from)
17 .collect();
18
19 Self { graphemes }
20 }
21
22 pub fn is_empty(&self) -> bool {
26 self.len() == 0
27 }
28
29 pub fn len(&self) -> usize {
31 self.graphemes.len()
32 }
33
34 pub fn previous_start_of_word(&self, index: usize) -> usize {
37 let previous_string =
38 &self.graphemes[..index.min(self.graphemes.len())].concat();
39
40 UnicodeSegmentation::split_word_bound_indices(previous_string as &str)
41 .filter(|(_, word)| !word.trim_start().is_empty())
42 .next_back()
43 .map(|(i, previous_word)| {
44 index
45 - UnicodeSegmentation::graphemes(previous_word, true)
46 .count()
47 - UnicodeSegmentation::graphemes(
48 &previous_string[i + previous_word.len()..] as &str,
49 true,
50 )
51 .count()
52 })
53 .unwrap_or(0)
54 }
55
56 pub fn next_end_of_word(&self, index: usize) -> usize {
59 let next_string = &self.graphemes[index..].concat();
60
61 UnicodeSegmentation::split_word_bound_indices(next_string as &str)
62 .find(|(_, word)| !word.trim_start().is_empty())
63 .map(|(i, next_word)| {
64 index
65 + UnicodeSegmentation::graphemes(next_word, true).count()
66 + UnicodeSegmentation::graphemes(
67 &next_string[..i] as &str,
68 true,
69 )
70 .count()
71 })
72 .unwrap_or(self.len())
73 }
74
75 pub fn select(&self, start: usize, end: usize) -> Self {
78 let graphemes =
79 self.graphemes[start.min(self.len())..end.min(self.len())].to_vec();
80
81 Self { graphemes }
82 }
83
84 pub fn until(&self, index: usize) -> Self {
87 let graphemes = self.graphemes[..index.min(self.len())].to_vec();
88
89 Self { graphemes }
90 }
91
92 pub fn insert(&mut self, index: usize, c: char) {
94 self.graphemes.insert(index, c.to_string());
95
96 self.graphemes =
97 UnicodeSegmentation::graphemes(&self.to_string() as &str, true)
98 .map(String::from)
99 .collect();
100 }
101
102 pub fn insert_many(&mut self, index: usize, mut value: Value) {
104 let _ = self
105 .graphemes
106 .splice(index..index, value.graphemes.drain(..));
107 }
108
109 pub fn remove(&mut self, index: usize) {
111 let _ = self.graphemes.remove(index);
112 }
113
114 pub fn remove_many(&mut self, start: usize, end: usize) {
116 let _ = self.graphemes.splice(start..end, std::iter::empty());
117 }
118
119 pub fn secure(&self) -> Self {
122 Self {
123 graphemes: std::iter::repeat(String::from("•"))
124 .take(self.graphemes.len())
125 .collect(),
126 }
127 }
128}
129
130impl std::fmt::Display for Value {
131 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132 f.write_str(&self.graphemes.concat())
133 }
134}