Skip to content

Instantly share code, notes, and snippets.

Avatar

Max Howell mxcl

View GitHub Profile
View chalk-demo.swift
#!/usr/bin/swift sh
import Foundation
import Chalk // @mxcl == 0.3
extension Int {
var fg: UInt8 {
if !(16..<250 ~= self) || 24...36 ~= (self - 16) % 36 {
return 16
} else {
return 255
@mxcl
mxcl / detweet.swift
Last active Aug 4, 2020
Delete all tweets and favorites older than two months ago. Instructions in comment.
View detweet.swift
#!/usr/bin/swift sh
import Foundation
import PromiseKit // @mxcl ~> 6.5
import Swifter // @mattdonnelly == b27a89
let swifter = Swifter(
consumerKey: "FILL",
consumerSecret: "ME",
oauthToken: "IN",
oauthTokenSecret: "https://developer.twitter.com/en/docs/basics/apps/overview.html"
View UUID+ascii85.swift
private extension UUID {
/// a more concise representation of UUIDs
var ascii85: String {
func convert(_ a: UInt8, _ b: UInt8, _ c: UInt8, _ d: UInt8) -> [UInt8] {
if a == 0, b == 0, c == 0, d == 0 {
return [122] // "z"
}
let x = UInt(a) * 52200625 + UInt(b) * 614125 + UInt(c) * 7225 + UInt(d)
View apple-receipt-validation-response-decodable.swift
struct AppleReceiptValidationResponse: Decodable {
let status: Int
let environment: String
struct Receipt: Decodable {
let receipt_type: String
let adam_id: Int
let app_item_id: Int
let bundle_id: String
let application_version: String
let download_id: Int
View UIAlertController+Error.swift
import PromiseKit
public extension UIViewController {
@discardableResult
func alert(error: Error, title: String? = nil, file: StaticString = #file, line: UInt = #line) -> Guarantee<Void> {
let (promise, seal) = Guarantee<UIAlertAction>.pending()
let alert = UIAlertController(title: title ?? String(describing: type(of: error)), message: error.legibleDescription, preferredStyle: .alert)
alert.addAction(.init(title: "OK", style: .default, handler: seal))
View Error+legibleDescription.swift
extension Error {
public var legibleDescription: String {
switch errorType {
case .swiftError(.enum?):
return "\(type(of: self)).\(self)"
case .swiftError:
return String(describing: self)
case .swiftLocalizedError(let msg):
return msg
case .nsError(_, "kCLErrorDomain", 0):
View Error+legibleDescription.swift
public extension Error {
var legibleDescription: String {
if let error = self as? LocalizedError, let description = error.errorDescription {
return description
} else if (self as AnyObject).isKind(of: NSError.self) {
return localizedDescription
} else {
let mirror = Mirror(reflecting: self)
switch mirror.displayStyle {
View Hexagon-hard-way.swift
// without turtle drawing a hexagon is math heavy and not trivial to modify
let numberOfSides: CGFloat = 6
let radiusOuterCircle: CGFloat = bounds.width
let sideLength = radiusOuterCircle / 2
let theta = (CGFloat.pi * 2) / numberOfSides
let centerX = sideLength / 2
let centerY = sideLength / 2
let initialPoint = CGPoint(x: radiusOuterCircle * cos(2 * CGFloat.pi * 0/numberOfSides + theta) + centerX, y: radiusOuterCircle * sin(2 * CGFloat.pi * 0/numberOfSides + theta) + centerY)
View UIImage+Blur+Swift4.swift
extension UIImage {
func blurred(radius: CGFloat) -> UIImage {
let ciContext = CIContext(options: nil)
guard let cgImage = cgImage else { return self }
let inputImage = CIImage(cgImage: cgImage)
guard let ciFilter = CIFilter(name: "CIGaussianBlur") else { return self }
ciFilter.setValue(inputImage, forKey: kCIInputImageKey)
ciFilter.setValue(radius, forKey: "inputRadius")
guard let resultImage = ciFilter.value(forKey: kCIOutputImageKey) as? CIImage else { return self }
View zodiac.swift
enum Zodiac: String {
case aries, taurus, gemini, cancer, leo, virgo, libra, scorpio, sagittarius, capricorn, aquarius, pisces
}
extension Zodiac {
var dateRangeString: String {
let df = DateFormatter()
df.dateFormat = "MMM d"
let from = df.string(from: dateRange.lowerBound)
let to = df.string(from: dateRange.upperBound)
You can’t perform that action at this time.