feat: add formatted output with line and file

This commit is contained in:
2021-07-09 23:39:42 -04:00
parent bd30081971
commit d908057134

View File

@@ -22,7 +22,7 @@ fn main() {
return
}
// TODO: find safer way to check for gitignore
// TODO find safer way to check for gitignore (windows compatible)
let gi_exists = fs::metadata(args[1].to_owned() + "/.gitignore").is_ok();
if !gi_exists {
println!("{}", "WRN: .gitignore not found, may output TODOs in dependencies".yellow())
@@ -40,19 +40,21 @@ fn main() {
traverse_dir(&args[1], &mut todos, None).unwrap();
}
// TODO eventually add option to export to file
println!("{:4} {:05} {:15} {:50}", "", "Line".bold(), "File".bold(), "Comment".bold());
for todo in todos {
println!("TODO: {}", todo.green());
println!("{:4} {:05} {:15} {:50}", "TODO", todo.line_number.to_string().yellow(), todo.file_name.green(), todo.comment);
}
}
fn traverse_dir(path: &str, todos: &mut Vec<String>, gi_option: Option<&gitignore::File>) -> Result<(), std::io::Error> {
fn traverse_dir(path: &str, todos: &mut Vec<Todo>, gi_option: Option<&gitignore::File>) -> Result<(), std::io::Error> {
let objects = fs::read_dir(path.to_owned())?;
for result in objects {
let obj_path = result?.path();
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);
@@ -72,16 +74,35 @@ fn traverse_dir(path: &str, todos: &mut Vec<String>, gi_option: Option<&gitignor
Ok(())
}
fn get_todos(path: &str, todos: &mut Vec<String>) -> Result<(), std::io::Error> {
fn get_todos(path: &str, todos: &mut Vec<Todo>) -> Result<(), std::io::Error> {
let contents = fs::read_to_string(path)?;
let mut line_number = 0;
let file_name = path.split("/").last().unwrap();
for line in contents.lines() {
line_number += 1;
if line.contains("TODO") {
let (_, comment) = line.split_once("TODO").unwrap();
let cleaned_comment = comment.replace(":", "").trim().to_owned();
todos.push(cleaned_comment)
todos.push(Todo::new(cleaned_comment, file_name.to_owned(), line_number))
}
}
Ok(())
}
struct Todo {
comment: String,
file_name: String,
line_number: u32
}
impl Todo {
fn new(comment: String, file_name: String, line_number: u32) -> Todo {
Todo {
comment: comment,
file_name: file_name,
line_number: line_number
}
}
}