├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md └── src ├── lib.rs └── main.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target/ -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "0.7.18" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "atty" 16 | version = "0.2.14" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" 19 | dependencies = [ 20 | "hermit-abi", 21 | "libc", 22 | "winapi", 23 | ] 24 | 25 | [[package]] 26 | name = "autocfg" 27 | version = "1.1.0" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" 30 | 31 | [[package]] 32 | name = "bitflags" 33 | version = "1.3.2" 34 | source = "registry+https://github.com/rust-lang/crates.io-index" 35 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 36 | 37 | [[package]] 38 | name = "clap" 39 | version = "3.0.14" 40 | source = "registry+https://github.com/rust-lang/crates.io-index" 41 | checksum = "b63edc3f163b3c71ec8aa23f9bd6070f77edbf3d1d198b164afa90ff00e4ec62" 42 | dependencies = [ 43 | "atty", 44 | "bitflags", 45 | "clap_derive", 46 | "indexmap", 47 | "lazy_static", 48 | "os_str_bytes", 49 | "strsim", 50 | "termcolor", 51 | "textwrap", 52 | ] 53 | 54 | [[package]] 55 | name = "clap_derive" 56 | version = "3.0.14" 57 | source = "registry+https://github.com/rust-lang/crates.io-index" 58 | checksum = "9a1132dc3944b31c20dd8b906b3a9f0a5d0243e092d59171414969657ac6aa85" 59 | dependencies = [ 60 | "heck", 61 | "proc-macro-error", 62 | "proc-macro2", 63 | "quote", 64 | "syn", 65 | ] 66 | 67 | [[package]] 68 | name = "colored" 69 | version = "2.0.0" 70 | source = "registry+https://github.com/rust-lang/crates.io-index" 71 | checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" 72 | dependencies = [ 73 | "atty", 74 | "lazy_static", 75 | "winapi", 76 | ] 77 | 78 | [[package]] 79 | name = "hashbrown" 80 | version = "0.11.2" 81 | source = "registry+https://github.com/rust-lang/crates.io-index" 82 | checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" 83 | 84 | [[package]] 85 | name = "heck" 86 | version = "0.4.0" 87 | source = "registry+https://github.com/rust-lang/crates.io-index" 88 | checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" 89 | 90 | [[package]] 91 | name = "hermit-abi" 92 | version = "0.1.19" 93 | source = "registry+https://github.com/rust-lang/crates.io-index" 94 | checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" 95 | dependencies = [ 96 | "libc", 97 | ] 98 | 99 | [[package]] 100 | name = "indexmap" 101 | version = "1.8.0" 102 | source = "registry+https://github.com/rust-lang/crates.io-index" 103 | checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" 104 | dependencies = [ 105 | "autocfg", 106 | "hashbrown", 107 | ] 108 | 109 | [[package]] 110 | name = "lazy_static" 111 | version = "1.4.0" 112 | source = "registry+https://github.com/rust-lang/crates.io-index" 113 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 114 | 115 | [[package]] 116 | name = "lazyfinder" 117 | version = "0.1.5" 118 | dependencies = [ 119 | "clap", 120 | "colored", 121 | "lazy_static", 122 | "regex", 123 | "walkdir", 124 | ] 125 | 126 | [[package]] 127 | name = "libc" 128 | version = "0.2.117" 129 | source = "registry+https://github.com/rust-lang/crates.io-index" 130 | checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c" 131 | 132 | [[package]] 133 | name = "memchr" 134 | version = "2.4.1" 135 | source = "registry+https://github.com/rust-lang/crates.io-index" 136 | checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" 137 | 138 | [[package]] 139 | name = "os_str_bytes" 140 | version = "6.0.0" 141 | source = "registry+https://github.com/rust-lang/crates.io-index" 142 | checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" 143 | dependencies = [ 144 | "memchr", 145 | ] 146 | 147 | [[package]] 148 | name = "proc-macro-error" 149 | version = "1.0.4" 150 | source = "registry+https://github.com/rust-lang/crates.io-index" 151 | checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" 152 | dependencies = [ 153 | "proc-macro-error-attr", 154 | "proc-macro2", 155 | "quote", 156 | "syn", 157 | "version_check", 158 | ] 159 | 160 | [[package]] 161 | name = "proc-macro-error-attr" 162 | version = "1.0.4" 163 | source = "registry+https://github.com/rust-lang/crates.io-index" 164 | checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" 165 | dependencies = [ 166 | "proc-macro2", 167 | "quote", 168 | "version_check", 169 | ] 170 | 171 | [[package]] 172 | name = "proc-macro2" 173 | version = "1.0.36" 174 | source = "registry+https://github.com/rust-lang/crates.io-index" 175 | checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" 176 | dependencies = [ 177 | "unicode-xid", 178 | ] 179 | 180 | [[package]] 181 | name = "quote" 182 | version = "1.0.15" 183 | source = "registry+https://github.com/rust-lang/crates.io-index" 184 | checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" 185 | dependencies = [ 186 | "proc-macro2", 187 | ] 188 | 189 | [[package]] 190 | name = "regex" 191 | version = "1.5.4" 192 | source = "registry+https://github.com/rust-lang/crates.io-index" 193 | checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" 194 | dependencies = [ 195 | "aho-corasick", 196 | "memchr", 197 | "regex-syntax", 198 | ] 199 | 200 | [[package]] 201 | name = "regex-syntax" 202 | version = "0.6.25" 203 | source = "registry+https://github.com/rust-lang/crates.io-index" 204 | checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" 205 | 206 | [[package]] 207 | name = "same-file" 208 | version = "1.0.6" 209 | source = "registry+https://github.com/rust-lang/crates.io-index" 210 | checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" 211 | dependencies = [ 212 | "winapi-util", 213 | ] 214 | 215 | [[package]] 216 | name = "strsim" 217 | version = "0.10.0" 218 | source = "registry+https://github.com/rust-lang/crates.io-index" 219 | checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" 220 | 221 | [[package]] 222 | name = "syn" 223 | version = "1.0.86" 224 | source = "registry+https://github.com/rust-lang/crates.io-index" 225 | checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" 226 | dependencies = [ 227 | "proc-macro2", 228 | "quote", 229 | "unicode-xid", 230 | ] 231 | 232 | [[package]] 233 | name = "termcolor" 234 | version = "1.1.2" 235 | source = "registry+https://github.com/rust-lang/crates.io-index" 236 | checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" 237 | dependencies = [ 238 | "winapi-util", 239 | ] 240 | 241 | [[package]] 242 | name = "textwrap" 243 | version = "0.14.2" 244 | source = "registry+https://github.com/rust-lang/crates.io-index" 245 | checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" 246 | 247 | [[package]] 248 | name = "unicode-xid" 249 | version = "0.2.2" 250 | source = "registry+https://github.com/rust-lang/crates.io-index" 251 | checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" 252 | 253 | [[package]] 254 | name = "version_check" 255 | version = "0.9.4" 256 | source = "registry+https://github.com/rust-lang/crates.io-index" 257 | checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" 258 | 259 | [[package]] 260 | name = "walkdir" 261 | version = "2.3.2" 262 | source = "registry+https://github.com/rust-lang/crates.io-index" 263 | checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" 264 | dependencies = [ 265 | "same-file", 266 | "winapi", 267 | "winapi-util", 268 | ] 269 | 270 | [[package]] 271 | name = "winapi" 272 | version = "0.3.9" 273 | source = "registry+https://github.com/rust-lang/crates.io-index" 274 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 275 | dependencies = [ 276 | "winapi-i686-pc-windows-gnu", 277 | "winapi-x86_64-pc-windows-gnu", 278 | ] 279 | 280 | [[package]] 281 | name = "winapi-i686-pc-windows-gnu" 282 | version = "0.4.0" 283 | source = "registry+https://github.com/rust-lang/crates.io-index" 284 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 285 | 286 | [[package]] 287 | name = "winapi-util" 288 | version = "0.1.5" 289 | source = "registry+https://github.com/rust-lang/crates.io-index" 290 | checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" 291 | dependencies = [ 292 | "winapi", 293 | ] 294 | 295 | [[package]] 296 | name = "winapi-x86_64-pc-windows-gnu" 297 | version = "0.4.0" 298 | source = "registry+https://github.com/rust-lang/crates.io-index" 299 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 300 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "lazyfinder" 3 | version = "0.1.5" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | walkdir = "2.3.2" 10 | clap = { version = "3.0.14", features = ["derive"] } 11 | colored = "2.0.0" 12 | regex = "1" 13 | lazy_static = "1.4.0" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lazyfinder 2 | 3 | ## 快速查询,极致性能 4 | 5 | 6 | ``` 7 | cargo build --release 8 | 9 | example: 10 | 不启用正则,仅匹配key,用逗号隔开 11 | ./lazyfinder -d / -p ".py" -k "120.77.35.111,get_username(),Library/Logs/tmp" > result.txt 12 | 启用正则,最后加-r参数 13 | ./lazyfinder -d ~/ -p "txt" -k "\d+\.\d+\.\d+\.\d+" -r > result.txt 14 | ``` 15 | 16 | # 效果 17 | 18 | image 19 | image 20 | 21 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | use regex::Regex; 2 | use std::fs; 3 | use std::error::Error; 4 | use colored::Colorize; 5 | 6 | pub struct FileMatcher<'a>{ 7 | pub filename:&'a String, 8 | pub keys:&'a Vec, 9 | pub is_reg_word:&'a bool, 10 | } 11 | 12 | impl<'a> FileMatcher<'a> { 13 | pub fn new(filename:&'a String,k:&'a Vec,iw:&'a bool) -> FileMatcher<'a>{ 14 | let keys = k; 15 | let is_reg_word = iw; 16 | FileMatcher{filename,keys,is_reg_word} 17 | } 18 | } 19 | pub fn reg_search(i:&str,rm:&str,contents:&str) -> (){ 20 | let re = Regex::new(&rm).unwrap(); 21 | if re.is_match(contents){ 22 | println!("{} : {} ","path".blue(),&i); 23 | let mut count = 0; 24 | for i in contents.lines(){ 25 | count+=1; 26 | for j in re.find_iter(i){ 27 | println!("\t{} {}: {}","line".red(),count.to_string().green(),j.as_str()) 28 | } 29 | } 30 | }else{ 31 | return ; 32 | } 33 | } 34 | 35 | pub fn normal_search(i:&str,key:&str,contents:&str)->(){ 36 | if contents.contains(key){ 37 | println!("{} : {} ","path".blue(),&i); 38 | let mut count = 0; 39 | for l in contents.lines(){ 40 | count+=1; 41 | if l.contains(key){ 42 | println!("\t{} {}: {}","line".red(),count.to_string().green(),l) 43 | } 44 | } 45 | }else{ 46 | return ; 47 | } 48 | } 49 | 50 | 51 | pub fn run(file_matcher:FileMatcher) -> Result<(),Box>{ 52 | 53 | if file_matcher.is_reg_word.clone(){ 54 | 55 | for j in file_matcher.keys.iter(){ 56 | match fs::read_to_string(file_matcher.filename.clone()){ 57 | Ok(contents)=>reg_search(&file_matcher.filename,&j, &contents), 58 | _=>() 59 | } 60 | } 61 | 62 | }else { 63 | for j in file_matcher.keys.iter(){ 64 | match fs::read_to_string(file_matcher.filename.clone()){ 65 | Ok(contents)=>normal_search(&file_matcher.filename,&j, &contents), 66 | _=>() 67 | } 68 | } 69 | } 70 | Ok(()) 71 | } 72 | 73 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use colored::Colorize; 2 | use walkdir::WalkDir; 3 | use clap::Parser; 4 | /// 遍历目标目录中包含指定关键字的文件,并从匹配到的文件中匹配特定字符串所在行 5 | #[derive(Parser, Debug)] 6 | #[clap(author, version, about, long_about = None)] 7 | struct Args { 8 | /// 查询文件目标目录 9 | #[clap(short, long)] 10 | dir: String, 11 | 12 | /// 指定文件名中包含的关键字 13 | #[clap(short, long)] 14 | pre: String, 15 | 16 | /// 指定文件内容包含的关键字(支持正则) 17 | #[clap(short, long)] 18 | keys: String, 19 | 20 | /// 是否启用正则(默认关闭,输入y开启正则模式) 21 | #[clap(short, long,takes_value = false)] 22 | reg:bool, 23 | } 24 | 25 | fn main() { 26 | 27 | let args = Args::parse(); 28 | let dir = args.dir; 29 | let pre:Vec<&str> = args.pre.as_str().split(",").collect(); 30 | println!("{}","Walking dir....please wait.....".red()); 31 | let f_name:Vec = WalkDir::new(dir) 32 | .into_iter() 33 | .filter_map(Result::ok) 34 | .filter(|e| !e.file_type().is_dir()) 35 | .map(|x| 36 | x.path().to_string_lossy().to_string()) 37 | .collect(); 38 | 39 | let keys = if args.reg{ 40 | vec![args.keys.as_str().to_string()] 41 | }else{ 42 | args.keys.as_str().split(",").map(|x| x.to_string()).collect() 43 | }; 44 | for i in pre{ 45 | for j in &f_name{ 46 | if j.contains(i){ 47 | let engine = lazyfinder::FileMatcher::new(j,&keys,&args.reg); 48 | if let Err(e) = lazyfinder::run(engine){ 49 | println!("Error !:{}",e); 50 | }; 51 | } 52 | } 53 | } 54 | println!("{}","done....".red()); 55 | } --------------------------------------------------------------------------------