aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/fs.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/fs.rs')
-rw-r--r--src/tools/fs.rs38
1 files changed, 31 insertions, 7 deletions
diff --git a/src/tools/fs.rs b/src/tools/fs.rs
index ac0a475..0075590 100644
--- a/src/tools/fs.rs
+++ b/src/tools/fs.rs
@@ -1,3 +1,10 @@
1/*
2 * This file is part of agent686
3 * Copyright (C) 2026 Salmonik <salmon@2137697.xyz>
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 * https://git.2137697.xyz/agent686
6 */
7
1use std::fs; 8use std::fs;
2use std::io::{BufRead, BufReader}; 9use std::io::{BufRead, BufReader};
3use std::path::{Path, PathBuf}; 10use std::path::{Path, PathBuf};
@@ -30,8 +37,15 @@ const SKIP_DIRS: &[&str] = &[
30/// human agrees. The file is re-read at apply time, so an edit made while the 37/// human agrees. The file is re-read at apply time, so an edit made while the
31/// approval prompt was open is not silently discarded. 38/// approval prompt was open is not silently discarded.
32pub enum Change { 39pub enum Change {
33 Write { path: String, content: String }, 40 Write {
34 Edit { path: String, old: String, new: String }, 41 path: String,
42 content: String,
43 },
44 Edit {
45 path: String,
46 old: String,
47 new: String,
48 },
35} 49}
36 50
37impl Change { 51impl Change {
@@ -157,9 +171,11 @@ fn locate(text: &str, old: &str) -> Result<Span, String> {
157 } 171 }
158 } 172 }
159 173
160 Err("text to replace was not found. Read the file and copy the exact text, \ 174 Err(
175 "text to replace was not found. Read the file and copy the exact text, \
161 including indentation." 176 including indentation."
162 .to_string()) 177 .to_string(),
178 )
163} 179}
164 180
165/// Byte offset in the original text for an offset in the CRLF-normalised text. 181/// Byte offset in the original text for an offset in the CRLF-normalised text.
@@ -262,7 +278,11 @@ pub fn plan_edit(args: &Value) -> Result<Change, String> {
262 278
263pub fn read(args: &Value) -> Result<String, String> { 279pub fn read(args: &Value) -> Result<String, String> {
264 let path = req(args, "path")?; 280 let path = req(args, "path")?;
265 let offset = args.get("offset").and_then(|v| v.as_u64()).unwrap_or(1).max(1); 281 let offset = args
282 .get("offset")
283 .and_then(|v| v.as_u64())
284 .unwrap_or(1)
285 .max(1);
266 let offset = usize::try_from(offset) 286 let offset = usize::try_from(offset)
267 .map_err(|_| format!("offset {offset} is too large for this build"))?; 287 .map_err(|_| format!("offset {offset} is too large for this build"))?;
268 let limit = args 288 let limit = args
@@ -622,7 +642,8 @@ fn walk(dir: &Path, out: &mut Vec<PathBuf>) {
622 walk(&path, out); 642 walk(&path, out);
623 } else if file_type.is_file() { 643 } else if file_type.is_file() {
624 out.push(path); 644 out.push(path);
625 } else if file_type.is_symlink() && fs::metadata(&path).map(|m| m.is_file()).unwrap_or(false) 645 } else if file_type.is_symlink()
646 && fs::metadata(&path).map(|m| m.is_file()).unwrap_or(false)
626 { 647 {
627 // Follow file symlinks; symlinked directories are skipped so a loop 648 // Follow file symlinks; symlinked directories are skipped so a loop
628 // cannot walk forever. 649 // cannot walk forever.
@@ -679,7 +700,10 @@ fn new_file_preview(path: &str, content: &str) -> String {
679 out.push_str(&format!("+ {}\n", clip(line, 200))); 700 out.push_str(&format!("+ {}\n", clip(line, 200)));
680 } 701 }
681 if lines.len() > PREVIEW_BUDGET { 702 if lines.len() > PREVIEW_BUDGET {
682 out.push_str(&format!("… {} more line(s)\n", lines.len() - PREVIEW_BUDGET)); 703 out.push_str(&format!(
704 "… {} more line(s)\n",
705 lines.len() - PREVIEW_BUDGET
706 ));
683 } 707 }
684 out 708 out
685} 709}