Skip to content

Instantly share code, notes, and snippets.

Charlton Provatas cprovatas

Block or report user

Report or block cprovatas

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@cprovatas
cprovatas / gist:8d4dafaa13da058da32b0f68f625b424
Created Oct 19, 2017
Solution to eliminate boilerplate code when Implementing Equatable for Associated Value Enums
View gist:8d4dafaa13da058da32b0f68f625b424
import UIKit
protocol AutoEquatableEnum {
static func == (lhs: Self, rhs: Self) -> Bool
}
extension AutoEquatableEnum {
static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.data == rhs.data
}
@cprovatas
cprovatas / ActivateFileNavigatorSearch.applescript
Last active Jan 15, 2018
Make file navigator search bar active in Xcode 7,8,9
View ActivateFileNavigatorSearch.applescript
on is_running(appName)
tell application "System Events" to (name of processes) contains appName
end is_running
tell application "Xcode" to activate
if is_running("Xcode") then
tell application "System Events"
tell application "Xcode"
if (count of windows) < 1 then
@cprovatas
cprovatas / TopLevelCollection.swift
Created Jan 19, 2018
Auto-implement class/struct as envelope for top level arrays in JSON by implementing this simple protocol
View TopLevelCollection.swift
protocol TopLevelCollection: Codable {
associatedtype ElementType: Codable
var elements: [ElementType] { get set }
init(elements: [ElementType])
}
extension TopLevelCollection {
public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
View RoundedSecond.swift
extension Date {
var roundedToSecond: Date {
let date = self
let diff = 1000000000 - Calendar.current.component(.nanosecond, from: date)
return Calendar.current.date(byAdding: .nanosecond, value: diff, to: date)!
}
}
/// USAGE:
let roundedDate = Date().roundedToSecond /// zero nanoseconds
@cprovatas
cprovatas / Trim.swift
Created Mar 8, 2018
trim null values out of a dictionary in Swift 4
View Trim.swift
//Uses only O(n) complexity.
extension Dictionary where Key == String, Value == Any? {
var trimmingNullValues: [String: Any] {
var copy = self
forEach { (key, value) in
if value == nil {
copy.removeValue(forKey: key)
}
@cprovatas
cprovatas / Throttle.swift
Last active Apr 9, 2018
Throttle , similar to RxSwift's throttle but can be used on any arbitrary block
View Throttle.swift
/// throttle a procedure for the given time interval, operation identifer is used to uniquely identify operation to prevent from firing multiple times
private var throttledOperations: Set<String> = []
func dispatchThrottle(_ interval: TimeInterval, operationIdentifier: String, queue: DispatchQueue = .main, _ closure: @escaping () -> Void) {
guard !throttledOperations.contains(operationIdentifier) else { return }
throttledOperations.insert(operationIdentifier)
closure()
queue.asyncAfter(deadline: .now() + interval) {
throttledOperations.remove(operationIdentifier)
}
}
@cprovatas
cprovatas / CGImage+Utilities.swift
Last active Jul 6, 2018
Determine if a CGImage is completely white
View CGImage+Utilities.swift
import UIKit
extension CGImage {
var isBlank: Bool {
guard let data = dataProvider?.data, let buffer = CFDataGetBytePtr(data) else { return false }
let length = CFDataGetLength(data)
var i = 0
while i < length {
if buffer[i] < 255, buffer[i + 1] < 255, buffer[i + 2] < 255 {
return false
@cprovatas
cprovatas / gist:6acef442fc43123bcd5d5e937dc7951a
Created Jan 6, 2018
Monitor Mouse Event for any arbitrary application on macOS in Swift
View gist:6acef442fc43123bcd5d5e937dc7951a
let callback: CGEventTapCallBack = { (tapProxy, eventType, event, refcon) -> Unmanaged<CGEvent>? in
debugPrint("we are monitoring the mouse event here")
return nil
}
let eventMask = (1 << CGEventType.leftMouseDown.rawValue) | (1 << CGEventType.leftMouseUp.rawValue)
let machPort = CGEvent.tapCreateForPid(pid: 40529, place: .tailAppendEventTap, options: .defaultTap, eventsOfInterest: CGEventMask(eventMask), callback: callback, userInfo: nil)!
let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, machPort, 0)
@cprovatas
cprovatas / TypeReflectable.swift
Last active Oct 20, 2018
Access current type by calling Self instead of manually calling <current type>.self
View TypeReflectable.swift
protocol TypeReflectable {
var `Self`: Self.Type { get }
static var `Self`: Self.Type { get }
}
extension TypeReflectable {
var `Self`: Self.Type {
return type(of: self)
}
@cprovatas
cprovatas / interpolation.swift
Created Jun 8, 2019
Decorating Command Line Output With DefaultStringInterpolation
View interpolation.swift
enum ASCIIColor: String {
case black = "\u{001B}[0;30m"
case red = "\u{001B}[0;31m"
case green = "\u{001B}[0;32m"
case yellow = "\u{001B}[0;33m"
case blue = "\u{001B}[0;34m"
case magenta = "\u{001B}[0;35m"
case cyan = "\u{001B}[0;36m"
case white = "\u{001B}[0;37m"
case `default` = "\u{001B}[0;0m"
You can’t perform that action at this time.