Organize code into separate files
This commit is contained in:
48
src/printer.rs
Normal file
48
src/printer.rs
Normal 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!();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user