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 { | |
/// Perform a block on each chunk of `size` elements in this collection | |
/// - Parameters: | |
/// - size: The maximum size of the chunk | |
/// - block: The block to call with the chunk. Set `stop = true` to stop processing | |
func chunking(into size: Int, _ block: (SubSequence, inout Bool) -> Void) { | |
var stop = false | |
var index = 0 | |
while index < self.count { | |
let offset = index + Swift.min(size, count - index) |
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
// | |
// Copyright © 2024 Darren Ford. All rights reserved. | |
// | |
// Adapted from the PaintCode blog: https://www.paintcodeapp.com/news/code-for-ios-7-rounded-rectangles | |
// | |
// MIT license | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | |
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
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
public extension Sequence where Element: Equatable { | |
/// Return the unique elements in the array using Equatable as the predicate | |
var unique: [Element] { | |
return self.reduce(into: []) { uniqueElements, element in | |
if !uniqueElements.contains(element) { | |
uniqueElements.append(element) | |
} | |
} | |
} | |
} |
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
/// Zip two sequences together, padding the shorter sequence with nil | |
func zipPadded<Value1, Value2>(_ z1: any Sequence<Value1>, _ z2: any Sequence<Value2>) -> [(Value1?, Value2?)] { | |
var v1 = z1.makeIterator() as any IteratorProtocol<Value1> | |
var v2 = z2.makeIterator() as any IteratorProtocol<Value2> | |
var vv1 = v1.next() | |
var vv2 = v2.next() | |
var result = [(Value1?, Value2?)]() |
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
#!/usr/bin/swift | |
import Foundation | |
import AppKit.NSWorkspace | |
// Returns the name of the frontmost app, or <none> if no app is frontmost | |
func currentFocusApp() -> String { | |
NSWorkspace.shared.frontmostApplication?.localizedName ?? "<none>" | |
} |
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
// Observe all notifications generated by the default NotificationCenter | |
NotificationCenter.default.addObserver( | |
forName: nil, object: nil, queue: nil) { notification in | |
Swift.print("Notification: \(notification.name.rawValue), Object: \(notification.object)") | |
} | |
// Observe all notifications generated by the default DistributedNotificationCenter | |
DistributedNotificationCenter.default().addObserver( | |
forName: nil, object: nil, queue: nil) { notification in | |
Swift.print("Notification: \(notification.name.rawValue), Object: \(notification.object)") |
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
// | |
// NSColor+animations.swift | |
// | |
// Created by Darren Ford on 27/7/21 | |
// | |
// MIT License | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights |
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 | |
/// Value transformer that returns NSNumber(true) if the value is a non empty string or NSNumber(false) otherwise | |
@objc public class IsEmptyStringValueTransformer: ValueTransformer { | |
// Public name to use in interface builder | |
private static let name = NSValueTransformerName(rawValue: "IsEmptyStringValueTransformer") | |
// Shared value transformer instance | |
@objc static public var shared = ValueTransformer(forName: IsEmptyStringValueTransformer.name) |
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
@objc class CIImageToNSImageTransformer: ValueTransformer { | |
@objc public static func RegisterTransformer() { | |
ValueTransformer.setValueTransformer( | |
CIImageToNSImageTransformer(), | |
forName: NSValueTransformerName("CIImageToNSImageTransformer") | |
) | |
} | |
override class func transformedValueClass() -> AnyClass { | |
return NSImage.self |
NewerOlder