From c6f2e07af04d6b7d91a44391c2f70330db2b6b6c Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Thu, 4 Aug 2022 22:32:10 +0000 Subject: [PATCH] add support for multiple directory scanning --- src/main.rs | 86 +++++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 39 deletions(-) diff --git a/src/main.rs b/src/main.rs index 792f426..902481b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::{env, path::{PathBuf, Path}, fs}; +use std::{env, path::Path, fs, collections::HashSet}; use comment_parser::CommentParser; use ignore::Walk; use regex::Regex; @@ -7,7 +7,6 @@ use regex::Regex; #[derive(Debug)] struct Reminder { - file: PathBuf, row: u32, col: u32, contents: String @@ -23,58 +22,67 @@ fn get_row_and_column(contents: &str, substr: &str, from: usize) -> (u32, u32) { (row, col) } -fn list_reminders>(path: P) -> Vec +fn list_reminders + ?Sized>(path: &P) -> Option> { let mut reminders = vec![]; let reminder_pattern: Regex = Regex::new(r"^[A-Z]+.*:").unwrap(); - for result in Walk::new(path) { - if result.is_err() { continue; } + let rules = comment_parser::get_syntax_from_path(path); + if rules.is_err() { return None; } - let entry = result.unwrap(); - let path = entry.path(); - if !path.is_file() { continue; } + let rules = rules.unwrap(); + let file_contents = fs::read_to_string(path).unwrap(); + let parser = CommentParser::new(&file_contents, rules); + let mut search_from = 0; + for comment in parser { + let text = comment.text().trim_start(); + for line in text.lines() { + if !reminder_pattern.is_match(line) { continue; } - let rules = comment_parser::get_syntax_from_path(path); - if rules.is_err() { continue; } + let is_allowed = ALLOWED_VERBS.into_iter().any(|v| line.starts_with(v)); + if !is_allowed { continue; } - let rules = rules.unwrap(); - let file_contents = fs::read_to_string(path).unwrap(); - let parser = CommentParser::new(&file_contents, rules); - let mut search_from = 0; - for comment in parser { - let text = comment.text().trim_start(); - for line in text.lines() { - if !reminder_pattern.is_match(line) { continue; } + let (row, col) = get_row_and_column(&file_contents, line, search_from); + reminders.push(Reminder { + row, + col, + contents: line.into() + }); - let is_allowed = ALLOWED_VERBS.into_iter().any(|v| line.starts_with(v)); - if !is_allowed { continue; } - - let (row, col) = get_row_and_column(&file_contents, line, search_from); - reminders.push(Reminder { - file: path.to_path_buf(), - row, - col, - contents: line.into() - }); - - search_from += file_contents[search_from..].find(line).unwrap()+1; - } + search_from += file_contents[search_from..].find(line).unwrap()+1; } } - reminders + Some(reminders) } fn main() { - let search_dir; - if let Some(path) = env::args().nth(1) { - search_dir = path; + let search_dir = if env::args().count() > 1 { + env::args().skip(1).collect() } else { - search_dir = ".".to_string(); - } + vec![".".into()] + }; - for reminder in list_reminders(search_dir) { - println!("{}:{}:{}:{}", reminder.file.display(), reminder.row, reminder.col, reminder.contents); + let mut showed_files = HashSet::new(); + for dir in search_dir { + for result in Walk::new(dir) { + if result.is_err() { continue; } + + let entry = result.unwrap(); + let path = entry.path(); + if !path.is_file() { continue; } + + let canonical = path.canonicalize().unwrap(); + if showed_files.contains(&canonical) { continue; } + + let reminders = list_reminders(path); + if let Some(reminders) = reminders { + for reminder in reminders{ + println!("{}:{}:{}:{}", path.display(), reminder.row, reminder.col, reminder.contents); + } + } + + showed_files.insert(canonical); + } } }