diff --git a/Cargo.toml b/Cargo.toml index ff31a64..8cf40e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2018" [dependencies] colored = "2.0.0" -gitignore = "1.0.7" \ No newline at end of file +gitignore = "1.0.7" +path-absolutize = "3.0.10" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index af431b1..80558fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use std::fs; use std::env; use colored::Colorize; use gitignore; +use path_absolutize::*; fn main() { let args: Vec = env::args().collect(); @@ -32,22 +33,24 @@ fn main() { if gi_exists { let gi_str = args[1].to_owned() + "/.gitignore"; - let gi_path = std::path::Path::new(&gi_str); + let _p = std::path::Path::new(&gi_str).absolutize().unwrap(); + let gi_str_abs = _p.to_str().unwrap(); + let gi_path = std::path::Path::new(&gi_str_abs); let gi= gitignore::File::new(gi_path).unwrap(); - traverse_dir(&args[1], &mut todos, Some(&gi)).unwrap(); + iterate_included_files(&mut todos, &gi).unwrap(); } else { - traverse_dir(&args[1], &mut todos, None).unwrap(); + traverse_dir(&args[1], &mut todos).unwrap(); } // TODO eventually add option to export to file - println!("{:4} {:05} {:15} {:50}", "", "Line".bold(), "File".bold(), "Comment".bold()); + println!("{:4} {:05} {:20} {:50}", "", "LINE".bold(), "FILE".bold(), "COMMENT".bold()); for todo in todos { - println!("{:4} {:05} {:15} {:50}", "TODO", todo.line_number.to_string().yellow(), todo.file_name.green(), todo.comment); + println!("{:4} {:05} {:20} {:50}", "TODO".bold(), todo.line_number.to_string().yellow(), todo.file_name.green(), todo.comment); } } -fn traverse_dir(path: &str, todos: &mut Vec, gi_option: Option<&gitignore::File>) -> Result<(), std::io::Error> { +fn traverse_dir(path: &str, todos: &mut Vec) -> Result<(), std::io::Error> { let objects = fs::read_dir(path.to_owned())?; for result in objects { @@ -55,18 +58,10 @@ fn traverse_dir(path: &str, todos: &mut Vec, gi_option: Option<&gitignore: let obj_str = obj_path.to_str().unwrap(); let obj_metadata = fs::metadata(obj_str)?; - let is_excluded = match gi_option { - Some(gi) => { - let dir_path = std::path::Path::new(obj_str); - gi.is_excluded(dir_path).unwrap() - } - None => { false } - }; - - if !is_excluded && obj_metadata.is_dir() { - traverse_dir(obj_str, todos, gi_option)?; + if obj_metadata.is_dir() { + traverse_dir(obj_str, todos)?; } - else if !is_excluded && obj_metadata.is_file() && obj_str.ends_with(".py") { + else if obj_metadata.is_file() && obj_str.ends_with(".py") { // TODO support other files besides .py get_todos(obj_str, todos)?; } } @@ -74,6 +69,17 @@ fn traverse_dir(path: &str, todos: &mut Vec, gi_option: Option<&gitignore: Ok(()) } +fn iterate_included_files(todos: &mut Vec, gi: &gitignore::File) -> Result<(), std::io::Error> { + for file_path in gi.included_files().unwrap() { + let file_str = file_path.to_str().unwrap(); + if file_str.ends_with(".py") { // TODO support other files besides .py + get_todos(file_str, todos)?; + } + } + + Ok(()) +} + fn get_todos(path: &str, todos: &mut Vec) -> Result<(), std::io::Error> { let contents = fs::read_to_string(path)?; let mut line_number = 0;