Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ contree

### Options
- `-d, --dir <PATH>`: Specify the directory to scan (defaults to `.`).
- `--max-depth <NUMBER>`: Maximum depth of directories to scan (relative to dir), unlimited if not specified.
- `-g, --grep <PATTERN>`: Filter files by content matching a pattern (plain text or `/regex/` for regex).
- `-i, --include <FILES>`: 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).
Expand All @@ -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
Expand Down
14 changes: 12 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<PathBuf>>,

/// Maximum depth of directories to scan (relative to dir), unlimited if not specified
#[arg(long)]
max_depth: Option<usize>,
}

fn parse_pathbuf(s: &str) -> Result<PathBuf, String> {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -118,11 +122,12 @@ fn print_file(path: &Path, writer: &mut Box<dyn Write>) -> 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<String>,
include_files: &Option<Vec<PathBuf>>,
max_depth: &Option<usize>,
writer: &mut Box<dyn Write>,
) -> Result<()> {
// Compile the grep pattern into a regex if provided
Expand All @@ -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| {
Expand Down