pctx 0.1.3

Generate LLM-ready context from your codebase
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# pctx


<p align="center">
  <img src="https://github.com/mc-marcocheng/pctx/blob/master/assets/pctx-carbon.png?raw=true" alt="pctx demo" width="600">
</p>

Generate LLM-ready context from your codebase. Intelligently packages source files with proper formatting, truncation, and filtering for optimal AI assistant consumption.


## Features


- **Smart file discovery**: Respects `.gitignore`, excludes binary files, and filters common non-source directories
- **Multiple output formats**: Markdown (default), XML, and plain text
- **Intelligent truncation**: Preserves file head and tail when truncating large files
- **Flexible filtering**: Include/exclude patterns with gitignore-style syntax
- **Multiple destinations**: stdout, clipboard, or file output
- **JSON mode**: Structured output for programmatic use and CI/CD integration
- **Stdin support**: Read file lists from pipes for integration with other tools
- **Token estimation**: Approximate token counts for various LLM models

## Installation


```bash
cargo install pctx
```

Or build from source:

```bash
git clone https://github.com/mc-marcocheng/pctx
cd pctx
cargo build --release
```

## Quick Start


```bash
# Generate context for current directory

pctx

# Copy to clipboard

pctx --clipboard

# Write to file

pctx --output context.md

# JSON output for scripts

pctx --json

# Filter specific files

pctx --include "*.rs" --include "*.toml"
pctx --exclude "*.test.ts" --exclude "__tests__"

# Pipe file list from other tools

find . -name "*.rs" -mtime -7 | pctx --stdin
pctx files list --quiet | grep -v test | pctx --stdin

# Preview without generating

pctx --dry-run

# Include file tree in output

pctx --tree

# Disable truncation for full file contents

pctx --no-truncation
```

## Usage


### Basic Commands


```bash
# Default: generate context from current directory

pctx [OPTIONS] [PATHS...]

# List files that would be included

pctx files list [OPTIONS]

# Show file tree structure

pctx files tree [OPTIONS]

# Configuration management

pctx config show      # Show current config
pctx config init      # Create .pctx.toml
pctx config defaults  # List default excludes

# Generate shell completions

pctx completions bash
pctx completions zsh
pctx completions fish
```

### Output Options


| Flag | Description |
|------|-------------|
| `--clipboard`, `-c` | Copy output to system clipboard |
| `--output FILE`, `-o` | Write to file (use `--force` to overwrite) |
| `--format`, `-f` | Output format: `markdown`, `xml`, `plain` |
| `--tree`, `-t` | Include file tree at beginning of output |
| `--stats`, `-s` | Show statistics summary |
| `--json` | Structured JSON output (for scripts) |
| `--stdin` | Read file paths from stdin (one per line) |

### Filtering Options


| Flag | Description |
|------|-------------|
| `--exclude PATTERN`, `-e` | Exclude files matching pattern (repeatable) |
| `--include PATTERN`, `-i` | Include only files matching pattern (repeatable) |
| `--hidden` | Include hidden files (starting with `.`) |
| `--no-default-excludes` | Disable built-in exclusions |
| `--no-gitignore` | Ignore `.gitignore` rules |
| `--max-size KB` | Maximum file size in KB (default: 1024) |
| `--max-depth N`, `-d` | Limit directory recursion depth |

### Truncation Options


| Flag | Description |
|------|-------------|
| `--no-truncation` | Disable all truncation |
| `--max-lines N` | Max lines per file before truncating (default: 500, 0 = unlimited) |
| `--head-lines N` | Lines to keep at file start (default: 20) |
| `--tail-lines N` | Lines to keep at file end (default: 10) |
| `--max-line-length N` | Max chars per line (default: 500, 0 = unlimited) |
| `--head-chars N` | Chars to keep at line start (default: 200) |
| `--tail-chars N` | Chars to keep at line end (default: 100) |

### Stdin Mode


The `--stdin` flag allows reading file paths from standard input, enabling powerful integrations:

