diff --git a/Cargo.lock b/Cargo.lock index 1fe17ff..06485dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,9 +69,9 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] name = "clap" -version = "2.33.0" +version = "2.33.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" +checksum = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129" dependencies = [ "ansi_term", "atty", @@ -134,9 +134,9 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.1.12" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61565ff7aaace3525556587bd2dc31d4a07071957be715e63ce7b1eccf51a8f4" +checksum = "91780f809e750b0a89f5544be56617ff6b1227ee485bcb06ebe10cdf89bd3b71" dependencies = [ "libc", ] @@ -149,9 +149,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99e85c08494b21a9054e7fe1374a732aeadaff3980b6990b94bfd3a70f690005" +checksum = "3baa92041a6fec78c687fa0cc2b3fae8884f743d672cf551bed1d6dac6988d0f" [[package]] name = "rack" @@ -223,9 +223,9 @@ checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" [[package]] name = "vec_map" -version = "0.8.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" [[package]] name = "wasi" diff --git a/Cargo.toml b/Cargo.toml index 56f8140..e70c672 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,6 @@ edition = "2018" [dependencies] clap = "2.33.0" term = "0.6.1" + +[profile.release] +lto = true diff --git a/src/filematch.rs b/src/filematch.rs new file mode 100644 index 0000000..256825b --- /dev/null +++ b/src/filematch.rs @@ -0,0 +1,28 @@ +use super::config::Config; +use super::printer::*; + +#[derive(Debug)] +pub struct FileMatch { + pub file_name: String, + pub matches: Vec, +} + +impl FileMatch { + pub fn print(&self, config: &Config) { + let cp = ColorPrinter {}; + cp.print(&config, &self); + } +} + +#[derive(Debug, Clone)] +pub struct Match { + pub line_number: usize, + pub match_indexes: Vec, + pub text: String, +} + +#[derive(Debug, Clone)] +pub struct MatchIndex { + pub start: usize, + pub end: usize, +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..4cf07ba --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,3 @@ +pub mod config; +pub mod filematch; +pub mod printer; diff --git a/src/main.rs b/src/main.rs index b2925a4..7d87308 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use rack::config::Config; +use rack::filematch::*; use std::fs::{self, File}; use std::io; use std::io::prelude::*; @@ -5,10 +7,8 @@ use std::path::Path; extern crate term; -mod config; - fn main() { - let config = config::Config::from_args(); + let config = Config::from_args(); let search_path = &config.path; let path = match search_path.starts_with('/') { @@ -22,15 +22,15 @@ fn main() { let results = traverse_dir(&config, path, |config, path| check_file(config, path)); match results { Result::Ok(matches) if matches.len() == 0 => println!("No matches :("), - Result::Ok(matches) => print_report(&matches), + Result::Ok(matches) => print_report(&config, &matches), Result::Err(msg) => println!("Error while parsing: {}", msg), } } fn traverse_dir( - config: &config::Config, + config: &Config, dir: &Path, - operation: fn(&config::Config, &Path) -> io::Result, + operation: fn(&Config, &Path) -> io::Result, ) -> io::Result> { let mut matches: Vec = Vec::new(); for entry in fs::read_dir(dir)? { @@ -60,64 +60,7 @@ fn traverse_dir( Ok(matches) } -#[derive(Debug)] -struct FileMatch { - file_name: String, - matches: Vec, -} - -impl FileMatch { - fn print(&self) { - let mut term = term::stdout().unwrap(); - term.fg(term::color::YELLOW).unwrap(); - term.attr(term::Attr::Bold).unwrap(); - println!("{}:", self.file_name); - term.reset().unwrap(); - - for mat in &self.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!(); - } -} - -#[derive(Debug, Clone)] -struct Match { - line_number: usize, - match_indexes: Vec, - text: String, -} - -#[derive(Debug, Clone)] -struct MatchIndex { - start: usize, - end: usize, -} - -fn check_file(config: &config::Config, path: &Path) -> io::Result { +fn check_file(config: &Config, path: &Path) -> io::Result { let mut pattern = config.pattern.clone(); let file = File::open(&path)?; @@ -155,8 +98,8 @@ fn check_file(config: &config::Config, path: &Path) -> io::Result { }) } -fn print_report(matches: &Vec) { +fn print_report(config: &Config, matches: &Vec) { for file in matches { - file.print() + file.print(config) } } diff --git a/src/printer.rs b/src/printer.rs new file mode 100644 index 0000000..1760f9e --- /dev/null +++ b/src/printer.rs @@ -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!(); + } +}