feat(tui): enhance system prompt with workspace structure information

This commit is contained in:
asepharyana
2026-07-20 16:16:26 +07:00
parent 6c7995f5f5
commit bed9f8cff6
2 changed files with 53 additions and 5 deletions
+42
View File
@@ -149,3 +149,45 @@ pub fn write_osc52(output: &mut impl Write, text: &str) -> std::io::Result<()> {
output.flush()?;
Ok(())
}
// ---------------------------------------------------------------------------
// Workspace Tree Builder
// ---------------------------------------------------------------------------
/// Build a string representing the directory tree of `root`, up to `max_files` limits.
/// Automatically respects `.gitignore` and `.ignore` via the `ignore` crate.
pub fn build_workspace_tree(root: &Path, max_files: usize) -> String {
use ignore::WalkBuilder;
let mut tree = String::new();
let mut count = 0;
// hidden(false) allows files like .github to be seen, but it still respects .gitignore
// and ignores .git directories by default.
for result in WalkBuilder::new(root).hidden(false).build() {
if let Ok(entry) = result {
if count >= max_files {
tree.push_str("\n... (truncated)");
break;
}
let path = entry.path();
if let Ok(rel) = path.strip_prefix(root) {
let name = rel.to_string_lossy();
if name.is_empty() {
tree.push_str(".\n");
} else {
let depth = entry.depth();
let indent = " ".repeat(depth.saturating_sub(1));
let is_dir = entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false);
let suffix = if is_dir { "/" } else { "" };
let filename = entry.file_name().to_string_lossy();
tree.push_str(&format!("{indent}{filename}{suffix}\n"));
count += 1;
}
}
}
}
tree.trim_end().to_string()
}