From ef7c7c7f6eaa0fb7b14dbe344ab749a1c86896cd Mon Sep 17 00:00:00 2001 From: Emile Date: Thu, 3 Feb 2022 21:53:51 +0100 Subject: don't display files part of the .gitignore --- src/main.rs | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9385e01..56a3f7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -501,13 +501,29 @@ fn recursive_read_dir(dir: &PathBuf, dir_only: bool) -> io::Result> return Ok(vec![]); } + // get all entries in the gitignore file, if it exists + let gitignore_entries: Vec = gitignore_entries(&dir)?; + // store the child pathes let mut entries: Vec = Vec::new(); // iterate over all items in the dir, pushing the dirs pathes to the dirs - // vector for returnin it - for entry in fs::read_dir(dir)? { - let path = &entry?.path(); + // vector for returning it + 'outer: for entry in fs::read_dir(dir)? { + let dir_entry = &entry?; + let path = dir_entry.path(); + + // check if the current entry is part of the gitignore, if so, skip it + for gitignore_entry in &gitignore_entries { + if gitignore_entry.to_str() == Some("") { + continue; + } + if path.ends_with(gitignore_entry) { + println!("gitignore: gitignore_entry: {:?}", gitignore_entry); + println!("gitignore: path: {:?}", path); + continue 'outer; + } + } if dir_only == true { if path.is_dir() { @@ -526,5 +542,22 @@ fn recursive_read_dir(dir: &PathBuf, dir_only: bool) -> io::Result> } // return the dirs, the ones from this folder and the ones from all child folders + Ok(entries) +} + +// try to open the gitignore file and read all entries from there. +fn gitignore_entries(dir: &PathBuf) -> io::Result> { + let gitignore_path = Path::new(&dir) + .join(Path::new(".gitignore")); + + let mut entries: Vec = Vec::new(); + if let Ok(gitignore) = File::open(&gitignore_path) { + let reader = BufReader::new(gitignore); + + for line in reader.lines() { + entries.push(PathBuf::from(line?)); + } + } + Ok(entries) } \ No newline at end of file -- cgit 1.4.1