Skip to content

Commit 7bcaa4b

Browse files
committed
Add an option to configure what parts of the URL to display
1 parent 54cf739 commit 7bcaa4b

File tree

4 files changed

+85
-14
lines changed

4 files changed

+85
-14
lines changed

Sources/PulseUI/Extensions/Pulse+Extensions.swift

+39
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,42 @@ extension NetworkTaskEntity {
131131
}
132132

133133
#endif
134+
135+
extension NetworkTaskEntity {
136+
func getFormattedContent(options: DisplayOptions) -> String? {
137+
if options.showTaskDescription, let taskDescription, !taskDescription.isEmpty {
138+
return taskDescription
139+
}
140+
guard let url else {
141+
return nil
142+
}
143+
let displayed = options.contentComponents
144+
if displayed.count == UserSettings.DisplayOptions.ContentComponent.allCases.count {
145+
return url
146+
}
147+
guard var components = URLComponents(string: url) else {
148+
return nil
149+
}
150+
if displayed.count == 1 && displayed.first == .path {
151+
return components.path // optimization
152+
}
153+
if !(components.password ?? "").isEmpty {
154+
components.password = "_"
155+
}
156+
if !displayed.contains(.scheme) { components.scheme = nil }
157+
if !displayed.contains(.user) { components.user = nil }
158+
if !displayed.contains(.password) { components.password = nil }
159+
if !displayed.contains(.host) { components.host = nil }
160+
if !displayed.contains(.port) { components.port = nil }
161+
if !displayed.contains(.path) { /* can't remove path */ }
162+
if !displayed.contains(.query) { components.query = nil }
163+
if !displayed.contains(.fragment) { components.fragment = nil }
164+
guard var string = components.string else {
165+
return nil
166+
}
167+
if string.hasPrefix("//") { // remove phantom scheme
168+
string.removeFirst(2)
169+
}
170+
return string
171+
}
172+
}

Sources/PulseUI/Features/Console/Views/ConsoleTaskCell.swift

+6-12
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct ConsoleTaskCell: View {
2525

2626
let contents = VStack(alignment: .leading, spacing: spacing) {
2727
title.dynamicTypeSize(...DynamicTypeSize.xxxLarge)
28-
message
28+
content
2929
#if !os(macOS)
3030
details
3131
#endif
@@ -80,17 +80,11 @@ struct ConsoleTaskCell: View {
8080
.monospacedDigit()
8181
}
8282

83-
private var message: some View {
84-
VStack(spacing: 3) {
85-
HStack {
86-
Text(ConsoleViewDelegate.getTitle(for: task) ?? "")
87-
.font(contentFont)
88-
.foregroundColor(.primary)
89-
.lineLimit(settings.displayOptions.contentLineLimit)
90-
91-
Spacer()
92-
}
93-
}
83+
private var content: some View {
84+
Text(task.getFormattedContent(options: settings.displayOptions) ?? "")
85+
.font(contentFont)
86+
.lineLimit(settings.displayOptions.contentLineLimit)
87+
.foregroundColor(.primary)
9488
}
9589

9690
@ViewBuilder

Sources/PulseUI/Features/Settings/SettingsConsoleCellDesignView.swift

+26-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,31 @@ private struct SettingsConsoleTaskOptionsView: View {
7272
Stepper("Font Size: \(options.contentFontSize)\(options.contentFontSize == defaultContentFontSize ? " (Default)" : "")", value: $options.contentFontSize, in: (defaultContentFontSize-3)...(defaultContentFontSize+3))
7373

7474
Stepper("Line Limit: \(options.contentLineLimit)", value: $options.contentLineLimit, in: 1...20)
75+
76+
Toggle("Show Task Description", isOn: $options.showTaskDescription)
77+
78+
NavigationLink {
79+
// TODO: navigation link
80+
List(selection: $options.contentComponents) {
81+
ForEach(UserSettings.DisplayOptions.ContentComponent.allCases) {
82+
Text($0.rawValue).tag($0.rawValue)
83+
}
84+
}
85+
.navigationTitle("Components")
86+
.navigationBarTitleDisplayMode(.inline)
87+
} label: {
88+
HStack {
89+
Text("URL Components")
90+
Spacer()
91+
if options.contentComponents.count == 1 {
92+
Text(options.contentComponents.first!.rawValue)
93+
.foregroundStyle(.secondary)
94+
} else {
95+
Text(options.contentComponents.count.description)
96+
.foregroundStyle(.secondary)
97+
}
98+
}
99+
}
75100
}
76101

77102
@ViewBuilder
@@ -153,7 +178,7 @@ enum StorePreview {
153178
static let previewTask: NetworkTaskEntity? = {
154179
guard let store else { return nil }
155180

156-
let url = URL(string: "https://api.example.com/v2.1/sites/91023547/users/49032328/profile?locale=en&fields=id,firstName,lastName,email,avatarURL")!
181+
let url = URL(string: "https://user:password@api.example.com:443/v2.1/sites/91023547/users/49032328/profile?locale=en&fields=id,firstName,lastName,email,avatarURL#me")!
157182

158183
var request = URLRequest(url: url)
159184
request.setValue("Pulse", forHTTPHeaderField: "User-Agent")

Sources/PulseUI/Helpers/UserSettings.swift

+14-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ public final class UserSettings: ObservableObject {
7878
public struct DisplayOptions: Codable {
7979
// MARK: - Content
8080

81+
/// If task description is available, show it instead of the `URL`.
82+
public var showTaskDescription = false
83+
84+
/// Defines what components to display in the list. By default, shows
85+
/// only path.
86+
public var contentComponents: Set<ContentComponent> = [.path]
87+
8188
/// By default, ``FontSize/regular``.
8289
public var contentFontSize: Int = defaultContentFontSize
8390

@@ -98,6 +105,12 @@ public final class UserSettings: ObservableObject {
98105
/// Fields to display below the main text label.
99106
public var detailsFields: [Field]
100107

108+
public enum ContentComponent: String, Identifiable, CaseIterable, Codable {
109+
case scheme, user, password, host, port, path, query, fragment
110+
111+
public var id: ContentComponent { self }
112+
}
113+
101114
public enum Field: Codable, Identifiable, CaseIterable {
102115
case method
103116
case requestSize
@@ -152,7 +165,7 @@ let defaultContentFontSize = 13
152165
let defaultDefailsFontSize = 11
153166
#elseif os(iOS) || os(visionOS)
154167
let defaultContentFontSize = 16
155-
let defaultDefailsFontSize = 13
168+
let defaultDefailsFontSize = 12
156169
#elseif os(tvOS)
157170
let defaultContentFontSize = 25
158171
let defaultDefailsFontSize = 20

0 commit comments

Comments
 (0)