| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 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<T> = std::result::Result<T, Box<dyn error::Error>>;
- /// Application.
- pub struct StatefulList<T> {
- pub state: ListState,
- pub items: Vec<T>,
- }
- impl<T> StatefulList<T> {
- fn with_items(items: Vec<T>) -> StatefulList<T> {
- 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<LumpInfo>,
- pub sizes: HashMap<Option<LumpType>, 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;
- }
- }
|