Skip to content

Commit

Permalink
Add an option to configure what parts of the URL to display
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Sep 8, 2024
1 parent 54cf739 commit 7bcaa4b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 14 deletions.
39 changes: 39 additions & 0 deletions Sources/PulseUI/Extensions/Pulse+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,42 @@ extension NetworkTaskEntity {
}

#endif

extension NetworkTaskEntity {
func getFormattedContent(options: DisplayOptions) -> String? {
if options.showTaskDescription, let taskDescription, !taskDescription.isEmpty {
return taskDescription
}
guard let url else {
return nil
}
let displayed = options.contentComponents
if displayed.count == UserSettings.DisplayOptions.ContentComponent.allCases.count {
return url
}
guard var components = URLComponents(string: url) else {
return nil
}
if displayed.count == 1 && displayed.first == .path {
return components.path // optimization
}
if !(components.password ?? "").isEmpty {
components.password = "_"
}
if !displayed.contains(.scheme) { components.scheme = nil }
if !displayed.contains(.user) { components.user = nil }
if !displayed.contains(.password) { components.password = nil }
if !displayed.contains(.host) { components.host = nil }
if !displayed.contains(.port) { components.port = nil }
if !displayed.contains(.path) { /* can't remove path */ }
if !displayed.contains(.query) { components.query = nil }
if !displayed.contains(.fragment) { components.fragment = nil }
guard var string = components.string else {
return nil
}
if string.hasPrefix("//") { // remove phantom scheme
string.removeFirst(2)
}
return string
}
}
18 changes: 6 additions & 12 deletions Sources/PulseUI/Features/Console/Views/ConsoleTaskCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct ConsoleTaskCell: View {

let contents = VStack(alignment: .leading, spacing: spacing) {
title.dynamicTypeSize(...DynamicTypeSize.xxxLarge)
message
content
#if !os(macOS)
details
#endif
Expand Down Expand Up @@ -80,17 +80,11 @@ struct ConsoleTaskCell: View {
.monospacedDigit()
}

private var message: some View {
VStack(spacing: 3) {
HStack {
Text(ConsoleViewDelegate.getTitle(for: task) ?? "")
.font(contentFont)
.foregroundColor(.primary)
.lineLimit(settings.displayOptions.contentLineLimit)

Spacer()
}
}
private var content: some View {
Text(task.getFormattedContent(options: settings.displayOptions) ?? "")
.font(contentFont)
.lineLimit(settings.displayOptions.contentLineLimit)
.foregroundColor(.primary)
}

@ViewBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ private struct SettingsConsoleTaskOptionsView: View {
Stepper("Font Size: \(options.contentFontSize)\(options.contentFontSize == defaultContentFontSize ? " (Default)" : "")", value: $options.contentFontSize, in: (defaultContentFontSize-3)...(defaultContentFontSize+3))

Stepper("Line Limit: \(options.contentLineLimit)", value: $options.contentLineLimit, in: 1...20)

Toggle("Show Task Description", isOn: $options.showTaskDescription)

NavigationLink {
// TODO: navigation link
List(selection: $options.contentComponents) {
ForEach(UserSettings.DisplayOptions.ContentComponent.allCases) {
Text($0.rawValue).tag($0.rawValue)
}
}
.navigationTitle("Components")
.navigationBarTitleDisplayMode(.inline)
} label: {
HStack {
Text("URL Components")
Spacer()
if options.contentComponents.count == 1 {
Text(options.contentComponents.first!.rawValue)
.foregroundStyle(.secondary)
} else {
Text(options.contentComponents.count.description)
.foregroundStyle(.secondary)
}
}
}
}

@ViewBuilder
Expand Down Expand Up @@ -153,7 +178,7 @@ enum StorePreview {
static let previewTask: NetworkTaskEntity? = {
guard let store else { return nil }

let url = URL(string: "https://api.example.com/v2.1/sites/91023547/users/49032328/profile?locale=en&fields=id,firstName,lastName,email,avatarURL")!
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")!

var request = URLRequest(url: url)
request.setValue("Pulse", forHTTPHeaderField: "User-Agent")
Expand Down
15 changes: 14 additions & 1 deletion Sources/PulseUI/Helpers/UserSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ public final class UserSettings: ObservableObject {
public struct DisplayOptions: Codable {
// MARK: - Content

/// If task description is available, show it instead of the `URL`.
public var showTaskDescription = false

/// Defines what components to display in the list. By default, shows
/// only path.
public var contentComponents: Set<ContentComponent> = [.path]

/// By default, ``FontSize/regular``.
public var contentFontSize: Int = defaultContentFontSize

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

public enum ContentComponent: String, Identifiable, CaseIterable, Codable {
case scheme, user, password, host, port, path, query, fragment

public var id: ContentComponent { self }
}

public enum Field: Codable, Identifiable, CaseIterable {
case method
case requestSize
Expand Down Expand Up @@ -152,7 +165,7 @@ let defaultContentFontSize = 13
let defaultDefailsFontSize = 11
#elseif os(iOS) || os(visionOS)
let defaultContentFontSize = 16
let defaultDefailsFontSize = 13
let defaultDefailsFontSize = 12
#elseif os(tvOS)
let defaultContentFontSize = 25
let defaultDefailsFontSize = 20
Expand Down

0 comments on commit 7bcaa4b

Please sign in to comment.