diff --git a/src/application.rs b/src/application.rs index 456d873..c3e8c2a 100644 --- a/src/application.rs +++ b/src/application.rs @@ -78,13 +78,16 @@ impl Application for BookManagerApp { let mut books: Vec> = vec![]; if let Some(project) = &self.project { - for book in project.books.iter() { - books.push(text(book.file_name.clone()).into()); + for (file_name, _) in project.books.iter() { + books.push(text(file_name.clone()).into()); } } - let left_panel = scrollable(Column::with_children(books).spacing(10).padding(10)) - .width(Length::Fixed(256.0)); + let left_panel = column![ + text(books.len()), + scrollable(Column::with_children(books).spacing(10).padding(10)) + .width(Length::Fixed(256.0)) + ]; let right_panel = column![hello_world, controls, active_editor] .spacing(10) .padding(10); diff --git a/src/project.rs b/src/project.rs index 79befec..658565e 100644 --- a/src/project.rs +++ b/src/project.rs @@ -1,22 +1,24 @@ -use std::{ffi::OsStr, path::PathBuf}; +use std::{collections::HashMap, ffi::OsStr, path::PathBuf}; use walkdir::WalkDir; #[derive(Debug, Clone)] pub struct Book { pub path: PathBuf, pub file_name: String, + pub languages: Vec, } #[derive(Debug, Clone)] pub struct ProjectState { pub path: PathBuf, - pub books: Vec, + pub books: HashMap, } impl ProjectState { pub fn new(path: PathBuf) -> Self { - let mut books = vec![]; - let book_walker = WalkDir::new(path.join("books")).into_iter(); + let search_path = path.join("books"); + let mut books = HashMap::new(); + let book_walker = WalkDir::new(search_path.clone()).into_iter(); for entry in book_walker.flatten() { if entry.metadata().is_ok_and(|md| md.is_dir()) { continue; @@ -24,10 +26,34 @@ impl ProjectState { let extension = entry.path().extension().and_then(OsStr::to_str); if extension.is_some_and(|ext| ext.to_lowercase() == "str") { - books.push(Book { - path: entry.path().to_owned(), - file_name: entry.file_name().to_str().unwrap().to_owned(), - }); + let language = entry + .path() + .parent() + .unwrap() + .strip_prefix(search_path.clone()) + .unwrap() + .to_str() + .unwrap() + .to_owned() + .to_lowercase(); + let file_name = entry + .file_name() + .to_str() + .unwrap() + .to_owned() + .to_lowercase(); + books + .entry(file_name.clone()) + .and_modify(|b: &mut Book| b.languages.push(language.clone())) + .or_insert(Book { + path: entry.path().to_owned(), + file_name, + languages: vec![language], + }); + // books.push(Book { + // path: entry.path().to_owned(), + // file_name: entry.file_name().to_str().unwrap().to_owned(), + // }); } }