|
| 1 | +# OpenTUI component |
| 2 | + |
| 3 | +`hunkdiff/opentui` exports `HunkDiffView`, a reusable terminal diff component built from the same renderer as the Hunk CLI. |
| 4 | + |
| 5 | +Use it when you want Hunk's split or stack diff view inside your own OpenTUI app. |
| 6 | + |
| 7 | +## Install |
| 8 | + |
| 9 | +```bash |
| 10 | +npm i hunkdiff @opentui/core @opentui/react react |
| 11 | +``` |
| 12 | + |
| 13 | +`hunkdiff` declares OpenTUI and React as peer dependencies, so install them in your app. |
| 14 | + |
| 15 | +## Quick start |
| 16 | + |
| 17 | +```tsx |
| 18 | +import { createCliRenderer } from "@opentui/core"; |
| 19 | +import { createRoot } from "@opentui/react"; |
| 20 | +import { HunkDiffView, parseDiffFromFile } from "hunkdiff/opentui"; |
| 21 | + |
| 22 | +const metadata = parseDiffFromFile( |
| 23 | + { |
| 24 | + cacheKey: "before", |
| 25 | + contents: "export const value = 1;\n", |
| 26 | + name: "example.ts", |
| 27 | + }, |
| 28 | + { |
| 29 | + cacheKey: "after", |
| 30 | + contents: "export const value = 2;\nexport const added = true;\n", |
| 31 | + name: "example.ts", |
| 32 | + }, |
| 33 | + { context: 3 }, |
| 34 | + true, |
| 35 | +); |
| 36 | + |
| 37 | +const renderer = await createCliRenderer({ |
| 38 | + useAlternateScreen: true, |
| 39 | + useMouse: true, |
| 40 | + exitOnCtrlC: true, |
| 41 | +}); |
| 42 | +const root = createRoot(renderer); |
| 43 | + |
| 44 | +root.render( |
| 45 | + <HunkDiffView |
| 46 | + diff={{ |
| 47 | + id: "example", |
| 48 | + metadata, |
| 49 | + language: "typescript", |
| 50 | + path: "example.ts", |
| 51 | + }} |
| 52 | + layout="split" |
| 53 | + width={88} |
| 54 | + theme="midnight" |
| 55 | + />, |
| 56 | +); |
| 57 | +``` |
| 58 | + |
| 59 | +In a real app, derive `width` from your layout or `useTerminalDimensions()`. |
| 60 | + |
| 61 | +## Building the `diff` input |
| 62 | + |
| 63 | +`HunkDiffView` renders one file at a time. Pass a `diff` object shaped like this: |
| 64 | + |
| 65 | +```ts |
| 66 | +type HunkDiffFile = { |
| 67 | + id: string; |
| 68 | + metadata: FileDiffMetadata; |
| 69 | + language?: string; |
| 70 | + path?: string; |
| 71 | + patch?: string; |
| 72 | +}; |
| 73 | +``` |
| 74 | + |
| 75 | +### From before/after contents |
| 76 | + |
| 77 | +Use `parseDiffFromFile(...)` when you already have the old and new file contents. |
| 78 | + |
| 79 | +```tsx |
| 80 | +import { parseDiffFromFile } from "hunkdiff/opentui"; |
| 81 | + |
| 82 | +const metadata = parseDiffFromFile(beforeFile, afterFile, { context: 3 }, true); |
| 83 | +``` |
| 84 | + |
| 85 | +### From unified diff text |
| 86 | + |
| 87 | +Use `parsePatchFiles(...)` when you already have a patch string. |
| 88 | + |
| 89 | +```tsx |
| 90 | +import { parsePatchFiles } from "hunkdiff/opentui"; |
| 91 | + |
| 92 | +const parsed = parsePatchFiles(patchText, "example:patch", true); |
| 93 | +const metadata = parsed.flatMap((entry) => entry.files)[0]; |
| 94 | + |
| 95 | +if (!metadata) { |
| 96 | + throw new Error("Expected at least one diff file."); |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Props |
| 101 | + |
| 102 | +| Prop | Type | Default | Notes | |
| 103 | +| ------------------- | ------------------------------------------------ | ------------ | ------------------------------------------------------------------------- | |
| 104 | +| `diff` | `HunkDiffFile` | `undefined` | File to render. When omitted, the component shows an empty-state message. | |
| 105 | +| `layout` | `"split" \| "stack"` | `"split"` | Chooses side-by-side or stacked rendering. | |
| 106 | +| `width` | `number` | — | Required content width in terminal columns. | |
| 107 | +| `theme` | `"graphite" \| "midnight" \| "paper" \| "ember"` | `"graphite"` | Matches Hunk's built-in themes. | |
| 108 | +| `showLineNumbers` | `boolean` | `true` | Toggles line-number columns. | |
| 109 | +| `showHunkHeaders` | `boolean` | `true` | Toggles `@@ ... @@` hunk header rows. | |
| 110 | +| `wrapLines` | `boolean` | `false` | Wraps long lines instead of clipping horizontally. | |
| 111 | +| `horizontalOffset` | `number` | `0` | Scroll offset for non-wrapped code rows. | |
| 112 | +| `highlight` | `boolean` | `true` | Enables syntax highlighting. | |
| 113 | +| `scrollable` | `boolean` | `true` | Set to `false` if your parent view owns scrolling. | |
| 114 | +| `selectedHunkIndex` | `number` | `0` | Highlights one hunk as the active target. | |
| 115 | + |
| 116 | +## Other exports |
| 117 | + |
| 118 | +- `parseDiffFromFile` |
| 119 | +- `parsePatchFiles` |
| 120 | +- `FileDiffMetadata` |
| 121 | +- `HUNK_DIFF_THEME_NAMES` |
| 122 | +- `HunkDiffThemeName` |
| 123 | +- `HunkDiffLayout` |
| 124 | +- `HunkDiffFile` |
| 125 | +- `HunkDiffViewProps` |
| 126 | + |
| 127 | +`parseDiffFromFile`, `parsePatchFiles`, and `FileDiffMetadata` are re-exported from `@pierre/diffs` so you can build `metadata` without adding a second diff dependency. |
| 128 | + |
| 129 | +## Examples |
| 130 | + |
| 131 | +- Runnable demo overview: [`examples/README.md`](../examples/README.md) |
| 132 | +- Component demos: [`examples/7-opentui-component/README.md`](../examples/7-opentui-component/README.md) |
| 133 | + |
| 134 | +The in-repo demos import from `../../src/opentui` so they run from source. Published consumers should import from `hunkdiff/opentui`. |
0 commit comments