diff --git a/src/parser.rs b/src/parser.rs index 93d0534..521f9c4 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -9,16 +9,15 @@ pub struct Parser{ impl Parser { pub fn new(keyword: String, end_filter: Box) -> bool>) -> Parser { - let callback = Box::from(|text : String, line : usize, file : &str| { + let callback = Box::from(|text: String, line: usize, file: &str| { // let path = Path::new(file).strip_prefix(env::current_dir().unwrap().to_str().unwrap()).unwrap(); println!("{} {} {} {} : {}", file, "TODO".green() ,"Line ".green(), line.to_string().green(), text.blue()); }); - Parser{keyword: keyword, end_filter: end_filter, callback} + Parser{ keyword, end_filter, callback } } pub fn new_callback(keyword: String, end_filter: Box) -> bool>, callback: Box) -> Parser { - - Parser{ keyword: keyword, end_filter : end_filter, callback } + Parser { keyword, end_filter, callback } } fn get_keyword(&self) -> String { diff --git a/src/regex.rs b/src/regex.rs index 2e598f1..7eb3cd5 100644 --- a/src/regex.rs +++ b/src/regex.rs @@ -1,5 +1,5 @@ use std::fs::File; -use std::io::{ self, BufRead }; +use std::io::{ self, BufRead, BufReader }; use std::path::Path; use regex::{RegexSet}; use crate::token::*; @@ -8,19 +8,17 @@ use crate::token::*; // Returns an Iterator to the Reader of the lines of the file. fn read_lines

(filename: P) -> io::Result>> where P: AsRef, { let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) + Ok(BufReader::new(file).lines()) } pub fn regex_parser(path : &str, regex : Vec, verbosity : i8) -> Result, io::Error> { let set = RegexSet::new(regex).unwrap(); let mut tokens = vec![]; - // let mut line_cpt = 0; - for (line_cpt, line) in (read_lines(path)?).enumerate() { // ? PLUS 1 - // for line in read_lines(path)? { - // line_cpt +=1; + for (line_cpt, line) in (read_lines(path)?).enumerate() { + let line_cpt_from1 = line_cpt + 1; let line = line.unwrap(); if set.is_match(line.to_lowercase().as_str()) { - tokens.push(Token::new(path.to_string(), line_cpt, line, verbosity)); + tokens.push(Token::new(path.to_string(), line_cpt_from1, line, verbosity)); // println!("{}", t); } } diff --git a/src/token.rs b/src/token.rs index a6041d0..4f96820 100644 --- a/src/token.rs +++ b/src/token.rs @@ -31,14 +31,14 @@ impl Token { // } let mut t = Token { - file: file, - line: line, - keyword: "todo".to_string(), + file, + line, + keyword: "todo".into(), comment: None, priority: None, date: None, member: None, - verbosity: verbosity + verbosity, }; for i in 0..fields.len() { @@ -71,23 +71,23 @@ impl Token { } pub fn inline(&self) { - let mut s; - s = string_format!("{} line: {} {} ".to_string(), self.file.clone(), self.line.to_string().green().to_string(), self.keyword.clone().green().to_string()); - if self.member.is_some() { - s = string_format!("{} Member: {}".to_string(),s ,self.member.clone().unwrap().red().to_string()); + let mut inline_msg = vec![ + format!("{} line: {} {:<6} ", self.file, self.line.to_string().green(), self.keyword.clone().green()) + ]; + if let Some(member) = &self.member { + inline_msg.push(format!("member: {}", member.red())); } - if self.priority.is_some() { - s = string_format!("{} Priority: {}".to_string(), s, self.priority.clone().unwrap().red().to_string()); + if let Some(priority) = &self.priority { + inline_msg.push(format!("priority: {}", priority.red())); } - if self.date.is_some() { - s = string_format!("{} Deadline: {}".to_string(), s, self.date.clone().unwrap().to_string().red().to_string()); + if let Some(date) = &self.date { + inline_msg.push(format!("deadline: {}", date.to_string().red())); } - if self.comment.is_some() { - s = string_format!("{} {}".to_string(), s, self.comment.clone().unwrap().blue().to_string()); + if let Some(comment) = &self.comment { + inline_msg.push(format!("{}", comment.blue())); } - println!("{}", s); + println!("{}", inline_msg.join(" ")); } - } // To use the `{}` marker, the trait `fmt::Display` must be implemented