31 lines
1.1 KiB
Swift
31 lines
1.1 KiB
Swift
import Foundation
|
|
|
|
/// The URL scheme of Plak, the scratchpad app: each send adds one block at the end of its page.
|
|
public enum Plak {
|
|
/// Text above this many UTF-8 bytes goes through the pasteboard, since a long URL can fail to open.
|
|
public static let appendLimit = 100_000
|
|
|
|
public static let pasteURL = URL(string: "plak://paste")!
|
|
|
|
public enum Delivery: Sendable, Equatable {
|
|
case append(URL)
|
|
case paste
|
|
}
|
|
|
|
/// URLComponents leaves `+` bare in a query, and Plak's contract asks for `%2B`.
|
|
public static func appendURL(_ text: String) -> URL? {
|
|
var components = URLComponents()
|
|
components.scheme = "plak"
|
|
components.host = "append"
|
|
components.queryItems = [URLQueryItem(name: "text", value: text)]
|
|
components.percentEncodedQuery = components.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B")
|
|
return components.url
|
|
}
|
|
|
|
/// Nil when short text yields no URL.
|
|
public static func delivery(for text: String) -> Delivery? {
|
|
guard text.utf8.count <= appendLimit else { return .paste }
|
|
return appendURL(text).map(Delivery.append)
|
|
}
|
|
}
|