Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
terminal: Improve tui property store
Browse files Browse the repository at this point in the history
  • Loading branch information
erak committed Jun 23, 2022
1 parent fd08541 commit 1a9b149
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions terminal/src/tui/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,125 @@ impl Store {
None => Err(StoreError::NotFound(key.to_owned())),
}
}

/// Return a mutable value for given property or an error if property does
/// not exist or can't be casted to given type.
pub fn get_mut<T: Any>(&mut self, key: &str) -> Result<&mut T, StoreError> {
match self.properties.get_mut(key) {
Some(prop) => match prop.downcast_mut::<T>() {
Some(value) => Ok(value),
None => Err(StoreError::InvalidType(key.to_owned())),
},
None => Err(StoreError::NotFound(key.to_owned())),
}
}
}

/// List of items that keeps track of the selection.
pub struct Items<T> {
values: Vec<T>,
index: usize,
}

impl<T> Items<T> {
pub fn all(&self) -> &Vec<T> {
&self.values
}

pub fn selected(&self) -> Option<&T> {
self.values.get(self.index)
}

pub fn select_index(&mut self, index: usize) {
self.index = index
}

pub fn selected_index(&self) -> usize {
self.index
}

pub fn is_empty(&self) -> bool {
self.values.is_empty()
}

pub fn count(&self) -> usize {
self.values.len()
}
}

/// List of items that cycles through the selection.
pub struct TabProperty<T> {
items: Items<T>,
}

impl<T> TabProperty<T> {
pub fn new(items: Vec<T>) -> Self {
Self {
items: Items {
values: items,
index: 0,
},
}
}

pub fn items(&self) -> &Items<T> {
&self.items
}

pub fn select_previous(&mut self) {
let len = self.items.all().len();
let index = match self.items.selected_index() == 0 {
true => len - 1,
false => self.items.selected_index() - 1,
};
self.items.select_index(index);
}

pub fn select_next(&mut self) {
let len = self.items.all().len();
let index = match self.items.selected_index() >= len - 1 {
true => 0,
false => self.items.selected_index() + 1,
};
self.items.select_index(index);
}
}

/// List of items that won't cycle through the selection,
/// but rather stops selecting a new item at the beginning
/// and the end of the list.
pub struct ListProperty<T> {
items: Items<T>,
}

impl<T> ListProperty<T> {
pub fn new(items: Vec<T>) -> Self {
Self {
items: Items {
values: items,
index: 0,
},
}
}

pub fn items(&self) -> &Items<T> {
&self.items
}

pub fn select_previous(&mut self) {
let index = match self.items.selected_index() == 0 {
true => 0,
false => self.items.selected_index() - 1,
};
self.items.select_index(index);
}

pub fn select_next(&mut self) {
let len = self.items.all().len();
let index = match self.items.selected_index() >= len - 1 {
true => len - 1,
false => self.items.selected_index() + 1,
};
self.items.select_index(index);
}
}

0 comments on commit 1a9b149

Please sign in to comment.