jrv-text-diff
Compare two texts, files, or code snippets and show exactly what changed. Supports unified diff, side-by-side view, word-level highlighting, and structural diff for JSON/YAML.
Quick Start
CODEBLOCK0
Commands
| Command | Description |
|---|
| INLINECODE0 | Compare two files (default: unified diff) |
| INLINECODE1 |
Compare two inline strings |
|
--format unified | Unified patch-style diff output |
|
--format side-by-side | Two-column comparison |
|
--format context | Context diff (like diff -c) |
|
--format json | Structural JSON diff |
|
--format yaml | Structural YAML diff |
|
--word-diff | Highlight word-level changes within lines |
|
--ignore-whitespace | Ignore leading/trailing whitespace |
|
--ignore-case | Case-insensitive comparison |
|
--context N | Lines of context (default: 3) |
|
--output-json | Output diff stats as JSON |
|
--no-color | Disable ANSI color output |
Use Cases
- - Code review: See exactly what changed between two versions of a config or script
- Config auditing: Spot differences between prod and staging configs
- JSON/YAML diffs: Structural comparison ignoring key ordering
- Copy editing: Word-level diff for prose documents
- CI pipelines: JSON output for programmatic diff analysis
Exit Codes
- -
0 — Files are identical - INLINECODE14 — Files differ
- INLINECODE15 — Error (file not found, parse error, etc.)
jrv-text-diff
比较两个文本、文件或代码片段,并精确显示更改内容。支持统一差异、并排视图、单词级高亮以及JSON/YAML的结构化差异。
快速开始
bash
比较两个文件
python3 scripts/text_diff.py file1.txt file2.txt
比较内联字符串
python3 scripts/text_diff.py --text hello world --text2 hello there
统一差异(补丁格式)
python3 scripts/text_diff.py file1.txt file2.txt --format unified
并排比较
python3 scripts/text_diff.py file1.txt file2.txt --format side-by-side
单词级差异
python3 scripts/text_diff.py file1.txt file2.txt --word-diff
忽略空白字符
python3 scripts/text_diff.py file1.txt file2.txt --ignore-whitespace
JSON结构化差异
python3 scripts/text_diff.py data1.json data2.json --format json
YAML结构化差异
python3 scripts/text_diff.py config1.yaml config2.yaml --format yaml
输出差异为JSON报告
python3 scripts/text_diff.py file1.txt file2.txt --output-json
命令
| 命令 | 描述 |
|---|
| text_diff.py <file1> <file2> | 比较两个文件(默认:统一差异) |
| --text <str> --text2 <str> |
比较两个内联字符串 |
| --format unified | 统一补丁样式差异输出 |
| --format side-by-side | 双列比较 |
| --format context | 上下文差异(类似 diff -c) |
| --format json | 结构化JSON差异 |
| --format yaml | 结构化YAML差异 |
| --word-diff | 高亮行内单词级更改 |
| --ignore-whitespace | 忽略前导/尾随空白字符 |
| --ignore-case | 不区分大小写比较 |
| --context N | 上下文行数(默认:3) |
| --output-json | 将差异统计输出为JSON |
| --no-color | 禁用ANSI颜色输出 |
使用场景
- - 代码审查:精确查看配置或脚本两个版本之间的更改内容
- 配置审计:发现生产环境与预发布环境配置之间的差异
- JSON/YAML差异:忽略键排序的结构化比较
- 文案编辑:散文文档的单词级差异
- CI流水线:用于程序化差异分析的JSON输出
退出码
- - 0 — 文件相同
- 1 — 文件不同
- 2 — 错误(文件未找到、解析错误等)