Organize code into separate files

This commit is contained in:
Cameron Cordes
2020-05-19 22:44:26 -04:00
parent 0daa2f4149
commit 263dad91da
6 changed files with 99 additions and 74 deletions

48
src/printer.rs Normal file
View File

@@ -0,0 +1,48 @@
extern crate term;
use super::config::Config;
use super::filematch::FileMatch;
pub trait Printer {
fn print(&self, config: &Config, file_match: &FileMatch);
}
pub struct ColorPrinter;
impl Printer for ColorPrinter {
fn print(&self, _config: &Config, mat: &FileMatch) {
let mut term = term::stdout().unwrap();
term.fg(term::color::YELLOW).unwrap();
term.attr(term::Attr::Bold).unwrap();
println!("{}:", mat.file_name);
term.reset().unwrap();
for mat in &mat.matches {
term.attr(term::Attr::Bold).unwrap();
print!("{}: ", mat.line_number);
term.reset().unwrap();
let mut last_position = 0;
for (idx, match_index) in mat.match_indexes.iter().enumerate() {
print!(
"{}",
mat.text.get(last_position..match_index.start).unwrap()
);
term.fg(term::color::BRIGHT_RED).unwrap();
print!(
"{}",
mat.text.get(match_index.start..match_index.end).unwrap()
);
term.reset().unwrap();
if idx == mat.match_indexes.len() - 1 {
println!("{}", mat.text.get(match_index.end..mat.text.len()).unwrap());
last_position = 0;
} else {
last_position = match_index.end;
}
}
}
println!();
}
}