# WatchDiff ๐ญ
A professional-grade file watcher with beautiful TUI, multiple diff algorithms, and comprehensive patch export capabilities, written in Rust.
## Features
### Core Capabilities
- ๐ **High Performance**: Built with Rust for maximum speed and efficiency
- ๐จ **Beautiful TUI**: Rich terminal interface with scrollable diff log and file list
- ๐ **Syntax Highlighting**: Full syntax highlighting for 25+ programming languages in diffs
- ๐ **Smart Filtering**: Respects `.gitignore` patterns automatically
- ๐ **Real-time Diffs**: Shows beautiful diffs for text file changes as they happen
- โจ๏ธ **Easy CLI**: Multiple output formats and intuitive keyboard shortcuts
- ๐งต **Async**: Non-blocking file watching with threaded architecture
### Advanced Features
- ๐ง **Multiple Diff Algorithms**: Choose from Myers, Patience, or LCS algorithms
- ๐ค **Patch Export**: Export changes as unified diffs, Git patches, or multifile bundles
- ๐ **Rich Statistics**: Comprehensive diff statistics with addition/deletion ratios
- ๐๏ธ **Modular Architecture**: Clean separation of concerns with trait-based design
- ๐ฏ **Professional Tools**: Enterprise-grade patch management and export functionality
## Installation
### From Cargo
```bash
cargo install watchdiff-tui
```
### Build from Source
```bash
git clone git@github.com:xicv/watchdiff.git
cd watchdiff
cargo build --release
./target/release/watchdiff
```
## Usage
### Basic Usage
```bash
# Watch current directory with TUI
watchdiff-tui
# Watch specific directory
watchdiff-tui /path/to/project
# Watch with text output
watchdiff-tui --output text
# Watch only specific file types
watchdiff-tui --extensions rs,py,js
# JSON output for scripting
watchdiff-tui --output json
```
### Advanced Usage
```bash
# Use different diff algorithms
watchdiff-tui --algorithm myers # Fast, general purpose (default)
watchdiff-tui --algorithm patience # Better for refactored code
watchdiff-tui --algorithm lcs # Minimal diffs
# Export patches while watching
watchdiff-tui --export-dir ./patches /path/to/project
# Combine algorithm and export
watchdiff-tui --algorithm patience --export-dir ./patches
```
## User Interface
### TUI Mode (Default)
WatchDiff features a modern, responsive terminal user interface built with ratatui. The interface is designed for maximum productivity and ease of use.
#### Interface Layout
```
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Changes (โโ to scroll, PgUp/PgDn, Home/End) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ [12:34:56] MODIFIED src/main.rs โ
โ --- src/main.rs โ
โ +++ src/main.rs โ
โ @@ -15,7 +15,8 @@ โ
โ - fn main() { โ
โ + fn main() -> Result<()> { โ
โ let cli = Cli::parse(); โ
โ + println!("Starting WatchDiff..."); โ
โ โ
โ [12:34:52] CREATED docs/api.md โ
โ Preview: โ
โ # API Documentation โ
โ This document describes the WatchDiff API... โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Watched Files (847) (โโ to scroll) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ src/main.rs โ
โ src/lib.rs โ
โ src/cli.rs โ
โ src/watcher.rs โ
โ src/diff.rs โ
โ src/tui.rs โ
โ ... โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Status โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Press q to quit, h for help, โโ to scroll diff โ
```
#### Panel Breakdown
**๐ Top Panel - Changes Log (70% height)**
- Real-time scrollable feed of file changes
- Color-coded event types:
- ๐ข **CREATED** - New files (green)
- ๐ก **MODIFIED** - Changed files (yellow)
- ๐ด **DELETED** - Removed files (red)
- ๐ต **MOVED** - Renamed/moved files (blue)
- **Full syntax highlighting** for 25+ languages including:
- Rust, Python, JavaScript, TypeScript, Java, C/C++, Go
- HTML, CSS, JSON, YAML, TOML, XML, Markdown
- Bash, SQL, Dockerfile, and many more
- Unified diff format with intelligent syntax preservation
- Timestamps for each event
- Syntax-highlighted content preview for new files
- Scrollbar for long change lists
**๐ Middle Panel - File List (25% height)**
- Live list of all watched files
- File count indicator
- Alternating row colors for readability
- Horizontal scrolling for long paths
- Respects .gitignore patterns
**โน๏ธ Bottom Panel - Status Bar (5% height)**
- Real-time statistics (event count, file count)
- Keyboard shortcuts reminder
- Application status indicators
#### Visual Features
- **Color Coding**: Intuitive colors for different change types
- **Responsive Layout**: Adapts to terminal size changes
- **Smooth Scrolling**: Efficient scrolling through large change logs
- **Typography**: Clear, readable text with proper spacing
- **Borders**: Clean visual separation between panels
- **Icons**: Unicode symbols for better visual hierarchy
#### Keyboard Shortcuts
| `q`, `Esc` | Quit application |
| `h`, `F1` | Toggle help screen |
| `โ`, `k` | Scroll diff log up |
| `โ`, `j` | Scroll diff log down |
| `PgUp` | Scroll diff log up (fast) |
| `PgDn` | Scroll diff log down (fast) |
| `Home` | Go to top of diff log |
| `End` | Go to bottom of diff log |
| `โ`, `โ` | Scroll file list |
#### Help Screen
Press `h` or `F1` to open the interactive help screen:
```
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Help โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ WatchDiff - File Watching Tool โ
โ โ
โ Keyboard Shortcuts: โ
โ โ
โ q, Esc - Quit the application โ
โ h, F1 - Show/hide this help โ
โ โ, k - Scroll diff log up โ
โ โ, j - Scroll diff log down โ
โ PgUp - Scroll diff log up (fast) โ
โ PgDn - Scroll diff log down (fast) โ
โ Home - Go to top of diff log โ
โ End - Go to bottom of diff log โ
โ โ, โ - Scroll file list โ
โ โ
โ Features: โ
โ โ
โ โข Real-time file change monitoring โ
โ โข Respects .gitignore patterns โ
โ โข Shows diffs for text file changes โ
โ โข Scrollable diff log and file list โ
โ โข High performance with async processing โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```
### Alternative Output Modes
For non-interactive use cases, WatchDiff supports several output formats:
#### Text Mode (`--output text`)
```bash
$ watchdiff-tui --output text
Watching: /home/user/project
Press Ctrl+C to quit
---
[12:34:56] MODIFIED src/main.rs
- fn main() {
+ fn main() -> Result<()> {
let cli = Cli::parse();
+ println!("Starting WatchDiff...");
[12:34:52] CREATED docs/README.md
```
#### JSON Mode (`--output json`)
```json
{"path":"src/main.rs","kind":"Modified","timestamp":{"secs_since_epoch":1703932496,"nanos_since_epoch":0},"diff":"--- src/main.rs\n+++ src/main.rs\n@@ -1,3 +1,4 @@\n-fn main() {\n+fn main() -> Result<()> {\n let cli = Cli::parse();\n+ println!(\"Starting WatchDiff...\");","content_preview":null}
```
#### Compact Mode (`--output compact`)
```bash
$ watchdiff-tui --output compact
M src/main.rs
C docs/README.md
D old_file.txt
```
**Format Legend:**
- `C` = Created
- `M` = Modified
- `D` = Deleted
- `V` = Moved
## CLI Options
```
Options:
-m, --mode <MODE> File watching mode [auto|native|polling]
--max-events <N> Maximum events to store [default: 1000]
-v, --verbose Enable verbose logging
--no-color Disable colored output
--extensions <EXTS> File extensions to watch (e.g., rs,py,js)
--ignore <PATTERNS> Additional patterns to ignore
--context <N> Number of diff context lines [default: 3]
--output <FORMAT> Output format [tui|json|text|compact]
--poll-interval <MS> Polling interval in ms [default: 1000]
--algorithm <ALG> Diff algorithm [myers|patience|lcs] [default: myers]
--export-dir <DIR> Export patches to directory (TUI mode only)
```
### Diff Algorithms
WatchDiff supports multiple diff algorithms, each optimized for different scenarios:
| **Myers** | General purpose | Fast, widely used, good balance |
| **Patience** | Code refactoring | Better handling of moved code blocks |
| **LCS** | Minimal changes | Produces smallest possible diffs |
### Export Functionality
When using `--export-dir`, WatchDiff automatically saves patches in multiple formats:
- **Unified diffs** (`.patch` files) - Standard diff format
- **Git patches** (`.git.patch` files) - Git-compatible format
- **Multifile patches** - Combined patches for multiple files
- **Patch bundles** - Organized directory structure with manifest
## Examples
### Development Workflow
```bash
# Watch Rust project files only
watchdiff-tui --extensions rs,toml,md
# Watch with additional ignores
watchdiff-tui --ignore "*.log,tmp/*"
# JSON output piped to file
watchdiff-tui --output json > changes.log
```
### CI/CD Integration
```bash
# Compact format for build scripts
watchdiff-tui --output compact --no-color
# Export patches for review process
watchdiff-tui --algorithm patience --export-dir ./review-patches
```
## Programmatic Usage
WatchDiff can also be used as a Rust library with a powerful API:
```rust
use watchdiff_tui::{
diff::{DiffGenerator, DiffAlgorithmType, DiffFormatter},
export::DiffExporter,
};
// Generate diffs with different algorithms
let generator = DiffGenerator::new(DiffAlgorithmType::Patience);
let result = generator.generate(old_content, new_content);
// Format as unified diff
let formatted = DiffFormatter::format_unified(&result, "old.rs", "new.rs");
// Export to file
let exporter = DiffExporter::unified();
exporter.export_diff(&result, old_path, new_path, "changes.patch")?;
// Get statistics
println!("Changes: {} additions, {} deletions",
result.stats.lines_added,
result.stats.lines_removed
);
```
### Library Features
- **Trait-based architecture** for extensible diff algorithms
- **Multiple export formats** (unified, Git patch, side-by-side)
- **Rich diff statistics** and metadata
- **Professional patch management** capabilities
- **Comprehensive test coverage**
See `examples/advanced_usage.rs` for complete usage examples.
## Architecture
WatchDiff is built with modern Rust practices and clean architecture:
### Module Organization
```
src/
โโโ core/ # Core file watching and event handling
โ โโโ events.rs # Event definitions and processing
โ โโโ filter.rs # File filtering with .gitignore support
โ โโโ watcher.rs # File system monitoring
โโโ diff/ # Modular diff generation system
โ โโโ algorithms.rs # Trait-based algorithm implementations
โ โโโ generator.rs # High-level diff generation
โ โโโ formatter.rs # Multiple output formats
โโโ export/ # Professional patch export capabilities
โโโ ui/ # Terminal user interface
โโโ highlight.rs # Syntax highlighting integration
```
### Key Dependencies
- **File Watching**: `notify` crate for cross-platform filesystem events
- **TUI**: `ratatui` with `crossterm` for beautiful terminal interfaces
- **Syntax Highlighting**: `syntect` crate for 25+ programming languages
- **CLI**: `clap` for robust argument parsing with derive macros
- **Diffing**: `similar` crate with multiple algorithm implementations
- **Filtering**: `ignore` crate for comprehensive `.gitignore` support
- **Async**: `tokio` for non-blocking operations
- **Date/Time**: `chrono` for export timestamps and metadata
### Design Principles
- **Trait-based Design**: Extensible algorithms via traits
- **Separation of Concerns**: Clear module boundaries
- **Type Safety**: Comprehensive type system usage
- **Performance**: Zero-cost abstractions and efficient algorithms
- **Testability**: Each module independently testable with comprehensive coverage
## Performance
- Handles thousands of files efficiently
- Memory-bounded event history (configurable)
- Native filesystem events (with polling fallback)
- Optimized diff generation
- Background processing threads
## License
MIT License - see LICENSE file for details.
## Contributing
Contributions welcome! Please open issues or pull requests.