2018-04-29 15:29:52 +02:00
|
|
|
extern crate grep_regex;
|
|
|
|
extern crate grep_searcher;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::error::Error;
|
|
|
|
use std::io;
|
|
|
|
use std::process;
|
|
|
|
|
|
|
|
use grep_regex::RegexMatcher;
|
|
|
|
use grep_searcher::sinks::UTF8;
|
2020-02-18 01:08:47 +02:00
|
|
|
use grep_searcher::Searcher;
|
2018-04-29 15:29:52 +02:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
if let Err(err) = example() {
|
|
|
|
eprintln!("{}", err);
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 22:47:33 +02:00
|
|
|
fn example() -> Result<(), Box<dyn Error>> {
|
2018-04-29 15:29:52 +02:00
|
|
|
let pattern = match env::args().nth(1) {
|
|
|
|
Some(pattern) => pattern,
|
2020-02-18 01:08:47 +02:00
|
|
|
None => {
|
|
|
|
return Err(From::from(format!("Usage: search-stdin <pattern>")))
|
|
|
|
}
|
2018-04-29 15:29:52 +02:00
|
|
|
};
|
|
|
|
let matcher = RegexMatcher::new(&pattern)?;
|
2020-02-18 01:08:47 +02:00
|
|
|
Searcher::new().search_reader(
|
|
|
|
&matcher,
|
|
|
|
io::stdin(),
|
|
|
|
UTF8(|lnum, line| {
|
|
|
|
print!("{}:{}", lnum, line);
|
|
|
|
Ok(true)
|
|
|
|
}),
|
|
|
|
)?;
|
2018-04-29 15:29:52 +02:00
|
|
|
Ok(())
|
|
|
|
}
|