```bash
# Process only recently modified Rust files

find . -name "*.rs" -mtime -1 | pctx --stdin

# Process files from a list

cat files_to_review.txt | pctx --stdin

# Chain with pctx's own file listing

pctx files list --quiet | grep -v _test | pctx --stdin

# Use with git to process only changed files
git diff --name-only HEAD~5 | pctx --stdin

# Process files matching complex criteria

fd -e rs -e toml --changed-within 2weeks | pctx --stdin
```

When using `--stdin`:
- Empty lines and whitespace-only lines are ignored
- Non-existent files are skipped with a warning (in verbose mode)
- Directories in the input are expanded recursively

## Configuration


### Config File


Create a `.pctx.toml` file in your project root:

```bash
pctx config init
```

Example configuration:

```toml
# Patterns to exclude (in addition to defaults)

exclude = [
    "*.generated.ts",
    "vendor/",
    "__snapshots__",
]

# Patterns to include (if specified, only these are included)

include = [
    "*.rs",
    "*.toml",
]

# Truncation settings

max_lines = 500
head_lines = 20
tail_lines = 10
max_line_length = 500
```

Configuration is loaded from `.pctx.toml` in the current directory or any parent directory. If the config file exists but has syntax errors, a warning is printed and the file is skipped.

### Configuration Precedence


Settings are applied in this order (highest priority first):

1. Command-line arguments
2. Config file (`.pctx.toml`)
3. Built-in defaults

### Default Exclusions


Common directories and files are excluded by default:

- **Version control**: `.git`, `.svn`, `.hg`
- **Dependencies**: `node_modules`, `vendor`, `target`, `.venv`
- **Build outputs**: `dist`, `build`, `out`, `bin`, `obj`
- **IDE/Editor**: `.idea`, `.vscode`, `.vs`
- **Caches**: `__pycache__`, `.cache`, `.pytest_cache`
- **Lock files**: `package-lock.json`, `yarn.lock`, `Cargo.lock`, etc.

See all defaults with: `pctx config defaults`

## Pattern Syntax


Patterns follow gitignore-style syntax:

| Pattern | Matches |
|---------|---------|
| `*.log` | All `.log` files |
| `test_*` | Files starting with `test_` |
| `**/tests/**` | Any `tests` directory at any level |
| `/src/generated` | `src/generated` at root only |
| `docs/` | `docs` directory |

**Limitations:**
- Negation patterns (`!pattern`) are not supported and will show a warning
- Character classes (`[abc]`) depend on glob crate support
- Some edge cases with `**/` patterns may differ from git behavior

## JSON Output


Use `--json` for structured output suitable for scripts and CI/CD:

```bash
# Generate context with JSON output

pctx --json

# List files as JSON

pctx files list --json

# Parse with jq

pctx --json | jq '.data.files[] | select(.truncated) | .path'
```

### Response Format


**Success:**
```json
{
  "status": "success",
  "data": {
    "content": "...",
    "format": "markdown",
    "files": [
      {
        "path": "src/main.rs",
        "extension": "rs",
        "size_bytes": 1234,
        "line_count": 45,
        "truncated": false
      }
    ]
  },
  "stats": {
    "file_count": 10,
    "total_lines": 500,
    "total_bytes": 15000,
    "truncated_count": 2,
    "skipped_count": 0,
    "token_estimate": 3500,
    "duration_ms": 45
  }
}
```

**Error:**
```json
{
  "status": "error",
  "code": "file_not_found",
  "message": "File not found: src/missing.rs",
  "input": {"path": "src/missing.rs"},
  "suggestion": "Check that the path exists and is spelled correctly",
  "transient": false,
  "exit_code": 3
}
```

**Partial Success:**
```json
{
  "status": "partial",
  "data": { ... },
  "stats": { ... },
  "errors": [
    {
      "path": "src/broken.rs",
      "code": "permission_denied",
      "message": "Permission denied",
      "transient": false
    }
  ]
}
```

## Exit Codes


