diff --git a/README.md b/README.md index 380bd38..e1d5ae6 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ contree ### Options - `-d, --dir `: Specify the directory to scan (defaults to `.`). +- `--max-depth `: Maximum depth of directories to scan (relative to dir), unlimited if not specified. - `-g, --grep `: Filter files by content matching a pattern (plain text or `/regex/` for regex). - `-i, --include `: Comma-separated list of files to include (e.g., `file1.rs,file2.rs`). - `-D, --include-deps`: Include dependency files referenced in errors (Rust projects only). @@ -79,7 +80,12 @@ contree cargo test | contree --include-deps ``` -4. Use Makefile targets: +4. Scan a directory with a maximum depth of 2: + ```bash + contree --dir src --max-depth 2 + ``` + +5. Use Makefile targets: ```bash make run-grep GREP=transaction make run-include INCLUDE=src/main.rs diff --git a/src/main.rs b/src/main.rs index 7ccae83..4cfc57b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,6 +34,10 @@ struct Args { /// List of files to include (comma-separated), even if they don't match grep or are outside the directory #[arg(short = 'i', long, value_delimiter = ',', value_parser = parse_pathbuf)] include: Option>, + + /// Maximum depth of directories to scan (relative to dir), unlimited if not specified + #[arg(long)] + max_depth: Option, } fn parse_pathbuf(s: &str) -> Result { @@ -79,7 +83,7 @@ fn main() -> Result<()> { writeln!(output_writer, "\n=== Project Context ===\n")?; // Print files from the scanned directory with grep filtering - print_project_files(&cwd, &args.grep, &args.include, &mut output_writer)?; + print_project_files(&cwd, &args.grep, &args.include, &args.max_depth, &mut output_writer)?; // Include dependencies if requested if args.include_deps { @@ -118,11 +122,12 @@ fn print_file(path: &Path, writer: &mut Box) -> Result<()> { Ok(()) } -// Print all files in the project, respecting .gitignore, .contreeignore, grep filter, and include list +// Print all files in the project, respecting .gitignore, .contreeignore, grep filter, include list, and max depth fn print_project_files( cwd: &PathBuf, grep_pattern: &Option, include_files: &Option>, + max_depth: &Option, writer: &mut Box, ) -> Result<()> { // Compile the grep pattern into a regex if provided @@ -145,6 +150,11 @@ fn print_project_files( builder.git_exclude(false); builder.add_custom_ignore_filename(".contreeignore"); builder.add_ignore(".git"); // Explicitly ignore .git directories + + // Set max depth if specified + if let Some(depth) = max_depth { + builder.max_depth(Some(*depth)); + } // Add a custom filter to explicitly exclude .git directories at any depth builder.filter_entry(|entry| {