This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PlaygroundSupport | |
import SpriteKit | |
// Set up scene: | |
let size = CGSize(width: 480, height: 320) | |
let scene = SKScene(size: size) | |
scene.physicsBody = SKPhysicsBody(edgeLoopFrom: scene.frame) | |
scene.physicsWorld.gravity = CGVector.zero // Enabled later |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from http://stackoverflow.com/a/1248 | |
private let minuteInterval: TimeInterval = 60 | |
private let hourInterval: TimeInterval = 60 * minuteInterval | |
private let dayInterval: TimeInterval = 24 * hourInterval | |
private let monthInterval: TimeInterval = 30 * dayInterval | |
extension Date { | |
var relative: String { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Connection adapter for Requests that allows it to talk with raw UNIX sockets. | |
Adapted from requests-unixsocket, which itself was adapted from docker-py. | |
https://github.com/msabramo/requests-unixsocket | |
https://github.com/docker/docker-py/blob/master/docker/unixconn/unixconn.py | |
""" | |
import socket | |
from urllib.parse import unquote, urlparse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RxSwift | |
extension ObservableType { | |
/// A map that unwraps an optional value, only continuing if the result is not nil. | |
func ignoreNilMap<R>(transform: @escaping (E) -> R?) -> Observable<R> { | |
return Observable.create { observer in | |
return self.subscribe { element in | |
switch element { | |
case .next(let value): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct SparkleCleanup { | |
private static let oldDownloadAge: TimeInterval = 86400 // 24 hours | |
/// The location where Sparkle caches file downloads. | |
private static var cacheURL: URL { | |
guard let bundleIdentifier = Bundle.main.bundleIdentifier else { | |
fatalError("Unable to get bundle identifier.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension String { | |
/// Finds and returns the ranges of occurrences of a given string within a given range of the `String`. | |
func ranges(of searchString: String, options: CompareOptions = [], range searchRange: Range<Index>? = nil, locale: Locale? = nil) -> [Range<Index>] { | |
let searchRange = searchRange ?? startIndex..<endIndex | |
if let foundRange = range(of: searchString, options: options, range: searchRange, locale: locale) { | |
let nextRange = foundRange.upperBound..<searchRange.upperBound |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AppKit | |
extension NSApplication { | |
/// Restarts the application. | |
func restart() { | |
let process = Process() | |
process.launchPath = "/bin/sh" | |
process.arguments = ["-c", "sleep 1; open '\(Bundle.main.bundlePath)'"] | |
process.launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
extension NSMutableAttributedString { | |
/// Makes the specified text a clickable hyperlink. | |
func hyperlink(text: String, with url: URL, additionalAttributes: [NSAttributedStringKey: Any] = [:]) { | |
var attributes = additionalAttributes | |
attributes[.link] = url.absoluteString | |
let range = (string as NSString).range(of: text) | |
addAttributes(attributes, range: range) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Collection { | |
/// Returns the element at the specified index if it is within bounds, or nil if it's outside. | |
subscript(safe index: Index) -> Iterator.Element? { | |
return indices.contains(index) ? self[index] : nil | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func insert(_ string: String, at index: Int) -> String { | |
let prefix = String(characters.prefix(index)) | |
let suffix = String(characters.suffix(characters.count - index)) | |
return prefix + string + suffix | |
} | |
} |