|
1 | | -## A Cli DiffView Component like GitHub, Easy to use and feature complete. |
| 1 | +# @git-diff-view/cli |
2 | 2 |
|
3 | | -### Usage |
| 3 | +> Terminal-based diff viewer with GitHub-style UI for CLI environments |
4 | 4 |
|
5 | | -#### There are two ways to use this component: |
| 5 | +[](https://www.npmjs.com/package/@git-diff-view/cli) |
| 6 | +[](https://www.npmjs.com/package/@git-diff-view/cli) |
6 | 7 |
|
7 | | -1. Use the `DiffView` component directly. |
| 8 | +## Features |
8 | 9 |
|
9 | | -```tsx |
10 | | -import { DiffView, DiffModeEnum } from "@git-diff-view/cli"; |
| 10 | +- ✅ Terminal-friendly diff rendering |
| 11 | +- ✅ Split & Unified views in terminal |
| 12 | +- ✅ Syntax highlighting in CLI |
| 13 | +- ✅ Light & Dark themes |
| 14 | +- ✅ Configurable width and display options |
| 15 | +- ✅ Works with @git-diff-view/core and @git-diff-view/file |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +```bash |
| 20 | +npm install @git-diff-view/cli |
| 21 | +# or |
| 22 | +pnpm add @git-diff-view/cli |
| 23 | +# or |
| 24 | +yarn add @git-diff-view/cli |
| 25 | +``` |
11 | 26 |
|
12 | | -<DiffView<string> |
13 | | - // use data |
14 | | - data={{ |
15 | | - oldFile?: { fileName?: string | null; fileLang?: string | null; content?: string | null }; |
16 | | - newFile?: { fileName?: string | null; fileLang?: string | null; content?: string | null }; |
17 | | - hunks: string[]; |
18 | | - }} |
19 | | - width={number} |
20 | | - extendData={{oldFile: {10: {data: 'foo'}}, newFile: {20: {data: 'bar'}}}} |
21 | | - renderExtendLine={({ data }) => ReactNode} |
22 | | - diffViewHighlight={boolean} |
23 | | - diffViewTabSpace={boolean} |
24 | | - diffViewTabWidth={"small" | "medium" | "large"} |
25 | | - diffViewMode={DiffModeEnum.Split | DiffModeEnum.Unified} |
26 | | - diffViewTheme={'light' | 'dark'} |
27 | | -/> |
| 27 | +## Quick Start |
28 | 28 |
|
| 29 | +### Basic Usage |
| 30 | + |
| 31 | +```typescript |
| 32 | +import { DiffView, DiffModeEnum } from "@git-diff-view/cli"; |
| 33 | + |
| 34 | +DiffView({ |
| 35 | + data: { |
| 36 | + oldFile: { fileName: "old.ts", content: "...", fileLang: "typescript" }, |
| 37 | + newFile: { fileName: "new.ts", content: "...", fileLang: "typescript" }, |
| 38 | + hunks: ["..."] |
| 39 | + }, |
| 40 | + diffViewMode: DiffModeEnum.Split, |
| 41 | + diffViewTheme: "dark", |
| 42 | + diffViewHighlight: true, |
| 43 | + width: 120 // Terminal width |
| 44 | +}); |
29 | 45 | ``` |
30 | 46 |
|
31 | | -2. Use the `DiffView` component with `@git-diff-view/core` or `@git-diff-view/file` |
| 47 | +### With DiffFile |
| 48 | + |
| 49 | +**File Comparison Mode** |
| 50 | + |
| 51 | +```typescript |
| 52 | +import { DiffView } from "@git-diff-view/cli"; |
| 53 | +import { generateDiffFile } from "@git-diff-view/file"; |
32 | 54 |
|
33 | | -```tsx |
34 | | -// with @git-diff-view/file |
35 | | -import { DiffFile, generateDiffFile } from "@git-diff-view/file"; |
36 | 55 | const file = generateDiffFile( |
37 | | - data?.oldFile?.fileName || "", |
38 | | - data?.oldFile?.content || "", |
39 | | - data?.newFile?.fileName || "", |
40 | | - data?.newFile?.content || "", |
41 | | - data?.oldFile?.fileLang || "", |
42 | | - data?.newFile?.fileLang || "" |
| 56 | + "old.ts", oldContent, |
| 57 | + "new.ts", newContent, |
| 58 | + "typescript", "typescript" |
43 | 59 | ); |
44 | 60 | file.init(); |
45 | 61 | file.buildSplitDiffLines(); |
46 | | -file.buildUnifiedDiffLines(); |
47 | 62 |
|
48 | | -// with @git-diff-view/core |
| 63 | +DiffView({ diffFile: file, width: 120 }); |
| 64 | +``` |
| 65 | + |
| 66 | +**Git Diff Mode** |
| 67 | + |
| 68 | +```typescript |
| 69 | +import { DiffView } from "@git-diff-view/cli"; |
49 | 70 | import { DiffFile } from "@git-diff-view/core"; |
| 71 | + |
50 | 72 | const file = new DiffFile( |
51 | | - data?.oldFile?.fileName || "", |
52 | | - data?.oldFile?.content || "", |
53 | | - data?.newFile?.fileName || "", |
54 | | - data?.newFile?.content || "", |
55 | | - data?.hunks || [], |
56 | | - data?.oldFile?.fileLang || "", |
57 | | - data?.newFile?.fileLang || "" |
| 73 | + oldFileName, oldContent, |
| 74 | + newFileName, newContent, |
| 75 | + hunks, |
| 76 | + oldFileLang, newFileLang |
58 | 77 | ); |
59 | 78 | file.init(); |
60 | 79 | file.buildSplitDiffLines(); |
61 | | -file.buildUnifiedDiffLines(); |
62 | | - |
63 | | -// use current data to render |
64 | | -<DiffView diffFile={file} {...props} />; |
65 | | -// or use the bundle data to render, eg: postMessage/httpRequest |
66 | | -const bundle = file.getBundle(); |
67 | | -const diffFile = DiffFile.createInstance(data || {}, bundle); |
68 | | -<DiffView diffFile={diffFile} {...props} />; |
69 | | -``` |
| 80 | + |
| 81 | +DiffView({ diffFile: file }); |
| 82 | +``` |
| 83 | + |
| 84 | +## API Reference |
| 85 | + |
| 86 | +### DiffView Options |
| 87 | + |
| 88 | +| Option | Type | Description | |
| 89 | +|--------|------|-------------| |
| 90 | +| `data` | `DiffData` | Diff data with `oldFile`, `newFile`, and `hunks` | |
| 91 | +| `diffFile` | `DiffFile` | Pre-processed diff file instance | |
| 92 | +| `diffViewMode` | `Split \| Unified` | View mode (default: `Split`) | |
| 93 | +| `diffViewTheme` | `light \| dark` | Theme (default: `light`) | |
| 94 | +| `diffViewHighlight` | `boolean` | Enable syntax highlighting | |
| 95 | +| `diffViewTabSpace` | `boolean` | Show tab characters | |
| 96 | +| `diffViewTabWidth` | `small \| medium \| large` | Tab display width | |
| 97 | +| `width` | `number` | Terminal width for rendering | |
| 98 | +| `extendData` | `ExtendData` | Additional data per line | |
| 99 | +| `renderExtendLine` | `Function` | Custom extend line renderer | |
| 100 | + |
| 101 | +### DiffData Type |
| 102 | + |
| 103 | +```typescript |
| 104 | +type DiffData = { |
| 105 | + oldFile?: { |
| 106 | + fileName?: string | null; |
| 107 | + fileLang?: string | null; |
| 108 | + content?: string | null; |
| 109 | + }; |
| 110 | + newFile?: { |
| 111 | + fileName?: string | null; |
| 112 | + fileLang?: string | null; |
| 113 | + content?: string | null; |
| 114 | + }; |
| 115 | + hunks: string[]; |
| 116 | +}; |
| 117 | +``` |
| 118 | + |
| 119 | +## Use Cases |
| 120 | + |
| 121 | +- CLI diff tools |
| 122 | +- Terminal-based code review |
| 123 | +- Git hooks with visual diff |
| 124 | +- CI/CD pipeline diff display |
| 125 | +- SSH remote diff viewing |
| 126 | +- Terminal-based editors |
| 127 | + |
| 128 | +## Examples |
| 129 | + |
| 130 | +```typescript |
| 131 | +// Simple diff with custom width |
| 132 | +DiffView({ |
| 133 | + data: diffData, |
| 134 | + width: 160, |
| 135 | + diffViewHighlight: true |
| 136 | +}); |
| 137 | + |
| 138 | +// Unified view in dark theme |
| 139 | +DiffView({ |
| 140 | + diffFile: file, |
| 141 | + diffViewMode: DiffModeEnum.Unified, |
| 142 | + diffViewTheme: "dark" |
| 143 | +}); |
| 144 | + |
| 145 | +// With tab configuration |
| 146 | +DiffView({ |
| 147 | + data: diffData, |
| 148 | + diffViewTabSpace: true, |
| 149 | + diffViewTabWidth: "medium" |
| 150 | +}); |
| 151 | +``` |
| 152 | + |
| 153 | +## Related Packages |
| 154 | + |
| 155 | +- [@git-diff-view/core](https://www.npmjs.com/package/@git-diff-view/core) - Core diff engine |
| 156 | +- [@git-diff-view/file](https://www.npmjs.com/package/@git-diff-view/file) - File comparison |
| 157 | +- [@git-diff-view/react](https://www.npmjs.com/package/@git-diff-view/react) - React UI components |
| 158 | +- [@git-diff-view/vue](https://www.npmjs.com/package/@git-diff-view/vue) - Vue UI components |
| 159 | + |
| 160 | +## License |
| 161 | + |
| 162 | +MIT © [MrWangJustToDo](https://github.com/MrWangJustToDo) |
0 commit comments