-
Notifications
You must be signed in to change notification settings - Fork 11
feat(opentui): publish reusable diff component #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1433292
feat(opentui): publish reusable diff component
benvinegar 8236d42
docs(opentui): add runnable diff component examples
benvinegar 3d4decc
feat(opentui): make diff example interactive
benvinegar de9194d
fix(opentui): narrow published package surface
benvinegar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # 7-opentui-component | ||
|
|
||
| Two minimal OpenTUI apps that embed `HunkDiffView` directly. | ||
|
|
||
| ## Run | ||
|
|
||
| ```bash | ||
| bun run examples/7-opentui-component/from-files.tsx | ||
| bun run examples/7-opentui-component/from-patch.tsx | ||
| ``` | ||
|
|
||
| ## What it shows | ||
|
|
||
| - embedding `HunkDiffView` inside a normal OpenTUI app shell | ||
| - building `diff.metadata` with `parseDiffFromFile` | ||
| - parsing raw unified diff text with `parsePatchFiles` | ||
| - switching between split and stacked layouts with example shell controls | ||
| - a scrollable terminal diff component that other OpenTUI apps can reuse | ||
|
|
||
| The in-repo demos import from `../../src/opentui` so they run from source. Published consumers should import from `hunkdiff/opentui` instead. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export interface ReviewSummary { | ||
| title: string; | ||
| confidence: number; | ||
| tags: string[]; | ||
| } | ||
|
|
||
| export function formatReviewSummary(summary: ReviewSummary) { | ||
| const tagSuffix = summary.tags.length > 0 ? ` [${summary.tags.join(", ")}]` : ""; | ||
| return `${summary.title} (${summary.confidence})${tagSuffix}`; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export interface ReviewSummary { | ||
| title: string; | ||
| confidence: number; | ||
| } | ||
|
|
||
| export function summarizeReview(summary: ReviewSummary) { | ||
| return `${summary.title} (${summary.confidence})`; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| diff --git a/src/reviewSummary.ts b/src/reviewSummary.ts | ||
| index 1111111..2222222 100644 | ||
| --- a/src/reviewSummary.ts | ||
| +++ b/src/reviewSummary.ts | ||
| @@ -1,8 +1,10 @@ | ||
| export interface ReviewSummary { | ||
| title: string; | ||
| confidence: number; | ||
| + tags: string[]; | ||
| } | ||
|
|
||
| -export function summarizeReview(summary: ReviewSummary) { | ||
| - return `${summary.title} (${summary.confidence})`; | ||
| +export function formatReviewSummary(summary: ReviewSummary) { | ||
| + const tagSuffix = summary.tags.length > 0 ? ` [${summary.tags.join(", ")}]` : ""; | ||
| + return `${summary.title} (${summary.confidence})${tagSuffix}`; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #!/usr/bin/env bun | ||
|
|
||
| import { parseDiffFromFile } from "../../src/opentui"; | ||
| import { readExampleFile, runExample } from "./support"; | ||
|
|
||
| const path = "src/reviewSummary.ts"; | ||
| const before = readExampleFile("before.ts"); | ||
| const after = readExampleFile("after.ts"); | ||
| const metadata = parseDiffFromFile( | ||
| { | ||
| cacheKey: "example:before", | ||
| contents: before, | ||
| name: path, | ||
| }, | ||
| { | ||
| cacheKey: "example:after", | ||
| contents: after, | ||
| name: path, | ||
| }, | ||
| { context: 3 }, | ||
| true, | ||
| ); | ||
|
|
||
| await runExample({ | ||
| title: "HunkDiffView from file contents", | ||
| subtitle: "Built with parseDiffFromFile. Press Ctrl-C to exit.", | ||
| diff: { | ||
| id: "example:files", | ||
| metadata, | ||
| language: "typescript", | ||
| path, | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #!/usr/bin/env bun | ||
|
|
||
| import { parsePatchFiles } from "../../src/opentui"; | ||
| import { readExampleFile, runExample } from "./support"; | ||
|
|
||
| const patch = readExampleFile("change.patch"); | ||
| const parsedPatches = parsePatchFiles(patch, "example:patch", true); | ||
| const metadata = parsedPatches.flatMap((entry) => entry.files)[0]; | ||
|
|
||
| if (!metadata) { | ||
| throw new Error("Expected one diff file in examples/7-opentui-component/change.patch."); | ||
| } | ||
|
|
||
| await runExample({ | ||
| title: "HunkDiffView from patch text", | ||
| subtitle: "Built with parsePatchFiles. Press Ctrl-C to exit.", | ||
| diff: { | ||
| id: "example:patch", | ||
| metadata, | ||
| language: "typescript", | ||
| path: metadata.name, | ||
| patch, | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { readFileSync } from "node:fs"; | ||
| import path from "node:path"; | ||
| import { createCliRenderer } from "@opentui/core"; | ||
| import { createRoot, useTerminalDimensions } from "@opentui/react"; | ||
| import { useState } from "react"; | ||
| import type { HunkDiffFile, HunkDiffLayout } from "../../src/opentui"; | ||
| import { HunkDiffView } from "../../src/opentui"; | ||
| import { fitText } from "../../src/ui/lib/text"; | ||
|
|
||
| interface ExampleProps { | ||
| title: string; | ||
| subtitle: string; | ||
| diff: HunkDiffFile; | ||
| layout?: HunkDiffLayout; | ||
| } | ||
|
|
||
| /** Read one checked-in example file relative to this folder. */ | ||
| export function readExampleFile(name: string) { | ||
| return readFileSync(path.join(import.meta.dir, name), "utf8"); | ||
| } | ||
|
|
||
| function LayoutButton({ | ||
| active, | ||
| label, | ||
| onPress, | ||
| }: { | ||
| active: boolean; | ||
| label: string; | ||
| onPress: () => void; | ||
| }) { | ||
| return ( | ||
| <box | ||
| style={{ | ||
| width: label.length + 2, | ||
| height: 1, | ||
| backgroundColor: active ? "#452650" : "#1f2430", | ||
| }} | ||
| onMouseUp={onPress} | ||
| > | ||
| <text fg={active ? "#fff0ff" : "#8f9bb3"}>{` ${label} `}</text> | ||
| </box> | ||
| ); | ||
| } | ||
|
|
||
| function ExampleApp({ title, subtitle, diff, layout = "split" }: ExampleProps) { | ||
| const [activeLayout, setActiveLayout] = useState(layout); | ||
| const terminal = useTerminalDimensions(); | ||
| const headerWidth = Math.max(1, terminal.width - 2); | ||
| const diffWidth = Math.max(24, terminal.width - 2); | ||
|
|
||
| return ( | ||
| <box | ||
| style={{ | ||
| width: "100%", | ||
| height: "100%", | ||
| flexDirection: "column", | ||
| paddingLeft: 1, | ||
| paddingRight: 1, | ||
| paddingTop: 1, | ||
| paddingBottom: 1, | ||
| }} | ||
| > | ||
| <box style={{ width: "100%", height: 1 }}> | ||
| <text fg="#d8b4fe">{fitText(title, headerWidth)}</text> | ||
| </box> | ||
| <box style={{ width: "100%", height: 1 }}> | ||
| <text fg="#8f9bb3">{fitText(subtitle, headerWidth)}</text> | ||
| </box> | ||
| <box style={{ width: "100%", height: 1, flexDirection: "row" }}> | ||
| <text fg="#6b7280">layout</text> | ||
| <box style={{ width: 1, height: 1 }} /> | ||
| <LayoutButton | ||
| active={activeLayout === "split"} | ||
| label="Split" | ||
| onPress={() => setActiveLayout("split")} | ||
| /> | ||
| <box style={{ width: 1, height: 1 }} /> | ||
| <LayoutButton | ||
| active={activeLayout === "stack"} | ||
| label="Stack" | ||
| onPress={() => setActiveLayout("stack")} | ||
| /> | ||
| </box> | ||
| <box style={{ height: 1 }} /> | ||
| <box style={{ flexGrow: 1 }}> | ||
| <HunkDiffView diff={diff} layout={activeLayout} width={diffWidth} theme="midnight" /> | ||
| </box> | ||
| </box> | ||
| ); | ||
| } | ||
|
|
||
| /** Launch a tiny OpenTUI app that embeds the exported Hunk diff component. */ | ||
| export async function runExample(props: ExampleProps) { | ||
| const renderer = await createCliRenderer({ | ||
| useAlternateScreen: true, | ||
| useMouse: true, | ||
| exitOnCtrlC: true, | ||
| openConsoleOnError: true, | ||
| }); | ||
| const root = createRoot(renderer); | ||
|
|
||
| root.render(<ExampleApp {...props} />); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.