use std::{error, collections::HashMap}; use ratatui::widgets::ListState; use wad::{Wad, load_wad_file}; use crate::lumps::{LumpInfo, get_lumps, LumpType}; /// Application result type. pub type AppResult = std::result::Result>; /// Application. pub struct StatefulList { pub state: ListState, pub items: Vec, } impl StatefulList { fn with_items(items: Vec) -> StatefulList { StatefulList { state: ListState::default(), items, } } pub fn next(&mut self) { let i = match self.state.selected() { Some(i) => { if i >= self.items.len() - 1 { 0 } else { i + 1 } } None => 0, }; self.state.select(Some(i)); } pub fn previous(&mut self) { let i = match self.state.selected() { Some(i) => { if i == 0 { self.items.len() - 1 } else { i - 1 } } None => 0, }; self.state.select(Some(i)); } pub fn unselect(&mut self) { self.state.select(None); } } pub struct App { pub wad:Wad, pub lumps:StatefulList, pub sizes: HashMap, usize>, pub running:bool, } impl App { fn from_wad(wad_path:&str) -> Self { let wad = load_wad_file(wad_path).unwrap(); let lumps = get_lumps(&wad); let mut sizes = HashMap::new(); for lump in lumps.iter() { if let Some(LumpType::Marker(_, _)) = lump.lump_type { continue; } sizes.insert(lump.lump_type.clone(), sizes.get(&lump.lump_type).unwrap_or(&0) + lump.size); } Self { wad: wad, lumps: StatefulList::with_items(lumps), sizes: sizes, running: true } } } impl App { /// Constructs a new instance of [`App`]. pub fn new(wad_path:&str) -> Self { Self::from_wad(wad_path) } /// 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; } }