iced_widget/text_input/
value.rs1use 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 = &self.graphemes[..index.min(self.graphemes.len())].concat();
38
39 UnicodeSegmentation::split_word_bound_indices(previous_string as &str)
40 .rfind(|(_, word)| !word.trim_start().is_empty())
41 .map(|(i, previous_word)| {
42 index
43 - UnicodeSegmentation::graphemes(previous_word, true).count()
44 - UnicodeSegmentation::graphemes(
45 &previous_string[i + previous_word.len()..] as &str,
46 true,
47 )
48 .count()
49 })
50 .unwrap_or(0)
51 }
52
53 pub fn next_end_of_word(&self, index: usize) -> usize {
56 let next_string = &self.graphemes[index..].concat();
57
58 UnicodeSegmentation::split_word_bound_indices(next_string as &str)
59 .find(|(_, word)| !word.trim_start().is_empty())
60 .map(|(i, next_word)| {
61 index
62 + UnicodeSegmentation::graphemes(next_word, true).count()
63 + UnicodeSegmentation::graphemes(&next_string[..i] as &str, true).count()
64 })
65 .unwrap_or(self.len())
66 }
67
68 pub fn select(&self, start: usize, end: usize) -> Self {
71 let graphemes = self.graphemes[start.min(self.len())..end.min(self.len())].to_vec();
72
73 Self { graphemes }
74 }
75
76 pub fn until(&self, index: usize) -> Self {
79 let graphemes = self.graphemes[..index.min(self.len())].to_vec();
80
81 Self { graphemes }
82 }
83
84 pub fn insert(&mut self, index: usize, c: char) {
86 self.graphemes.insert(index, c.to_string());
87
88 self.graphemes = UnicodeSegmentation::graphemes(&self.to_string() as &str, true)
89 .map(String::from)
90 .collect();
91 }
92
93 pub fn insert_many(&mut self, index: usize, mut value: Value) {
95 let _ = self
96 .graphemes
97 .splice(index..index, value.graphemes.drain(..));
98 }
99
100 pub fn remove(&mut self, index: usize) {
102 let _ = self.graphemes.remove(index);
103 }
104
105 pub fn remove_many(&mut self, start: usize, end: usize) {
107 let _ = self.graphemes.splice(start..end, std::iter::empty());
108 }
109
110 pub fn secure(&self) -> Self {
113 Self {
114 graphemes: std::iter::repeat_n(String::from("•"), self.graphemes.len()).collect(),
115 }
116 }
117}
118
119impl std::fmt::Display for Value {
120 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
121 f.write_str(&self.graphemes.concat())
122 }
123}