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 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<P: AsRef<Path>>(path: P) -> Vec<Reminder>
fn list_reminders<P: AsRef<Path> + ?Sized>(path: &P) -> Option<Vec<Reminder>>
{
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);
}
}
}