forked from pointfreeco/swift-custom-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftUI.swift
76 lines (72 loc) · 2.26 KB
/
SwiftUI.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#if canImport(SwiftUI)
import SwiftUI
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension Animation: CustomDumpStringConvertible {
public var customDumpDescription: String {
switch self {
case .easeIn:
return "Animation.easeIn"
case .easeInOut:
return "Animation.easeInOut"
case .easeOut:
return "Animation.easeOut"
case .interactiveSpring():
return "Animation.interactiveSpring()"
case .linear:
return "Animation.linear"
case .spring():
return "Animation.spring()"
default:
let base = _customDump(
Mirror(reflecting: self).children.first?.value as Any,
name: nil,
indent: 2,
maxDepth: .max
)
return """
Animation(
\(base)
)
"""
}
}
}
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension LocalizedStringKey: CustomDumpRepresentable {
public var customDumpValue: Any {
self.formatted()
}
private func formatted(
locale: Locale? = nil,
tableName: String? = nil,
bundle: Bundle? = nil,
comment: StaticString? = nil
) -> String {
let children = Array(Mirror(reflecting: self).children)
let key = children[0].value as! String
let arguments: [CVarArg] = Array(Mirror(reflecting: children[2].value).children)
.compactMap {
let children = Array(Mirror(reflecting: $0.value).children)
let value: Any
let formatter: Formatter?
// `LocalizedStringKey.FormatArgument` differs depending on OS/platform.
if children[0].label == "storage" {
(value, formatter) =
Array(Mirror(reflecting: children[0].value).children)[0].value as! (Any, Formatter?)
} else {
value = children[0].value
formatter = children[1].value as? Formatter
}
return formatter?.string(for: value) ?? value as! CVarArg
}
let format = NSLocalizedString(
key,
tableName: tableName,
bundle: bundle ?? .main,
value: "",
comment: comment.map(String.init) ?? ""
)
return String(format: format, locale: locale, arguments: arguments)
}
}
#endif