cosmic/widget/text_input/
value.rs1use unicode_segmentation::UnicodeSegmentation;
6
7#[derive(Default, Debug, Clone, PartialEq)]
12pub struct Value {
13 graphemes: Vec<String>,
14}
15
16impl Value {
17 pub fn new(string: &str) -> Self {
19 let graphemes = UnicodeSegmentation::graphemes(string, true)
20 .map(String::from)
21 .collect();
22
23 Self { graphemes }
24 }
25
26 #[must_use]
30 #[inline]
31 pub fn is_empty(&self) -> bool {
32 self.len() == 0
33 }
34
35 #[must_use]
37 #[inline]
38 pub fn len(&self) -> usize {
39 self.graphemes.len()
40 }
41
42 #[must_use]
45 pub fn previous_start_of_word(&self, index: usize) -> usize {
46 let previous_string = &self.graphemes[..index.min(self.graphemes.len())].concat();
47
48 UnicodeSegmentation::split_word_bound_indices(previous_string as &str)
49 .rfind(|(_, word)| !word.trim_start().is_empty())
50 .map_or(0, |(i, previous_word)| {
51 index
52 - UnicodeSegmentation::graphemes(previous_word, true).count()
53 - UnicodeSegmentation::graphemes(
54 &previous_string[i + previous_word.len()..] as &str,
55 true,
56 )
57 .count()
58 })
59 }
60
61 #[must_use]
64 pub fn next_end_of_word(&self, index: usize) -> usize {
65 let next_string = &self.graphemes[index..].concat();
66
67 UnicodeSegmentation::split_word_bound_indices(next_string as &str)
68 .find(|(_, word)| !word.trim_start().is_empty())
69 .map_or(self.len(), |(i, next_word)| {
70 index
71 + UnicodeSegmentation::graphemes(next_word, true).count()
72 + UnicodeSegmentation::graphemes(&next_string[..i] as &str, true).count()
73 })
74 }
75
76 #[must_use]
79 #[inline]
80 pub fn select(&self, start: usize, end: usize) -> Self {
81 let graphemes = self.graphemes[start.min(self.len())..end.min(self.len())].to_vec();
82
83 Self { graphemes }
84 }
85
86 #[must_use]
89 #[inline]
90 pub fn until(&self, index: usize) -> Self {
91 let graphemes = self.graphemes[..index.min(self.len())].to_vec();
92
93 Self { graphemes }
94 }
95
96 #[inline]
98 pub fn insert(&mut self, index: usize, c: char) {
99 self.graphemes.insert(index, c.to_string());
100
101 self.graphemes = UnicodeSegmentation::graphemes(&self.to_string() as &str, true)
102 .map(String::from)
103 .collect();
104 }
105
106 #[inline]
108 pub fn insert_many(&mut self, index: usize, mut value: Value) {
109 let _ = self
110 .graphemes
111 .splice(index..index, value.graphemes.drain(..));
112 }
113
114 #[inline]
116 pub fn remove(&mut self, index: usize) {
117 let _ = self.graphemes.remove(index);
118 }
119
120 #[inline]
122 pub fn remove_many(&mut self, start: usize, end: usize) {
123 let _ = self.graphemes.splice(start..end, std::iter::empty());
124 }
125
126 #[must_use]
129 pub fn secure(&self) -> Self {
130 Self {
131 graphemes: std::iter::repeat_n(String::from("•"), self.graphemes.len()).collect(),
132 }
133 }
134
135 #[must_use]
137 pub fn byte_index_at_grapheme(&self, grapheme_index: usize) -> usize {
138 self.graphemes[..grapheme_index.min(self.graphemes.len())]
139 .iter()
140 .map(|g| g.len())
141 .sum()
142 }
143
144 #[must_use]
147 pub fn rfind_char(&self, ch: char) -> Option<usize> {
148 let needle = ch.to_string();
149 self.graphemes.iter().rposition(|g| g == &needle)
150 }
151
152 #[must_use]
154 pub fn grapheme_index_at_byte(&self, byte_index: usize) -> usize {
155 let mut bytes = 0;
156 for (i, g) in self.graphemes.iter().enumerate() {
157 if bytes >= byte_index {
158 return i;
159 }
160 bytes += g.len();
161 }
162
163 self.graphemes.len()
164 }
165}
166
167impl std::fmt::Display for Value {
168 #[inline]
169 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
170 f.write_str(&self.graphemes.concat())
171 }
172}