1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//! Suggestions: setting them, and those that match the value.
use super::Model;
impl Model {
/// Sets the suggestions, matched by prefix as the text is typed.
///
/// ```
/// use petitcarnets as tea;
/// use tea::bubbles::textinput;
///
/// let mut input = textinput::Model::new();
/// input.show_suggestions = true;
/// input.set_suggestions(&["apple", "apricot", "banana"]);
/// input.set_value("ap");
/// assert_eq!(input.matched_suggestions(), ["apple", "apricot"]);
/// ```
pub fn set_suggestions(&mut self, suggestions: &[&str]) {
self.suggestions = suggestions.iter().map(|s| s.chars().collect()).collect();
self.update_suggestions();
}
/// Every suggestion.
///
/// ```
/// use petitcarnets as tea;
/// use tea::bubbles::textinput;
///
/// let mut input = textinput::Model::new();
/// input.set_suggestions(&["apple", "banana"]);
/// input.set_value("ap");
/// assert_eq!(input.available_suggestions(), ["apple", "banana"]);
/// ```
#[must_use]
pub fn available_suggestions(&self) -> Vec<String> {
self.suggestions
.iter()
.map(|s| s.iter().collect())
.collect()
}
/// The suggestions that match the text.
///
/// ```
/// use petitcarnets as tea;
/// use tea::bubbles::textinput;
///
/// let mut input = textinput::Model::new();
/// input.show_suggestions = true;
/// input.set_suggestions(&["apple", "banana"]);
/// input.set_value("b");
/// assert_eq!(input.matched_suggestions(), ["banana"]);
/// ```
#[must_use]
pub fn matched_suggestions(&self) -> Vec<String> {
self.matched.iter().map(|s| s.iter().collect()).collect()
}
/// Which matched suggestion is offered.
///
/// ```
/// use petitcarnets as tea;
/// use tea::bubbles::textinput;
///
/// let mut input = textinput::Model::new();
/// input.show_suggestions = true;
/// input.set_suggestions(&["apple", "apricot"]);
/// let _ = input.focus();
/// input.set_value("ap");
/// assert_eq!(input.current_suggestion_index(), 0);
/// let _ = input.update(&tea::Msg::KeyPress(tea::Key::new(tea::KeyCode::Down)));
/// assert_eq!(input.current_suggestion_index(), 1);
/// ```
#[must_use]
pub const fn current_suggestion_index(&self) -> usize {
self.current_suggestion
}
/// The matched suggestion offered, or nothing.
///
/// ```
/// use petitcarnets as tea;
/// use tea::bubbles::textinput;
///
/// let mut input = textinput::Model::new();
/// input.show_suggestions = true;
/// input.set_suggestions(&["apple", "apricot"]);
/// let _ = input.focus();
/// input.set_value("apr");
/// assert_eq!(input.current_suggestion(), "apricot");
/// let _ = input.update(&tea::Msg::KeyPress(tea::Key::new(tea::KeyCode::Tab)));
/// assert_eq!(input.value(), "apricot", "Tab takes it");
/// ```
#[must_use]
pub fn current_suggestion(&self) -> String {
self.matched
.get(self.current_suggestion)
.map(|s| s.iter().collect())
.unwrap_or_default()
}
}