use std::fmt;
use promkit_core::grapheme::StyledGraphemes;
use crate::cursor::Cursor;
#[derive(Clone)]
pub struct Listbox(Cursor<Vec<StyledGraphemes>>);
impl Default for Listbox {
fn default() -> Self {
Self(Cursor::new(Vec::new(), 0, false))
}
}
impl<E: fmt::Display, I: IntoIterator<Item = E>> From<I> for Listbox {
fn from(items: I) -> Self {
Self(Cursor::new(
items
.into_iter()
.map(|e| StyledGraphemes::from(format!("{}", e)))
.collect(),
0,
false,
))
}
}
impl Listbox {
pub fn len(&self) -> usize {
self.0.contents().len()
}
pub fn is_empty(&self) -> bool {
self.0.contents().is_empty()
}
pub fn push_string(&mut self, item: String) {
self.0.contents_mut().push(StyledGraphemes::from(item));
}
pub fn from_styled_graphemes(items: Vec<StyledGraphemes>) -> Self {
Self(Cursor::new(items, 0, false))
}
pub fn items(&self) -> &Vec<StyledGraphemes> {
self.0.contents()
}
pub fn position(&self) -> usize {
self.0.position()
}
pub fn get(&self) -> StyledGraphemes {
self.items()
.get(self.position())
.unwrap_or(&StyledGraphemes::default())
.clone()
}
pub fn backward(&mut self) -> bool {
self.0.backward()
}
pub fn forward(&mut self) -> bool {
self.0.forward()
}
pub fn move_to_head(&mut self) {
self.0.move_to_head()
}
pub fn move_to_tail(&mut self) {
self.0.move_to_tail()
}
pub fn is_tail(&self) -> bool {
self.0.is_tail()
}
}
#[cfg(test)]
mod tests {
use super::Listbox;
#[test]
fn default_is_empty() {
let listbox = Listbox::default();
assert!(listbox.is_empty());
assert_eq!(listbox.len(), 0);
}
}