diff --git a/src/token.rs b/src/token.rs index 4f96820..7ca7383 100644 --- a/src/token.rs +++ b/src/token.rs @@ -1,5 +1,4 @@ use chrono::NaiveDate; -use string_format::string_format; use std::fmt; use colored::Colorize; use regex::Regex; @@ -19,7 +18,7 @@ pub struct Token{ impl Token { pub fn new(file: String, line: usize, s: String, verbosity: i8) -> Token { // println!("{}", s); - let fields : Vec<&str>= s.split_whitespace().collect(); + let fields: Vec<&str>= s.split_whitespace().collect(); let number_regex = Regex::new("\\b[1-9]\\b").unwrap(); let date_regex = Regex::new("(\\d*/\\d*/\\d*)").unwrap(); let member_regex = Regex::new("!\\w*").unwrap(); @@ -59,11 +58,10 @@ impl Token { t.member = Some(member); } else { - if t.comment.is_none() { - t.comment = Some(fields[i].to_string()); - } else { - t.comment = Some(string_format!("{} {}".to_string(),t.comment.unwrap(), fields[i].to_string())); - } + t.comment = match t.comment { + None => Some(fields[i].to_string()), + Some(comment) => Some(format!("{} {}", comment, fields[i])), + }; } } @@ -93,32 +91,34 @@ impl Token { // To use the `{}` marker, the trait `fmt::Display` must be implemented // manually for the type. impl fmt::Display for Token { - // This trait requires `fmt` with this exact signature. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let mut s; + let mut multiline_msg = vec![ + format!("{} line: {} {}", self.file, self.line.to_string().green(), self.keyword.green()) + ]; - s = string_format!("{} line: {} {} \n".to_string(), self.file.clone(), self.line.to_string().green().to_string(), self.keyword.clone().green().to_string()); - if self.verbosity <= 1{ - if self.comment.is_some() { - s = string_format!("{}{}\n".to_string(), s, self.comment.clone().unwrap().blue().to_string()); + if self.verbosity <= 1 { + if let Some(comment) = &self.comment { + multiline_msg.push(format!("{}", comment.blue())); } - } - else { - if self.member.is_some() { - s = string_format!("{}Member: {}\n".to_string(),s ,self.member.clone().unwrap().red().to_string()); + } else { + if let Some(member) = &self.member { + multiline_msg.push(format!("- member : {}", member.red())); } - if self.priority.is_some() { - s = string_format!("{}Priority: {}\n".to_string(), s, self.priority.clone().unwrap().red().to_string()); + if let Some(priority) = &self.priority { + multiline_msg.push(format!("- priority: {}", priority.red())); } - if self.date.is_some() { - s = string_format!("{}Deadline: {}\n".to_string(), s, self.date.clone().unwrap().to_string().red().to_string()); + if let Some(date) = &self.date { + multiline_msg.push(format!("- deadline: {}", date.to_string().red())); } - if self.comment.is_some() { - s = string_format!("{}{}\n".to_string(), s, self.comment.clone().unwrap().blue().to_string()); + if let Some(comment) = &self.comment { + multiline_msg.push(format!("{}", comment.blue())); } + // if self.comment.is_some() { + // s = string_format!("{}{}\n".to_string(), s, self.comment.clone().unwrap().blue().to_string()); + // } } - write!(f, "{}", s)?; + write!(f, "{}\n", multiline_msg.join("\n"))?; Ok(()) } }