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,20 +22,13 @@ 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) {
if result.is_err() { continue; }
let entry = result.unwrap();
let path = entry.path();
if !path.is_file() { continue; }
let rules = comment_parser::get_syntax_from_path(path); let rules = comment_parser::get_syntax_from_path(path);
if rules.is_err() { continue; } if rules.is_err() { return None; }
let rules = rules.unwrap(); let rules = rules.unwrap();
let file_contents = fs::read_to_string(path).unwrap(); let file_contents = fs::read_to_string(path).unwrap();
@ -52,7 +44,6 @@ fn list_reminders<P: AsRef<Path>>(path: P) -> Vec<Reminder>
let (row, col) = get_row_and_column(&file_contents, line, search_from); let (row, col) = get_row_and_column(&file_contents, line, search_from);
reminders.push(Reminder { reminders.push(Reminder {
file: path.to_path_buf(),
row, row,
col, col,
contents: line.into() contents: line.into()
@ -61,20 +52,37 @@ fn list_reminders<P: AsRef<Path>>(path: P) -> Vec<Reminder>
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() { 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()]
};
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);
}
} }
for reminder in list_reminders(search_dir) { showed_files.insert(canonical);
println!("{}:{}:{}:{}", reminder.file.display(), reminder.row, reminder.col, reminder.contents); }
} }
} }