Skip to content

Instantly share code, notes, and snippets.

View daltonclaybrook's full-sized avatar

Dalton Claybrook daltonclaybrook

View GitHub Profile
import Foundation
import SnapshotTesting
import XCTest
func assertBundledSnapshot<Value, Format>(
of value: @autoclosure () throws -> Value,
as snapshotting: Snapshotting<Value, Format>,
named name: String? = nil,
record recording: Bool? = nil,
timeout: TimeInterval = 5,
import Foundation
#if canImport(DeveloperToolsSupport)
import DeveloperToolsSupport
#endif
#if SWIFT_PACKAGE
private let resourceBundle = Foundation.Bundle.module
#else
private class ResourceBundleClass {}
private let resourceBundle = Foundation.Bundle(for: ResourceBundleClass.self)
@daltonclaybrook
daltonclaybrook / ai_replica.py
Created May 1, 2025 21:28
Create recursive replicas of an image with ChatGPT
import base64
import os
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
count = 50
start_duration = 1
mid_duration = 0.2
/// A macro that produces an expression to initialize an `IntegerPair` after validating that the bit
/// width of `Second` is a positive multiple of `First`.
@freestanding(expression)
public macro IntegerPair<First: UnsignedInteger, Second: UnsignedInteger>(
_ firstType: First.Type,
_ secondType: Second.Type
) -> IntegerPair<First, Second> = #externalMacro(module: "BitWidthMacros", type: "IntegerPairMacro")
@daltonclaybrook
daltonclaybrook / useElementSize.ts
Created August 26, 2024 03:43
React hook for observing an element's size
import { useEffect, useRef, useState } from 'react';
interface ElementSize<T extends HTMLElement> extends Size {
ref: React.RefObject<T>;
}
interface Size {
width: number;
height: number;
}
@daltonclaybrook
daltonclaybrook / install-swift.sh
Created April 7, 2024 04:26
Gist for installing Swift on Fedora
#!/bin/bash
yum -y install \
git \
gcc-c++ \
libcurl-devel \
libedit-devel \
libuuid-devel \
libxml2-devel \
ncurses-devel \
import SwiftUI
struct ContentView: View {
@State private var leftCounter = 1
@State private var rightCounter = 1
var body: some View {
VStack(spacing: 10) {
HStack {
Text("\(leftCounter)")
@daltonclaybrook
daltonclaybrook / DateStyle.swift
Created September 4, 2023 16:09
An example unwrapping a (Date, Text.DateStyle) from LocalizedStringKey underlying storage. I'm not sure how to actually format the date with the DateStyle.
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
private func stringFromDateStyle(storage: Any) -> String? {
let dateStyleChildren = Array(Mirror(reflecting: storage).children)
guard dateStyleChildren.count == 2, let text = dateStyleChildren[0].value as? Text else {
return nil
}
let textStorage = Array(Mirror(reflecting: text).children)[0].value // Text.Storage
let dateTextStorage = Array(Mirror(reflecting: textStorage).children)[0].value
guard "\(type(of: dateTextStorage))" == "DateTextStorage" else {
@daltonclaybrook
daltonclaybrook / ImageLoader.swift
Created August 6, 2023 19:02
A thin wrapper around Kingfisher's image cache
import Foundation
import Kingfisher
final class ImageLoader {
private let imageCache: ImageCache
init() throws {
self.imageCache = try Self.loadImageCache()
}
@daltonclaybrook
daltonclaybrook / Backoff.swift
Last active August 2, 2023 14:45
A simple exponential backoff operation using async/await
// Created by Dalton Claybrook on 8/1/23.
import Foundation
struct Backoff {
enum RetryPolicy {
case indefinite
case maxAttempts(Int)
}