Skip to content

A SwiftUI component for comparing and displaying text differences, similar to GitHub Desktop's diff view. 一个用于比较和显示文本差异的 SwiftUI 组件,类似于 GitHub Desktop 的差异视图。

License

Notifications You must be signed in to change notification settings

nookery/MagicDiffView

Repository files navigation

MagicDiffView

A powerful SwiftUI view component for comparing and displaying text differences with unified diff support.

📖 中文文档

Features

  • Text Diff Comparison: Compare two text strings and highlight differences
  • Unified Diff Parser: Parse unified diff format from Git and other tools
  • Syntax Highlighting: Support for multiple programming languages
  • Collapsible Blocks: Automatically collapse unchanged sections
  • Multiple View Modes: Unified diff, split view, original, and modified text
  • Line Numbers: Accurate line number mapping
  • Cross-platform: Supports macOS and iOS

Installation

Swift Package Manager

Add this package to your project via Xcode:

  1. In Xcode, go to File > Swift Packages > Add Package Dependency
  2. Enter the repository URL
  3. Select the main branch
  4. Click Add Package

Or add it to your Package.swift:

dependencies: [
    .package(
        url: "https://github.com/your-username/MagicDiffView.git",
        branch: "main"
    )
]

Quick Start

Basic Usage - Compare Two Texts

import SwiftUI
import MagicDiffView

struct ContentView: View {
    var body: some View {
        MagicDiffView(
            oldText: "Hello World\nThis is line 2",
            newText: "Hello Swift\nThis is line 2\nNew line 3"
        )
    }
}

Parse Git Diff

import MagicDiffView

struct GitDiffView: View {
    let gitDiffOutput: String = """
    diff --git a/file.txt b/file.txt
    @@ -1,3 +1,3 @@
     Line 1
    -Line 2 old
    +Line 2 new
     Line 3
    """

    var body: some View {
        let diffLines = MyersDiffAlgorithm.parseUnifiedDiffSafely(gitDiffOutput)
        return MagicDiffView(diffLines: diffLines)
    }
}

Requirements

  • iOS 17.0+ or macOS 14.0+
  • Swift 5.9+
  • Xcode 15.0+

License

MIT License - see LICENSE for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

About

A SwiftUI component for comparing and displaying text differences, similar to GitHub Desktop's diff view. 一个用于比较和显示文本差异的 SwiftUI 组件,类似于 GitHub Desktop 的差异视图。

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Contributors 2

  •  
  •