feat: replace loading enum with FRAMES const

This commit is contained in:
Jack Wills
2023-06-14 20:47:40 +00:00
parent 3b61a704e2
commit e17aad06cd
+19 -63
View File
@@ -1,8 +1,5 @@
use ratatui::layout::{Constraint, Rect}; use ratatui::layout::{Constraint, Rect};
use std::{ use std::collections::{HashMap, HashSet};
collections::{HashMap, HashSet},
fmt,
};
use uuid::Uuid; use uuid::Uuid;
use crate::app_data::{ContainerId, Header}; use crate::app_data::{ContainerId, Header};
@@ -145,60 +142,12 @@ impl BoxLocation {
} }
} }
/// State for the loading animation // loading animation frames
#[derive(Debug, Default, Clone, Copy)] const FRAMES: [char; 10] = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
pub enum Loading { const FRAMES_LEN: u8 = 9;
#[default]
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
}
impl Loading {
pub const fn next(self) -> Self {
match self {
Self::One => Self::Two,
Self::Two => Self::Three,
Self::Three => Self::Four,
Self::Four => Self::Five,
Self::Five => Self::Six,
Self::Six => Self::Seven,
Self::Seven => Self::Eight,
Self::Eight => Self::Nine,
Self::Nine => Self::Ten,
Self::Ten => Self::One,
}
}
}
impl fmt::Display for Loading {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let disp = match self {
Self::One => '⠋',
Self::Two => '⠙',
Self::Three => '⠹',
Self::Four => '⠸',
Self::Five => '⠼',
Self::Six => '⠴',
Self::Seven => '⠦',
Self::Eight => '⠧',
Self::Nine => '⠇',
Self::Ten => '⠏',
};
write!(f, "{disp}")
}
}
/// The application gui state can be in multiple of these four states at the same time /// The application gui state can be in multiple of these four states at the same time
/// Various functions (e.g input handler), operate differently depending upon current Status /// Various functions (e.g input handler), operate differently depending upon current Status
// Copy
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum Status { pub enum Status {
Init, Init,
@@ -213,7 +162,7 @@ pub enum Status {
pub struct GuiState { pub struct GuiState {
heading_map: HashMap<Header, Rect>, heading_map: HashMap<Header, Rect>,
is_loading: HashSet<Uuid>, is_loading: HashSet<Uuid>,
loading_icon: Loading, loading_index: u8,
panel_map: HashMap<SelectablePanel, Rect>, panel_map: HashMap<SelectablePanel, Rect>,
delete_map: HashMap<DeleteButton, Rect>, delete_map: HashMap<DeleteButton, Rect>,
status: HashSet<Status>, status: HashSet<Status>,
@@ -327,24 +276,31 @@ impl GuiState {
self.selected_panel = self.selected_panel.prev(); self.selected_panel = self.selected_panel.prev();
} }
/// Insert a new loading_uuid into HashSet, and advance the animation by one frame /// Insert a new loading_uuid into HashSet, and advance the loading_index by one frame, or reset to 0 if at end of array
pub fn next_loading(&mut self, uuid: Uuid) { pub fn next_loading(&mut self, uuid: Uuid) {
self.loading_icon = self.loading_icon.next(); if self.loading_index == FRAMES_LEN {
self.loading_index = 0;
} else {
self.loading_index += 1;
}
self.is_loading.insert(uuid); self.is_loading.insert(uuid);
} }
/// If is_loading has any entries, return the current loading_icon, else an empty string, which needs to take up the same space, hence ' ' /// If is_loading has any entries, return the char at FRAMES[index], else an empty char, which needs to take up the same space, hence ' '
pub fn get_loading(&mut self) -> String { pub fn get_loading(&mut self) -> char {
if self.is_loading.is_empty() { if self.is_loading.is_empty() {
String::from(" ") ' '
} else { } else {
self.loading_icon.to_string() FRAMES[usize::from(self.loading_index)]
} }
} }
/// Remove a loading_uuid from the is_loading HashSet /// Remove a loading_uuid from the is_loading HashSet, if empty, reset loading_index to 0
pub fn remove_loading(&mut self, uuid: Uuid) { pub fn remove_loading(&mut self, uuid: Uuid) {
self.is_loading.remove(&uuid); self.is_loading.remove(&uuid);
if self.is_loading.is_empty() {
self.loading_index = 0;
}
} }
/// Set info box content /// Set info box content