| Code | Meaning |
|------|---------|
| 0 | Success |
| 1 | General failure |
| 2 | Usage error (bad arguments) |
| 3 | File/directory not found |
| 4 | Permission denied |
| 5 | Conflict (output file exists) |
| 6 | No files matched filters |
| 7 | Partial success (some files failed) |

## Examples


### Basic Usage


```bash
# Current directory to stdout

pctx

# Specific paths

pctx src/ tests/ README.md

# Copy to clipboard for pasting into ChatGPT

pctx --clipboard

# Save to file

pctx --output context.md
pctx -o context.md --force  # Overwrite existing
```

### Filtering


```bash
# Only Rust files

pctx --include "*.rs"

# Exclude tests

pctx --exclude "*_test.go" --exclude "**/__tests__/**"

# Include hidden files

pctx --hidden

# Disable default excludes (include node_modules, etc.)

pctx --no-default-excludes
```

### Output Formats


```bash
# Markdown (default) - good for most LLMs

pctx --format markdown

# XML - useful for Claude

pctx --format xml

# Plain text - minimal formatting

pctx --format plain
```

### Working with Large Codebases


```bash
# Limit recursion depth

pctx --max-depth 2

# Disable truncation for full content

pctx --no-truncation

# Custom truncation settings

pctx --max-lines 1000 --head-lines 50 --tail-lines 25

# Smaller file size limit

pctx --max-size 100  # 100KB max

# Preview what would be included

pctx --dry-run
pctx --dry-run --json  # With file details
```

### CI/CD Integration


```bash
# Check if context generation would succeed

pctx --dry-run --json > /dev/null && echo "OK" || echo "Failed"

# Generate context and check size

pctx --json | jq -e '.stats.token_estimate < 100000'

# List large files

pctx files list --json | jq '.data[] | select(.size_bytes > 50000)'

# Process only changed files in a PR

git diff --name-only origin/main | pctx --stdin --json
```

### Integration with Other Tools


```bash
# Process files found by fd/find

fd -e py -e js | pctx --stdin

# Process files from ripgrep matches

rg -l "TODO" | pctx --stdin

# Process git-tracked files only

git ls-files | pctx --stdin

# Combine with file filtering

pctx files list -q | grep -E '\.(rs|toml)$' | pctx --stdin
```

## Shell Completions


Generate completions for your shell:

```bash
# Bash

pctx completions bash > ~/.local/share/bash-completion/completions/pctx

# Zsh

pctx completions zsh > ~/.zfunc/_pctx

# Fish

pctx completions fish > ~/.config/fish/completions/pctx.fish

# PowerShell

pctx completions powershell > $PROFILE.CurrentUserAllHosts
```

## Troubleshooting


### "No files matched the specified filters"


- Check that your paths exist: `ls <path>`
- Try `--no-default-excludes` to include commonly excluded directories
- Use `pctx files list` to see what files would be included
- Check your `.pctx.toml` for overly restrictive patterns

### Clipboard not working on Linux


Clipboard access requires a display server. Make sure:
- You're running in a graphical environment (X11 or Wayland)
- `xclip` or `wl-clipboard` is installed

### Config file not being applied


If your `.pctx.toml` has syntax errors, pctx will warn and continue without it:
```
Warning: failed to load config file: TOML parse error...
```

Check your config syntax with:
```bash
pctx config show
```

### Files being truncated unexpectedly


Use `--no-truncation` to disable truncation, or adjust thresholds:
```bash
pctx --max-lines 1000 --max-line-length 1000
```

### Binary files being included


Binary files should be automatically detected and skipped. If a binary file is being included:
- Check if it has a text-like extension
- Use `--exclude` to explicitly exclude it

## License


MIT License - see [LICENSE](LICENSE) for details.

## Contributing


Contributions welcome! Please read [CONTRIBUTING.md](CONTRIBUTING.md) first.

```bash
# Run tests

cargo test

# Run with verbose output

cargo run -- --verbose .

# Run integration tests

cargo test --test integration_test
```