macOS client parity review
This commit is contained in:
@@ -52,12 +52,82 @@ func jsString(_ v: (any Sendable)??) -> String {
|
||||
}
|
||||
|
||||
func prettyJSON(_ v: (any Sendable)?) -> String {
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
|
||||
guard let container = try? OpenAPIValueContainer(unvalidatedValue: v), let data = try? encoder.encode(container) else {
|
||||
return String(describing: v)
|
||||
jsonText(v, indent: " ", depth: 0)
|
||||
}
|
||||
|
||||
func compactJSON(_ v: (any Sendable)?) -> String {
|
||||
jsonText(v, indent: nil, depth: 0)
|
||||
}
|
||||
|
||||
private func jsonText(_ v: (any Sendable)?, indent: String?, depth: Int) -> String {
|
||||
guard let v else {
|
||||
return "null"
|
||||
}
|
||||
return String(decoding: data, as: UTF8.self)
|
||||
if let s = v as? String {
|
||||
return jsonQuote(s)
|
||||
}
|
||||
if let b = v as? Bool {
|
||||
return b ? "true" : "false"
|
||||
}
|
||||
if let i = v as? Int {
|
||||
return String(i)
|
||||
}
|
||||
if let d = v as? Double {
|
||||
return d.isFinite ? jsString(d) : "null"
|
||||
}
|
||||
let items: [String]
|
||||
let open: String
|
||||
let close: String
|
||||
if let list = v as? [(any Sendable)?] {
|
||||
items = list.map { jsonText($0, indent: indent, depth: depth + 1) }
|
||||
open = "["
|
||||
close = "]"
|
||||
} else if let d = v as? JSONDict {
|
||||
let keys = d.keys.sorted { $0.utf8.lexicographicallyPrecedes($1.utf8) }
|
||||
items = keys.map { "\(jsonQuote($0)):\(indent == nil ? "" : " ")\(jsonText(d[$0] ?? nil, indent: indent, depth: depth + 1))" }
|
||||
open = "{"
|
||||
close = "}"
|
||||
} else {
|
||||
return jsonQuote(String(describing: v))
|
||||
}
|
||||
if items.isEmpty {
|
||||
return open + close
|
||||
}
|
||||
guard let indent else {
|
||||
return open + items.joined(separator: ",") + close
|
||||
}
|
||||
let inner = "\n" + String(repeating: indent, count: depth + 1)
|
||||
return open + inner + items.joined(separator: "," + inner) + "\n" + String(repeating: indent, count: depth) + close
|
||||
}
|
||||
|
||||
private func jsonQuote(_ s: String) -> String {
|
||||
var out = "\""
|
||||
for u in s.unicodeScalars {
|
||||
switch u {
|
||||
case "\"":
|
||||
out += "\\\""
|
||||
case "\\":
|
||||
out += "\\\\"
|
||||
case "\u{08}":
|
||||
out += "\\b"
|
||||
case "\u{0C}":
|
||||
out += "\\f"
|
||||
case "\n":
|
||||
out += "\\n"
|
||||
case "\r":
|
||||
out += "\\r"
|
||||
case "\t":
|
||||
out += "\\t"
|
||||
default:
|
||||
if u.value < 0x20 {
|
||||
let hex = String(u.value, radix: 16)
|
||||
out += "\\u" + String(repeating: "0", count: 4 - hex.count) + hex
|
||||
} else {
|
||||
out.unicodeScalars.append(u)
|
||||
}
|
||||
}
|
||||
}
|
||||
return out + "\""
|
||||
}
|
||||
|
||||
func ago(_ date: Date) -> String {
|
||||
@@ -78,6 +148,10 @@ func isoText(_ date: Date) -> String {
|
||||
date.formatted(Date.ISO8601FormatStyle(includingFractionalSeconds: true))
|
||||
}
|
||||
|
||||
func clockText(_ d: Date) -> String {
|
||||
d.formatted(Date.VerbatimFormatStyle(format: "\(hour: .twoDigits(clock: .twentyFourHour, hourCycle: .zeroBased)):\(minute: .twoDigits):\(second: .twoDigits)", timeZone: .current, calendar: .current))
|
||||
}
|
||||
|
||||
func trimmed(_ s: String) -> String {
|
||||
s.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
}
|
||||
@@ -490,3 +564,7 @@ struct FlowLayout: Layout {
|
||||
return rows
|
||||
}
|
||||
}
|
||||
|
||||
let overlayShadow = Color(nsColor: NSColor(name: nil) { appearance in
|
||||
appearance.bestMatch(from: [.aqua, .darkAqua]) == .darkAqua ? NSColor(srgbRed: 0, green: 0, blue: 0, alpha: 0.5) : NSColor(srgbRed: 16 / 255, green: 24 / 255, blue: 40 / 255, alpha: 0.18)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user