Ignore case and allowing the user to specify a path
Also ran rustfmt over the code.
This commit is contained in:
74
src/main.rs
74
src/main.rs
@@ -1,5 +1,6 @@
|
||||
use std::fs::{self, File};
|
||||
use std::io;
|
||||
use std::fs::{self};
|
||||
use std::io::prelude::*;
|
||||
use std::path::Path;
|
||||
|
||||
extern crate term;
|
||||
@@ -9,13 +10,20 @@ mod config;
|
||||
fn main() {
|
||||
let config = config::Config::from_args();
|
||||
|
||||
let path = Path::new(".");
|
||||
let search_path = &config.path;
|
||||
let path = match search_path.starts_with('/') {
|
||||
true => Path::new(search_path),
|
||||
false => {
|
||||
let p: &'static str = Box::leak(Box::new(String::from("./") + search_path));
|
||||
Path::new(p)
|
||||
}
|
||||
};
|
||||
|
||||
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::Err(msg) => println!("Error while parsing: {}", msg)
|
||||
Result::Err(msg) => println!("Error while parsing: {}", msg),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +63,7 @@ fn traverse_dir(
|
||||
#[derive(Debug)]
|
||||
struct FileMatch {
|
||||
file_name: String,
|
||||
matches: Vec<Match>
|
||||
matches: Vec<Match>,
|
||||
}
|
||||
|
||||
impl FileMatch {
|
||||
@@ -73,12 +81,18 @@ impl FileMatch {
|
||||
|
||||
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());
|
||||
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());
|
||||
print!(
|
||||
"{}",
|
||||
mat.text.get(match_index.start..match_index.end).unwrap()
|
||||
);
|
||||
term.reset().unwrap();
|
||||
|
||||
if idx == mat.match_indexes.len() -1 {
|
||||
if idx == mat.match_indexes.len() - 1 {
|
||||
println!("{}", mat.text.get(match_index.end..mat.text.len()).unwrap());
|
||||
last_position = 0;
|
||||
} else {
|
||||
@@ -94,43 +108,55 @@ impl FileMatch {
|
||||
struct Match {
|
||||
line_number: usize,
|
||||
match_indexes: Vec<MatchIndex>,
|
||||
text: String
|
||||
text: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct MatchIndex {
|
||||
start: usize,
|
||||
end: usize
|
||||
end: usize,
|
||||
}
|
||||
|
||||
fn check_file(config: &config::Config, path: &Path) -> io::Result<FileMatch> {
|
||||
let pattern = &config.pattern;
|
||||
let mut pattern = config.pattern.clone();
|
||||
|
||||
// TODO: Use BufRead and read_line instead of reading the entire file into memory.
|
||||
let content = fs::read_to_string(&path)?;
|
||||
let file = File::open(&path)?;
|
||||
let file = io::BufReader::new(file);
|
||||
let mut matches = Vec::new();
|
||||
for (number, line) in content.lines().enumerate() {
|
||||
if line.contains(pattern) {
|
||||
|
||||
for (number, line) in file.lines().enumerate() {
|
||||
let mut line = line?;
|
||||
let orig_line = line.clone();
|
||||
if config.ignore_case {
|
||||
line = line.to_lowercase();
|
||||
pattern = pattern.to_lowercase();
|
||||
}
|
||||
|
||||
if line.contains(&pattern) {
|
||||
let mut indexes = Vec::<MatchIndex>::new();
|
||||
for m in line.trim().match_indices(pattern) {
|
||||
indexes.push(MatchIndex { start: m.0, end: m.0 + pattern.len() });
|
||||
for m in line.trim().match_indices(&pattern) {
|
||||
indexes.push(MatchIndex {
|
||||
start: m.0,
|
||||
end: m.0 + pattern.len(),
|
||||
});
|
||||
}
|
||||
|
||||
matches.push(Match { line_number: number + 1, text: line.trim().to_string(), match_indexes: indexes });
|
||||
matches.push(Match {
|
||||
line_number: number + 1,
|
||||
text: orig_line.trim().to_string(),
|
||||
match_indexes: indexes,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(FileMatch { file_name: path.to_str().unwrap().to_string(), matches: matches })
|
||||
Ok(FileMatch {
|
||||
file_name: path.to_str().unwrap().trim_start_matches("./").to_string(),
|
||||
matches: matches,
|
||||
})
|
||||
}
|
||||
|
||||
fn print_report(matches: &Vec<FileMatch>) {
|
||||
for file in matches {
|
||||
file.print()
|
||||
// println!("{}:", file.file_name);
|
||||
// for mat in &file.matches {
|
||||
// println!("{}", mat.text);
|
||||
// }
|
||||
// println!();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user