Skip to content

Instantly share code, notes, and snippets.

View doozMen's full-sized avatar

Stijn Willems doozMen

View GitHub Profile
@doozMen
doozMen / Process+Bash+Async.swift
Last active September 6, 2023 07:20
Run a simple bash command or run an executable with arguments both async
// MARK: - Functions
/// Until the spm proposal to import things in swift scripts we have to copy this
/// [SwiftPM support for Swift scripts - Evolution / Pitches - Swift Forums](https://forums.swift.org/t/swiftpm-support-for-swift-scripts/33126)
extension Process {
func runAsync() async throws {
let command = "\(executableURL?.lastPathComponent ?? "") \(arguments?.joined(separator: " ") ?? "")"
print(command)
try run()
try await Task {
waitUntilExit()
@doozMen
doozMen / ResultBuilder.swift
Last active June 23, 2022 09:03
ResultBuilder for a Dictionary in > Swift 5.4
//
// DictionaryBuilder.swift
//
// From blog of alejandrom: https://alejandromp.com/blog/better-dictionary-literals-with-swift-result-builders/
//
import Foundation
/// Based on https://alejandromp.com/blog/better-dictionary-literals-with-swift-result-builders/
///
@doozMen
doozMen / LocalNetworkAuthorization.swift
Created February 25, 2022 15:44
Uses bonjour networking to relialby check if user has granted local network access with async await as of iOS14
import Foundation
import Network
/// Uses bonjour networking to relialby check if user has granted local network access
/// How to use:
/// Add LocalNetworkAuthorization class to your project
/// Open .plist file and add "_bonjour._tcp", "_lnp._tcp.", as a values under "Bonjour services"
/// Call requestAuthorization() to trigger the prompt or get the authorization status if it already been approved/denied
/// about the author: https://stackoverflow.com/a/67758105/705761
@doozMen
doozMen / CreateEmptyVideo.swift
Created January 28, 2022 13:07
Creates a SMSampleBuffer for video to be used in unit tests
func createEmptyVideo() -> CMSampleBuffer {
var pixelBuffer : CVPixelBuffer? = nil
CVPixelBufferCreate(kCFAllocatorDefault, 100, 100, kCVPixelFormatType_444YpCbCr10BiPlanarVideoRange, nil, &pixelBuffer)
var info = CMSampleTimingInfo()
info.presentationTimeStamp = .zero
info.duration = .invalid
info.decodeTimeStamp = .invalid
@doozMen
doozMen / CreateSilentAudio.swift
Last active January 27, 2022 10:19
CMSampleBuffer for audio created to be used in unit tests - swift 5.5
import AVFoundation
func createSilentAudio(startFrm: Int64, nFrames: Int, sampleRate: Float64, numChannels: UInt32) -> CMSampleBuffer {
let bytesPerFrame = UInt32(2 * numChannels)
let blockSize = nFrames*Int(bytesPerFrame)
var block: CMBlockBuffer?
var status = CMBlockBufferCreateWithMemoryBlock(
allocator: kCFAllocatorDefault,
memoryBlock: nil,
@doozMen
doozMen / gist:4a7cdc7996c03cf6f460
Created June 11, 2015 18:13
Array index extension
extension Array {
func indexes<T>(xs: [T], check: T -> Bool) -> [Int] {
var result = [Int]()
var i = 0
for x in xs {
if check(x) {
result.append(i)
}
i++
@doozMen
doozMen / gist:15981fef2e025ad63608
Created June 10, 2015 10:52
A swift vertical layout alternative
//
// Layout.swift
//
// Created by Stijn Willems on 10/06/15.
// Copyright (c) 2015 dooZ. All rights reserved.
//
import Foundation