Skip to content

Commit 7d86dec

Browse files
Merge pull request #7 from paololeonardi/showfileheaders
Make showFileHeaders flag functional with diffFileHeaders(_:) modifier
2 parents 2770ba5 + de02bc7 commit 7d86dec

6 files changed

Lines changed: 73 additions & 20 deletions

File tree

GitDiffExample/GitDiffExample/CustomizationPlayground.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import gitdiff
1010

1111
struct CustomizationPlayground: View {
1212
@State private var showLineNumbers = true
13+
@State private var showFileHeaders = true
1314
@State private var fontSize: CGFloat = 13
1415
@State private var lineSpacing: DiffConfiguration.LineSpacing = .compact
1516
@State private var wordWrap = true
@@ -29,6 +30,7 @@ struct CustomizationPlayground: View {
2930
DiffRenderer(diffText: SampleDiffs.configPlaygroundDiff)
3031
.diffTheme(selectedTheme)
3132
.diffLineNumbers(showLineNumbers)
33+
.diffFileHeaders(showFileHeaders)
3234
.diffFont(size: fontSize, weight: fontWeight)
3335
.diffLineSpacing(lineSpacing)
3436
.diffWordWrap(wordWrap)
@@ -43,6 +45,7 @@ struct CustomizationPlayground: View {
4345
fontSize: fontSize,
4446
fontWeight: fontWeight,
4547
lineNumbers: showLineNumbers,
48+
fileHeaders: showFileHeaders,
4649
wordWrap: wordWrap,
4750
lineSpacing: lineSpacing
4851
)
@@ -61,6 +64,7 @@ struct CustomizationPlayground: View {
6164
.sheet(isPresented: $showCustomizationSheet) {
6265
CustomizationSheet(
6366
showLineNumbers: $showLineNumbers,
67+
showFileHeaders: $showFileHeaders,
6468
fontSize: $fontSize,
6569
lineSpacing: $lineSpacing,
6670
wordWrap: $wordWrap,
@@ -83,6 +87,7 @@ struct CustomizationPlayground: View {
8387

8488
struct CustomizationSheet: View {
8589
@Binding var showLineNumbers: Bool
90+
@Binding var showFileHeaders: Bool
8691
@Binding var fontSize: CGFloat
8792
@Binding var lineSpacing: DiffConfiguration.LineSpacing
8893
@Binding var wordWrap: Bool
@@ -147,7 +152,9 @@ struct CustomizationSheet: View {
147152
.font(.headline)
148153

149154
Toggle("Show Line Numbers", isOn: $showLineNumbers)
150-
155+
156+
Toggle("Show File Headers", isOn: $showFileHeaders)
157+
151158
Toggle("Word Wrap", isOn: $wordWrap)
152159

153160
VStack(alignment: .leading, spacing: 8) {
@@ -224,6 +231,7 @@ struct CustomizationSheet: View {
224231
withAnimation {
225232
selectedTheme = config.theme
226233
showLineNumbers = config.showLineNumbers
234+
showFileHeaders = config.showFileHeaders
227235
fontSize = config.fontSize
228236
fontWeight = config.fontWeight
229237
lineSpacing = config.lineSpacing
@@ -234,6 +242,7 @@ struct CustomizationSheet: View {
234242
private func resetToDefaults() {
235243
withAnimation {
236244
showLineNumbers = true
245+
showFileHeaders = true
237246
fontSize = 13
238247
lineSpacing = .compact
239248
wordWrap = true
@@ -271,6 +280,7 @@ struct ConfigurationSummary: View {
271280
let fontSize: CGFloat
272281
let fontWeight: Font.Weight
273282
let lineNumbers: Bool
283+
let fileHeaders: Bool
274284
let wordWrap: Bool
275285
let lineSpacing: DiffConfiguration.LineSpacing
276286

@@ -284,6 +294,7 @@ struct ConfigurationSummary: View {
284294
ConfigItem(label: "Font Size", value: "\(Int(fontSize))pt")
285295
ConfigItem(label: "Font Weight", value: fontWeightName)
286296
ConfigItem(label: "Line Numbers", value: lineNumbers ? "On" : "Off")
297+
ConfigItem(label: "File Headers", value: fileHeaders ? "On" : "Off")
287298
ConfigItem(label: "Word Wrap", value: wordWrap ? "On" : "Off")
288299
ConfigItem(label: "Line Spacing", value: lineSpacingName)
289300
}

GitDiffExample/GitDiffExample/ExamplesView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ struct ExampleDetailView: View {
210210
@Environment(\.dismiss) var dismiss
211211
@State private var selectedTheme: DiffTheme = .light
212212
@State private var showLineNumbers = true
213+
@State private var showFileHeaders = true
213214

214215
var body: some View {
215216
NavigationView {
@@ -240,6 +241,7 @@ struct ExampleDetailView: View {
240241
// Controls
241242
HStack {
242243
Toggle("Line Numbers", isOn: $showLineNumbers)
244+
Toggle("File Headers", isOn: $showFileHeaders)
243245
Spacer()
244246
Button(action: shareExample) {
245247
Image(systemName: "square.and.arrow.up")
@@ -253,6 +255,7 @@ struct ExampleDetailView: View {
253255
DiffRenderer(diffText: example.diffContent)
254256
.diffTheme(selectedTheme)
255257
.diffLineNumbers(showLineNumbers)
258+
.diffFileHeaders(showFileHeaders)
256259
.padding()
257260
}
258261
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ For reusable configurations:
121121
let codeReviewConfig = DiffConfiguration(
122122
theme: .light,
123123
showLineNumbers: true,
124+
showFileHeaders: true,
124125
fontSize: 13,
125126
fontWeight: .regular,
126127
lineSpacing: .comfortable,
@@ -201,6 +202,7 @@ struct PullRequestView: View {
201202

202203
- `.diffTheme(_ theme: DiffTheme)` - Apply a color theme
203204
- `.diffLineNumbers(_ show: Bool)` - Toggle line numbers
205+
- `.diffFileHeaders(_ show: Bool)` - Toggle file headers
204206
- `.diffFont(size: CGFloat?, weight: Font.Weight?, design: Font.Design?)` - Configure font
205207
- `.diffLineSpacing(_ spacing: LineSpacing)` - Set line spacing
206208
- `.diffWordWrap(_ wrap: Bool)` - Enable word wrapping

Sources/gitdiff/DiffConfigurationBuilder.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ public extension DiffConfiguration {
6666
)
6767
}
6868

69+
/// Creates a new configuration with file headers toggled
70+
func withFileHeaders(_ show: Bool) -> DiffConfiguration {
71+
DiffConfiguration(
72+
theme: theme,
73+
showLineNumbers: showLineNumbers,
74+
showFileHeaders: show,
75+
fontFamily: fontFamily,
76+
fontSize: fontSize,
77+
fontWeight: fontWeight,
78+
lineHeight: lineHeight,
79+
lineSpacing: lineSpacing,
80+
wordWrap: wordWrap,
81+
contentPadding: contentPadding
82+
)
83+
}
84+
6985
/// Creates a new configuration with word wrap toggled
7086
func withWordWrap(_ wrap: Bool) -> DiffConfiguration {
7187
DiffConfiguration(

Sources/gitdiff/DiffEnvironment.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,26 @@ public extension View {
6060
)
6161
}
6262
}
63-
63+
64+
/// Shows or hides file headers.
65+
/// - Parameter show: Whether to show file headers
66+
func diffFileHeaders(_ show: Bool) -> some View {
67+
transformEnvironment(\.diffConfiguration) { config in
68+
config = DiffConfiguration(
69+
theme: config.theme,
70+
showLineNumbers: config.showLineNumbers,
71+
showFileHeaders: show,
72+
fontFamily: config.fontFamily,
73+
fontSize: config.fontSize,
74+
fontWeight: config.fontWeight,
75+
lineHeight: config.lineHeight,
76+
lineSpacing: config.lineSpacing,
77+
wordWrap: config.wordWrap,
78+
contentPadding: config.contentPadding
79+
)
80+
}
81+
}
82+
6483
/// Configures font properties.
6584
/// - Parameters:
6685
/// - size: Font size

Sources/gitdiff/Views/DiffFileView.swift

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,31 @@ struct DiffFileView: View {
1515

1616
var body: some View {
1717
VStack(alignment: .leading, spacing: 0) {
18-
HStack {
19-
Image(systemName: "doc.text")
20-
.foregroundColor(configuration.theme.fileHeaderText)
18+
if configuration.showFileHeaders {
19+
HStack {
20+
Image(systemName: "doc.text")
21+
.foregroundColor(configuration.theme.fileHeaderText)
2122

22-
Text(file.displayName)
23-
.font(.system(.headline, design: configuration.fontFamily))
24-
.fontWeight(.bold)
25-
.foregroundColor(configuration.theme.fileHeaderText)
23+
Text(file.displayName)
24+
.font(.system(.headline, design: configuration.fontFamily))
25+
.fontWeight(.bold)
26+
.foregroundColor(configuration.theme.fileHeaderText)
2627

27-
Spacer()
28+
Spacer()
2829

29-
if file.isBinary {
30-
Text("Binary file")
31-
.font(.caption)
32-
.foregroundColor(.secondary)
33-
.padding(.horizontal, 8)
34-
.padding(.vertical, 2)
35-
.background(Color.secondary.opacity(0.2))
36-
.cornerRadius(4)
30+
if file.isBinary {
31+
Text("Binary file")
32+
.font(.caption)
33+
.foregroundColor(.secondary)
34+
.padding(.horizontal, 8)
35+
.padding(.vertical, 2)
36+
.background(Color.secondary.opacity(0.2))
37+
.cornerRadius(4)
38+
}
3739
}
40+
.padding()
41+
.background(configuration.theme.fileHeaderBackground)
3842
}
39-
.padding()
40-
.background(configuration.theme.fileHeaderBackground)
4143

4244
if file.isBinary {
4345
Text("Binary file not shown")

0 commit comments

Comments
 (0)