Recount-TUI/src/app.rs

70 lines
1.5 KiB
Rust
Raw Normal View History

2023-05-28 22:18:30 -07:00
use std::error;
/// Application result type.
pub type AppResult<T> = std::result::Result<T, Box<dyn error::Error>>;
/// Application.
#[derive(Debug)]
pub struct App<'a> {
/// Is the application running?
pub running: bool,
/// counter
pub counter: u8,
pub tab_titles: Vec<&'a str>,
pub current_tab: usize,
}
impl<'a> Default for App<'a> {
fn default() -> Self {
Self {
running: true,
counter: 0,
tab_titles: vec!["History", "New Entry", "Accounts"],
current_tab: 0,
}
}
}
impl<'a> App<'a> {
/// Constructs a new instance of [`App`].
pub fn new() -> Self {
Self::default()
}
/// Handles the tick event of the terminal.
pub fn tick(&self) {}
/// Set running to false to quit the application.
pub fn quit(&mut self) {
self.running = false;
}
pub fn next_tab(&mut self) {
self.current_tab += 1;
if self.current_tab >= self.tab_titles.len() {
self.current_tab = 0;
}
}
pub fn prev_tab(&mut self) {
if self.current_tab == 0 {
self.current_tab = self.tab_titles.len() - 1;
} else {
self.current_tab -= 1;
}
}
pub fn increment_counter(&mut self) {
if let Some(res) = self.counter.checked_add(1) {
self.counter = res;
}
}
pub fn decrement_counter(&mut self) {
if let Some(res) = self.counter.checked_sub(1) {
self.counter = res;
}
}
}