1
0

add support for multiple directory scanning

This commit is contained in:
Rokas Puzonas 2022-08-04 22:32:10 +00:00
parent bacb7a94a4
commit c6f2e07af0

View File

@ -1,4 +1,4 @@
use std::{env, path::{PathBuf, Path}, fs}; use std::{env, path::Path, fs, collections::HashSet};
use comment_parser::CommentParser; use comment_parser::CommentParser;
use ignore::Walk; use ignore::Walk;
use regex::Regex; use regex::Regex;
@ -7,7 +7,6 @@ use regex::Regex;
#[derive(Debug)] #[derive(Debug)]
struct Reminder { struct Reminder {
file: PathBuf,
row: u32, row: u32,
col: u32, col: u32,
contents: String contents: String
@ -23,58 +22,67 @@ fn get_row_and_column(contents: &str, substr: &str, from: usize) -> (u32, u32) {
(row, col) (row, col)
} }
fn list_reminders<P: AsRef<Path>>(path: P) -> Vec<Reminder> fn list_reminders<P: AsRef<Path> + ?Sized>(path: &P) -> Option<Vec<Reminder>>
{ {
let mut reminders = vec![]; let mut reminders = vec![];
let reminder_pattern: Regex = Regex::new(r"^[A-Z]+.*:").unwrap(); let reminder_pattern: Regex = Regex::new(r"^[A-Z]+.*:").unwrap();
for result in Walk::new(path) { let rules = comment_parser::get_syntax_from_path(path);
if result.is_err() { continue; } if rules.is_err() { return None; }
let entry = result.unwrap(); let rules = rules.unwrap();
let path = entry.path(); let file_contents = fs::read_to_string(path).unwrap();
if !path.is_file() { continue; } 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); let is_allowed = ALLOWED_VERBS.into_iter().any(|v| line.starts_with(v));
if rules.is_err() { continue; } if !is_allowed { continue; }
let rules = rules.unwrap(); let (row, col) = get_row_and_column(&file_contents, line, search_from);
let file_contents = fs::read_to_string(path).unwrap(); reminders.push(Reminder {
let parser = CommentParser::new(&file_contents, rules); row,
let mut search_from = 0; col,
for comment in parser { contents: line.into()
let text = comment.text().trim_start(); });
for line in text.lines() {
if !reminder_pattern.is_match(line) { continue; }
let is_allowed = ALLOWED_VERBS.into_iter().any(|v| line.starts_with(v)); search_from += file_contents[search_from..].find(line).unwrap()+1;
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;
}
} }
} }
reminders Some(reminders)
} }
fn main() { fn main() {
let search_dir; let search_dir = if env::args().count() > 1 {
if let Some(path) = env::args().nth(1) { env::args().skip(1).collect()
search_dir = path;
} else { } else {
search_dir = ".".to_string(); vec![".".into()]
} };
for reminder in list_reminders(search_dir) { let mut showed_files = HashSet::new();
println!("{}:{}:{}:{}", reminder.file.display(), reminder.row, reminder.col, reminder.contents); 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);
}
} }
} }