diff.mdx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ---
  2. title: Diff
  3. description: Unified or split diff viewer
  4. order: 15
  5. ---
  6. # Diff
  7. Render unified or split diffs with syntax highlighting and optional line numbers.
  8. ## Basic usage
  9. ### Renderable API
  10. ```typescript
  11. import { DiffRenderable, SyntaxStyle, RGBA, createCliRenderer } from "@opentui/core"
  12. const renderer = await createCliRenderer()
  13. const syntaxStyle = SyntaxStyle.fromStyles({
  14. default: { fg: RGBA.fromHex("#E6EDF3") },
  15. string: { fg: RGBA.fromHex("#A5D6FF") },
  16. keyword: { fg: RGBA.fromHex("#FF7B72"), bold: true },
  17. })
  18. const diff = new DiffRenderable(renderer, {
  19. id: "diff",
  20. width: "100%",
  21. height: 16,
  22. diff: `diff --git a/app.ts b/app.ts\nindex 1111111..2222222 100644\n--- a/app.ts\n+++ b/app.ts\n@@ -1,3 +1,3 @@\n-const a = 1\n+const a = 2\n`,
  23. view: "split",
  24. filetype: "typescript",
  25. syntaxStyle,
  26. showLineNumbers: true,
  27. })
  28. renderer.root.add(diff)
  29. ```
  30. ## Construct API
  31. > Not available yet. Use `DiffRenderable` for now.
  32. ## Split view scroll sync
  33. When `view: "split"` is active, you can link the vertical scroll of both panes. Scrolling one pane then moves the other:
  34. ```typescript
  35. const diff = new DiffRenderable(renderer, {
  36. id: "diff",
  37. view: "split",
  38. syncScroll: true,
  39. diff: patch,
  40. syntaxStyle,
  41. })
  42. // Toggle at runtime
  43. diff.syncScroll = false
  44. ```
  45. Scroll sync is a no-op in the unified view.
  46. ## Properties
  47. | Property | Type | Default | Description |
  48. | --------------------- | ------------------------------- | ----------- | ------------------------------------- |
  49. | `diff` | `string` | `""` | Unified diff string |
  50. | `view` | `"unified"` or `"split"` | `"unified"` | Layout style |
  51. | `syncScroll` | `boolean` | `false` | Link vertical scroll in split view |
  52. | `filetype` | `string` | - | Syntax highlighting language |
  53. | `fg` | `string` or `RGBA` | - | Base foreground for inner code panes |
  54. | `syntaxStyle` | `SyntaxStyle` | - | Syntax style for code |
  55. | `wrapMode` | `"word"`, `"char"`, or `"none"` | - | Code wrapping mode |
  56. | `conceal` | `boolean` | `false` | Conceal markup when highlighting |
  57. | `selectionBg` | `string` or `RGBA` | - | Selection background in code panes |
  58. | `selectionFg` | `string` or `RGBA` | - | Selection foreground in code panes |
  59. | `treeSitterClient` | `TreeSitterClient` | - | Custom Tree-sitter client |
  60. | `showLineNumbers` | `boolean` | `true` | Show line numbers |
  61. | `lineNumberFg` | `string` or `RGBA` | `#888888` | Line number text color |
  62. | `lineNumberBg` | `string` or `RGBA` | transparent | Line number background |
  63. | `addedLineNumberBg` | `string` or `RGBA` | transparent | Line number background for added |
  64. | `removedLineNumberBg` | `string` or `RGBA` | transparent | Line number background for removed |
  65. | `addedBg` | `string` or `RGBA` | `#1a4d1a` | Background for added lines |
  66. | `removedBg` | `string` or `RGBA` | `#4d1a1a` | Background for removed lines |
  67. | `contextBg` | `string` or `RGBA` | transparent | Background for context lines |
  68. | `addedContentBg` | `string` or `RGBA` | - | Optional content background (added) |
  69. | `removedContentBg` | `string` or `RGBA` | - | Optional content background (removed) |
  70. | `contextContentBg` | `string` or `RGBA` | - | Optional content background (context) |
  71. | `addedSignColor` | `string` or `RGBA` | `#22c55e` | Sign color for added lines |
  72. | `removedSignColor` | `string` or `RGBA` | `#ef4444` | Sign color for removed lines |