Skip to content

Commit

Permalink
feat: super cringe and hacky project file sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpq committed Oct 6, 2023
1 parent fbbb51b commit bd2c93c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,16 @@ async fn main() -> std::io::Result<()> {
.watch("./projects/", RecursiveMode::Recursive)
.unwrap();

let paths = fs::read_dir(format!("{}/projects/", CONFIG.path_root)).unwrap();
let mut paths: Vec<std::path::PathBuf> = fs::read_dir(format!("{}/projects/", CONFIG.path_root)).unwrap().filter_map(|p| p.ok()).map(|p| p.path()).collect();
paths.sort_by_cached_key(|path| path.file_name()
.and_then(|s| s.to_str())
.and_then(|s| s.split('_').next())
.and_then(|s| s.parse::<u32>().ok())
.unwrap_or(50)
);
for path in paths {
projects.process(path.unwrap().path());
debug!("found project {}", &path.display());
projects.process(path);
}

let projects_ref = Arc::new(Mutex::new(projects));
Expand Down
29 changes: 16 additions & 13 deletions src/models/projects.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use log::error;
use log::{error, debug};
use notify::{watcher, DebouncedEvent, RecommendedWatcher};
use yaml_rust::YamlLoader;
use std::sync::mpsc::{channel, Receiver};
Expand Down Expand Up @@ -103,18 +103,21 @@ impl Projects {
}

pub fn value(&self) -> Arc<Mutex<HashMap<String, Project>>> {
self.rx.try_iter().for_each(|e: DebouncedEvent| match e {
DebouncedEvent::NoticeWrite(p) => self.process(p),
DebouncedEvent::NoticeRemove(p) => self.process(p),
DebouncedEvent::Create(p) => self.process(p),
DebouncedEvent::Write(p) => self.process(p),
DebouncedEvent::Chmod(p) => self.process(p),
DebouncedEvent::Remove(p) => {
self.value.lock().unwrap().remove(&String::from(p.file_name().unwrap().to_str().unwrap()));
},
DebouncedEvent::Rename(_, p) => self.process(p),
_ => {}
});
self.rx.try_iter().for_each(|e: DebouncedEvent| {
debug!("fs watch triggered!");
match e {
DebouncedEvent::NoticeWrite(p) => self.process(p),
DebouncedEvent::NoticeRemove(p) => self.process(p),
DebouncedEvent::Create(p) => self.process(p),
DebouncedEvent::Write(p) => self.process(p),
DebouncedEvent::Chmod(p) => self.process(p),
DebouncedEvent::Remove(p) => {
self.value.lock().unwrap().remove(&String::from(p.file_name().unwrap().to_str().unwrap()));
},
DebouncedEvent::Rename(_, p) => self.process(p),
_ => {}
}
});
self.value.clone()
}

Expand Down

0 comments on commit bd2c93c

Please sign in to comment.