|
| 1 | +# gitdiff |
| 2 | + |
| 3 | +Render ugly git diff output into something users actually want to look at in their phones. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +gitdiff is a native Swift library for rendering Git diffs in SwiftUI. It offers accurate, efficient diff visualization with the look and feel of tools like GitHub or GitLab, while leveraging the performance and customization benefits. |
| 8 | + |
| 9 | +<p align="center"> |
| 10 | + <img src="https://img.shields.io/badge/Swift-5.9+-orange.svg" /> |
| 11 | + <img src="https://img.shields.io/badge/iOS-15.0+-blue.svg" /> |
| 12 | + <img src="https://img.shields.io/badge/SwiftUI-Native-green.svg" /> |
| 13 | +</p> |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +### Swift Package Manager |
| 18 | + |
| 19 | +```swift |
| 20 | +dependencies: [ |
| 21 | + .package(url: "https://github.com/tornikegomareli/gitdiff.git", from: "0.0.1") |
| 22 | +] |
| 23 | +``` |
| 24 | + |
| 25 | +Or add through Xcode: **File → Add Package Dependencies** |
| 26 | + |
| 27 | +## Quick Start |
| 28 | + |
| 29 | +```swift |
| 30 | +import SwiftUI |
| 31 | +import gitdiff |
| 32 | + |
| 33 | +struct ContentView: View { |
| 34 | + let diffText = """ |
| 35 | + @@ -1,3 +1,3 @@ |
| 36 | + -let oldValue = "Hello" |
| 37 | + +let newValue = "World" |
| 38 | + let unchanged = true |
| 39 | + """ |
| 40 | + |
| 41 | + var body: some View { |
| 42 | + DiffRenderer(diffText: diffText) |
| 43 | + .diffTheme(.dark) |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## Themes |
| 49 | + |
| 50 | +gitdiff comes with three crafted themes: |
| 51 | + |
| 52 | +| Light (GitHub Style) | Dark | GitLab | |
| 53 | +|---------------------|------|--------| |
| 54 | +| Clean and familiar | Easy on the eyes | Simple and clean | |
| 55 | + |
| 56 | +### Using Built-in Themes |
| 57 | + |
| 58 | +```swift |
| 59 | +DiffRenderer(diffText: diffContent) |
| 60 | + .diffTheme(.light) // GitHub-style |
| 61 | + .diffTheme(.dark) // Modern dark theme |
| 62 | + .diffTheme(.gitlab) // GitLab's style |
| 63 | +``` |
| 64 | + |
| 65 | +### Creating Custom Themes |
| 66 | + |
| 67 | +```swift |
| 68 | +let customTheme = DiffTheme( |
| 69 | + addedBackground: Color.green.opacity(0.2), |
| 70 | + addedText: Color.green, |
| 71 | + removedBackground: Color.red.opacity(0.2), |
| 72 | + removedText: Color.red, |
| 73 | + contextBackground: Color(UIColor.systemBackground), |
| 74 | + contextText: Color.primary, |
| 75 | + lineNumberBackground: Color.gray.opacity(0.1), |
| 76 | + lineNumberText: Color.secondary, |
| 77 | + headerBackground: Color.blue.opacity(0.1), |
| 78 | + headerText: Color.blue, |
| 79 | + fileHeaderBackground: Color.gray.opacity(0.05), |
| 80 | + fileHeaderText: Color.primary |
| 81 | +) |
| 82 | + |
| 83 | +DiffRenderer(diffText: diffContent) |
| 84 | + .diffTheme(customTheme) |
| 85 | +``` |
| 86 | + |
| 87 | +## Configuration |
| 88 | + |
| 89 | +### View Modifiers |
| 90 | + |
| 91 | +Chain modifiers for quick configuration: |
| 92 | + |
| 93 | +```swift |
| 94 | +DiffRenderer(diffText: diffContent) |
| 95 | + .diffTheme(.dark) |
| 96 | + .diffLineNumbers(true) |
| 97 | + .diffFont(size: 14, weight: .medium) |
| 98 | + .diffLineSpacing(.comfortable) |
| 99 | + .diffWordWrap(true) |
| 100 | +``` |
| 101 | + |
| 102 | +### Configuration Object |
| 103 | + |
| 104 | +For reusable configurations: |
| 105 | + |
| 106 | +```swift |
| 107 | +let codeReviewConfig = DiffConfiguration( |
| 108 | + theme: .light, |
| 109 | + showLineNumbers: true, |
| 110 | + fontSize: 13, |
| 111 | + fontWeight: .regular, |
| 112 | + lineSpacing: .comfortable, |
| 113 | + wordWrap: false |
| 114 | +) |
| 115 | + |
| 116 | +DiffRenderer(diffText: diffContent) |
| 117 | + .environment(\.diffConfiguration, codeReviewConfig) |
| 118 | +``` |
| 119 | + |
| 120 | +### Preset Configurations |
| 121 | + |
| 122 | +Ready-to-use configurations for common scenarios: |
| 123 | + |
| 124 | +```swift |
| 125 | +// For code reviews - compact and efficient |
| 126 | +.environment(\.diffConfiguration, .codeReview) |
| 127 | + |
| 128 | +// For presentations - large and readable |
| 129 | +.environment(\.diffConfiguration, .presentation) |
| 130 | +``` |
| 131 | + |
| 132 | +## Advanced Usage |
| 133 | + |
| 134 | +### Working with the Parser |
| 135 | + |
| 136 | +For custom rendering needs: |
| 137 | + |
| 138 | +```swift |
| 139 | +let parser = DiffParser() |
| 140 | +let files = parser.parse(diffText) |
| 141 | + |
| 142 | +ForEach(files) { file in |
| 143 | + VStack(alignment: .leading) { |
| 144 | + Text(file.displayName) |
| 145 | + .font(.headline) |
| 146 | + |
| 147 | + ForEach(file.hunks) { hunk in |
| 148 | + Text(hunk.header) |
| 149 | + .font(.caption) |
| 150 | + .foregroundColor(.secondary) |
| 151 | + |
| 152 | + ForEach(hunk.lines) { line in |
| 153 | + // Custom line rendering |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +### Integration Examples |
| 161 | + |
| 162 | +**With Git Commands:** |
| 163 | +```swift |
| 164 | +let gitOutput = shell("git diff HEAD~1") |
| 165 | +DiffRenderer(diffText: gitOutput) |
| 166 | +``` |
| 167 | + |
| 168 | +**In a Code Review App:** |
| 169 | +```swift |
| 170 | +struct PullRequestView: View { |
| 171 | + let pullRequest: PullRequest |
| 172 | + @State private var showLineNumbers = true |
| 173 | + |
| 174 | + var body: some View { |
| 175 | + ScrollView { |
| 176 | + DiffRenderer(diffText: pullRequest.diff) |
| 177 | + .diffTheme(.light) |
| 178 | + .diffLineNumbers(showLineNumbers) |
| 179 | + } |
| 180 | + .toolbar { |
| 181 | + Toggle("Line Numbers", isOn: $showLineNumbers) |
| 182 | + } |
| 183 | + } |
| 184 | +} |
| 185 | +``` |
| 186 | +### View Modifiers |
| 187 | + |
| 188 | +- `.diffTheme(_ theme: DiffTheme)` - Apply a color theme |
| 189 | +- `.diffLineNumbers(_ show: Bool)` - Toggle line numbers |
| 190 | +- `.diffFont(size: CGFloat?, weight: Font.Weight?, design: Font.Design?)` - Configure font |
| 191 | +- `.diffLineSpacing(_ spacing: LineSpacing)` - Set line spacing |
| 192 | +- `.diffWordWrap(_ wrap: Bool)` - Enable word wrapping |
| 193 | +- `.diffConfiguration(_ config: DiffConfiguration)` - Apply complete configuration |
| 194 | + |
| 195 | +## Example App |
| 196 | + |
| 197 | +Explore all features with the included example app: |
| 198 | + |
| 199 | +1. Open `GitDiffExample/GitDiffExample.xcodeproj` |
| 200 | +2. Run the app to see: |
| 201 | + - Live theme switching |
| 202 | + - Interactive customization |
| 203 | + - Various diff examples |
| 204 | + - Code snippets |
| 205 | + |
| 206 | +## Performance |
| 207 | + |
| 208 | +- Efficient parsing of large diffs |
| 209 | +- Smooth scrolling performance |
| 210 | +- Minimal memory footprint |
| 211 | +- No external dependencies (except Docc) |
| 212 | + |
| 213 | +## Requirements |
| 214 | + |
| 215 | +- iOS 15.0+ |
| 216 | +- Swift 5.9+ |
| 217 | +- Xcode 15.0+ |
| 218 | + |
| 219 | +## Contributing |
| 220 | + |
| 221 | +Any ideas or improvements? Create pull requests. |
| 222 | + |
| 223 | +## License |
| 224 | + |
| 225 | +MIT License - see [LICENSE](LICENSE) for details. |
0 commit comments