Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion GitDiffExample/GitDiffExample/CustomizationPlayground.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import gitdiff

struct CustomizationPlayground: View {
@State private var showLineNumbers = true
@State private var showFileHeaders = true
@State private var fontSize: CGFloat = 13
@State private var lineSpacing: DiffConfiguration.LineSpacing = .compact
@State private var wordWrap = true
Expand All @@ -29,6 +30,7 @@ struct CustomizationPlayground: View {
DiffRenderer(diffText: SampleDiffs.configPlaygroundDiff)
.diffTheme(selectedTheme)
.diffLineNumbers(showLineNumbers)
.diffFileHeaders(showFileHeaders)
.diffFont(size: fontSize, weight: fontWeight)
.diffLineSpacing(lineSpacing)
.diffWordWrap(wordWrap)
Expand All @@ -43,6 +45,7 @@ struct CustomizationPlayground: View {
fontSize: fontSize,
fontWeight: fontWeight,
lineNumbers: showLineNumbers,
fileHeaders: showFileHeaders,
wordWrap: wordWrap,
lineSpacing: lineSpacing
)
Expand All @@ -61,6 +64,7 @@ struct CustomizationPlayground: View {
.sheet(isPresented: $showCustomizationSheet) {
CustomizationSheet(
showLineNumbers: $showLineNumbers,
showFileHeaders: $showFileHeaders,
fontSize: $fontSize,
lineSpacing: $lineSpacing,
wordWrap: $wordWrap,
Expand All @@ -83,6 +87,7 @@ struct CustomizationPlayground: View {

struct CustomizationSheet: View {
@Binding var showLineNumbers: Bool
@Binding var showFileHeaders: Bool
@Binding var fontSize: CGFloat
@Binding var lineSpacing: DiffConfiguration.LineSpacing
@Binding var wordWrap: Bool
Expand Down Expand Up @@ -147,7 +152,9 @@ struct CustomizationSheet: View {
.font(.headline)

Toggle("Show Line Numbers", isOn: $showLineNumbers)


Toggle("Show File Headers", isOn: $showFileHeaders)

Toggle("Word Wrap", isOn: $wordWrap)

VStack(alignment: .leading, spacing: 8) {
Expand Down Expand Up @@ -224,6 +231,7 @@ struct CustomizationSheet: View {
withAnimation {
selectedTheme = config.theme
showLineNumbers = config.showLineNumbers
showFileHeaders = config.showFileHeaders
fontSize = config.fontSize
fontWeight = config.fontWeight
lineSpacing = config.lineSpacing
Expand All @@ -234,6 +242,7 @@ struct CustomizationSheet: View {
private func resetToDefaults() {
withAnimation {
showLineNumbers = true
showFileHeaders = true
fontSize = 13
lineSpacing = .compact
wordWrap = true
Expand Down Expand Up @@ -271,6 +280,7 @@ struct ConfigurationSummary: View {
let fontSize: CGFloat
let fontWeight: Font.Weight
let lineNumbers: Bool
let fileHeaders: Bool
let wordWrap: Bool
let lineSpacing: DiffConfiguration.LineSpacing

Expand All @@ -284,6 +294,7 @@ struct ConfigurationSummary: View {
ConfigItem(label: "Font Size", value: "\(Int(fontSize))pt")
ConfigItem(label: "Font Weight", value: fontWeightName)
ConfigItem(label: "Line Numbers", value: lineNumbers ? "On" : "Off")
ConfigItem(label: "File Headers", value: fileHeaders ? "On" : "Off")
ConfigItem(label: "Word Wrap", value: wordWrap ? "On" : "Off")
ConfigItem(label: "Line Spacing", value: lineSpacingName)
}
Expand Down
3 changes: 3 additions & 0 deletions GitDiffExample/GitDiffExample/ExamplesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ struct ExampleDetailView: View {
@Environment(\.dismiss) var dismiss
@State private var selectedTheme: DiffTheme = .light
@State private var showLineNumbers = true
@State private var showFileHeaders = true

var body: some View {
NavigationView {
Expand Down Expand Up @@ -240,6 +241,7 @@ struct ExampleDetailView: View {
// Controls
HStack {
Toggle("Line Numbers", isOn: $showLineNumbers)
Toggle("File Headers", isOn: $showFileHeaders)
Spacer()
Button(action: shareExample) {
Image(systemName: "square.and.arrow.up")
Expand All @@ -253,6 +255,7 @@ struct ExampleDetailView: View {
DiffRenderer(diffText: example.diffContent)
.diffTheme(selectedTheme)
.diffLineNumbers(showLineNumbers)
.diffFileHeaders(showFileHeaders)
.padding()
}
}
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ For reusable configurations:
let codeReviewConfig = DiffConfiguration(
theme: .light,
showLineNumbers: true,
showFileHeaders: true,
fontSize: 13,
fontWeight: .regular,
lineSpacing: .comfortable,
Expand Down Expand Up @@ -201,6 +202,7 @@ struct PullRequestView: View {

- `.diffTheme(_ theme: DiffTheme)` - Apply a color theme
- `.diffLineNumbers(_ show: Bool)` - Toggle line numbers
- `.diffFileHeaders(_ show: Bool)` - Toggle file headers
- `.diffFont(size: CGFloat?, weight: Font.Weight?, design: Font.Design?)` - Configure font
- `.diffLineSpacing(_ spacing: LineSpacing)` - Set line spacing
- `.diffWordWrap(_ wrap: Bool)` - Enable word wrapping
Expand Down
16 changes: 16 additions & 0 deletions Sources/gitdiff/DiffConfigurationBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ public extension DiffConfiguration {
)
}

/// Creates a new configuration with file headers toggled
func withFileHeaders(_ show: Bool) -> DiffConfiguration {
DiffConfiguration(
theme: theme,
showLineNumbers: showLineNumbers,
showFileHeaders: show,
fontFamily: fontFamily,
fontSize: fontSize,
fontWeight: fontWeight,
lineHeight: lineHeight,
lineSpacing: lineSpacing,
wordWrap: wordWrap,
contentPadding: contentPadding
)
}

/// Creates a new configuration with word wrap toggled
func withWordWrap(_ wrap: Bool) -> DiffConfiguration {
DiffConfiguration(
Expand Down
21 changes: 20 additions & 1 deletion Sources/gitdiff/DiffEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,26 @@ public extension View {
)
}
}


/// Shows or hides file headers.
/// - Parameter show: Whether to show file headers
func diffFileHeaders(_ show: Bool) -> some View {
transformEnvironment(\.diffConfiguration) { config in
config = DiffConfiguration(
theme: config.theme,
showLineNumbers: config.showLineNumbers,
showFileHeaders: show,
fontFamily: config.fontFamily,
fontSize: config.fontSize,
fontWeight: config.fontWeight,
lineHeight: config.lineHeight,
lineSpacing: config.lineSpacing,
wordWrap: config.wordWrap,
contentPadding: config.contentPadding
)
}
}

/// Configures font properties.
/// - Parameters:
/// - size: Font size
Expand Down
38 changes: 20 additions & 18 deletions Sources/gitdiff/Views/DiffFileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,31 @@ struct DiffFileView: View {

var body: some View {
VStack(alignment: .leading, spacing: 0) {
HStack {
Image(systemName: "doc.text")
.foregroundColor(configuration.theme.fileHeaderText)
if configuration.showFileHeaders {
HStack {
Image(systemName: "doc.text")
.foregroundColor(configuration.theme.fileHeaderText)

Text(file.displayName)
.font(.system(.headline, design: configuration.fontFamily))
.fontWeight(.bold)
.foregroundColor(configuration.theme.fileHeaderText)
Text(file.displayName)
.font(.system(.headline, design: configuration.fontFamily))
.fontWeight(.bold)
.foregroundColor(configuration.theme.fileHeaderText)

Spacer()
Spacer()

if file.isBinary {
Text("Binary file")
.font(.caption)
.foregroundColor(.secondary)
.padding(.horizontal, 8)
.padding(.vertical, 2)
.background(Color.secondary.opacity(0.2))
.cornerRadius(4)
if file.isBinary {
Text("Binary file")
.font(.caption)
.foregroundColor(.secondary)
.padding(.horizontal, 8)
.padding(.vertical, 2)
.background(Color.secondary.opacity(0.2))
.cornerRadius(4)
}
}
.padding()
.background(configuration.theme.fileHeaderBackground)
}
.padding()
.background(configuration.theme.fileHeaderBackground)

if file.isBinary {
Text("Binary file not shown")
Expand Down