diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-15 15:27:16 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-15 15:27:16 +0100 |
| commit | 5563622557ac60b6842f6c09c4c76a5ece9f6d09 (patch) | |
| tree | 9a841885b861d8543f21d19738e7db5a663f72e5 /src/tools/fs.rs | |
| parent | 0.3.0: --version and --doctor (diff) | |
| download | agent686-5563622557ac60b6842f6c09c4c76a5ece9f6d09.tar.gz agent686-5563622557ac60b6842f6c09c4c76a5ece9f6d09.zip | |
0.4.0: grep takes a regular expression, fixed=true for literals
Invalid patterns explain themselves and point at fixed, rather than failing with
a bare regex error the model cannot act on.
Diffstat (limited to '')
| -rw-r--r-- | src/tools/fs.rs | 63 |
1 files changed, 48 insertions, 15 deletions
diff --git a/src/tools/fs.rs b/src/tools/fs.rs index e543cb6..caa3992 100644 --- a/src/tools/fs.rs +++ b/src/tools/fs.rs | |||
| @@ -268,6 +268,29 @@ fn read_line_limited<R: BufRead>( | |||
| 268 | } | 268 | } |
| 269 | } | 269 | } |
| 270 | 270 | ||
| 271 | enum Matcher { | ||
| 272 | Literal { needle: String, ignore_case: bool }, | ||
| 273 | Regex(regex::Regex), | ||
| 274 | } | ||
| 275 | |||
| 276 | impl Matcher { | ||
| 277 | fn is_match(&self, line: &str) -> bool { | ||
| 278 | match self { | ||
| 279 | Matcher::Literal { | ||
| 280 | needle, | ||
| 281 | ignore_case, | ||
| 282 | } => { | ||
| 283 | if *ignore_case { | ||
| 284 | line.to_lowercase().contains(needle) | ||
| 285 | } else { | ||
| 286 | line.contains(needle) | ||
| 287 | } | ||
| 288 | } | ||
| 289 | Matcher::Regex(regex) => regex.is_match(line), | ||
| 290 | } | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 271 | pub fn grep(args: &Value) -> Result<String, String> { | 294 | pub fn grep(args: &Value) -> Result<String, String> { |
| 272 | let pattern = req(args, "pattern")?; | 295 | let pattern = req(args, "pattern")?; |
| 273 | if pattern.is_empty() { | 296 | if pattern.is_empty() { |
| @@ -282,10 +305,29 @@ pub fn grep(args: &Value) -> Result<String, String> { | |||
| 282 | .get("ignore_case") | 305 | .get("ignore_case") |
| 283 | .and_then(|v| v.as_bool()) | 306 | .and_then(|v| v.as_bool()) |
| 284 | .unwrap_or(false); | 307 | .unwrap_or(false); |
| 285 | let needle = if ignore_case { | 308 | let fixed = args.get("fixed").and_then(|v| v.as_bool()).unwrap_or(false); |
| 286 | pattern.to_lowercase() | 309 | |
| 310 | let matcher = if fixed { | ||
| 311 | Matcher::Literal { | ||
| 312 | needle: if ignore_case { | ||
| 313 | pattern.to_lowercase() | ||
| 314 | } else { | ||
| 315 | pattern.clone() | ||
| 316 | }, | ||
| 317 | ignore_case, | ||
| 318 | } | ||
| 287 | } else { | 319 | } else { |
| 288 | pattern.clone() | 320 | Matcher::Regex( |
| 321 | regex::RegexBuilder::new(&pattern) | ||
| 322 | .case_insensitive(ignore_case) | ||
| 323 | .build() | ||
| 324 | .map_err(|error| { | ||
| 325 | format!( | ||
| 326 | "invalid regex `{pattern}`: {error}\npass \"fixed\": true to match \ | ||
| 327 | the text literally" | ||
| 328 | ) | ||
| 329 | })?, | ||
| 330 | ) | ||
| 289 | }; | 331 | }; |
| 290 | 332 | ||
| 291 | let root = Path::new(&base); | 333 | let root = Path::new(&base); |
| @@ -303,7 +345,7 @@ pub fn grep(args: &Value) -> Result<String, String> { | |||
| 303 | if matches >= MAX_MATCHES { | 345 | if matches >= MAX_MATCHES { |
| 304 | break; | 346 | break; |
| 305 | } | 347 | } |
| 306 | grep_file(&file, &needle, ignore_case, &mut matches, &mut out); | 348 | grep_file(&file, &matcher, &mut matches, &mut out); |
| 307 | } | 349 | } |
| 308 | 350 | ||
| 309 | if matches == 0 { | 351 | if matches == 0 { |
| @@ -368,7 +410,7 @@ pub fn glob(args: &Value) -> Result<String, String> { | |||
| 368 | Ok(out) | 410 | Ok(out) |
| 369 | } | 411 | } |
| 370 | 412 | ||
| 371 | fn grep_file(path: &Path, needle_lower: &str, ignore_case: bool, count: &mut usize, out: &mut String) { | 413 | fn grep_file(path: &Path, matcher: &Matcher, count: &mut usize, out: &mut String) { |
| 372 | let meta = match fs::metadata(path) { | 414 | let meta = match fs::metadata(path) { |
| 373 | Ok(m) => m, | 415 | Ok(m) => m, |
| 374 | Err(_) => return, | 416 | Err(_) => return, |
| @@ -384,21 +426,12 @@ fn grep_file(path: &Path, needle_lower: &str, ignore_case: bool, count: &mut usi | |||
| 384 | return; | 426 | return; |
| 385 | } | 427 | } |
| 386 | let text = String::from_utf8_lossy(&bytes); | 428 | let text = String::from_utf8_lossy(&bytes); |
| 387 | let mut needle = needle_lower.to_string(); | ||
| 388 | if !ignore_case { | ||
| 389 | needle = needle.to_string(); | ||
| 390 | } | ||
| 391 | 429 | ||
| 392 | for (index, line) in text.lines().enumerate() { | 430 | for (index, line) in text.lines().enumerate() { |
| 393 | if *count >= MAX_MATCHES { | 431 | if *count >= MAX_MATCHES { |
| 394 | return; | 432 | return; |
| 395 | } | 433 | } |
| 396 | let hit = if ignore_case { | 434 | if matcher.is_match(line) { |
| 397 | line.to_lowercase().contains(&needle) | ||
| 398 | } else { | ||
| 399 | line.contains(&needle) | ||
| 400 | }; | ||
| 401 | if hit { | ||
| 402 | *count += 1; | 435 | *count += 1; |
| 403 | out.push_str(&format!( | 436 | out.push_str(&format!( |
| 404 | "{}:{}: {}\n", | 437 | "{}:{}: {}\n", |
