promkit_widgets/listbox/
listbox.rs1use std::fmt;
2
3use promkit_core::grapheme::StyledGraphemes;
4
5use crate::cursor::Cursor;
6
7#[derive(Clone)]
14pub struct Listbox(Cursor<Vec<StyledGraphemes>>);
15
16impl Default for Listbox {
17 fn default() -> Self {
18 Self(Cursor::new(vec![StyledGraphemes::default()], 0, false))
19 }
20}
21
22impl<E: fmt::Display, I: IntoIterator<Item = E>> From<I> for Listbox {
23 fn from(items: I) -> Self {
24 Self(Cursor::new(
25 items
26 .into_iter()
27 .map(|e| StyledGraphemes::from(format!("{}", e)))
28 .collect(),
29 0,
30 false,
31 ))
32 }
33}
34
35impl Listbox {
36 pub fn len(&self) -> usize {
37 self.0.contents().len()
38 }
39
40 pub fn is_empty(&self) -> bool {
41 self.0.contents().is_empty()
42 }
43
44 pub fn push_string(&mut self, item: String) {
45 self.0.contents_mut().push(StyledGraphemes::from(item));
46 }
47
48 pub fn from_styled_graphemes(items: Vec<StyledGraphemes>) -> Self {
50 Self(Cursor::new(items, 0, false))
51 }
52
53 pub fn items(&self) -> &Vec<StyledGraphemes> {
55 self.0.contents()
56 }
57
58 pub fn position(&self) -> usize {
60 self.0.position()
61 }
62
63 pub fn get(&self) -> StyledGraphemes {
67 self.items()
68 .get(self.position())
69 .unwrap_or(&StyledGraphemes::default())
70 .clone()
71 }
72
73 pub fn backward(&mut self) -> bool {
76 self.0.backward()
77 }
78
79 pub fn forward(&mut self) -> bool {
82 self.0.forward()
83 }
84
85 pub fn move_to_head(&mut self) {
87 self.0.move_to_head()
88 }
89
90 pub fn move_to_tail(&mut self) {
92 self.0.move_to_tail()
93 }
94
95 pub fn is_tail(&self) -> bool {
96 self.0.is_tail()
97 }
98}