Skip to content

Instantly share code, notes, and snippets.

View daltonclaybrook's full-sized avatar

Dalton Claybrook daltonclaybrook

View GitHub Profile
@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)
}
@daltonclaybrook
daltonclaybrook / Flag.css
Last active August 15, 2022 15:30
The US flag in React + CSS
.container {
position: relative;
display: flex;
flex-direction: column;
gap: 0px;
aspect-ratio: 1.9;
}
.stripe {
flex-basis: 0;
@daltonclaybrook
daltonclaybrook / lib.rs
Created June 23, 2022 16:36
Example of compile-time URL parsing in Rust
extern crate proc_macro;
use proc_macro::TokenStream;
use syn::parse_macro_input;
use quote::quote;
use regex::Regex;
#[proc_macro]
pub fn make_url(_item: TokenStream) -> TokenStream {
let input = parse_macro_input!(_item as syn::LitStr);
let url_regex = Regex::new(r"^(\w+)://([\w-]+(?:\.[\w-]+)*)((?:/[\w-]+)*)?(\?[\w\-=&]+)?$").unwrap();
typealias Roll = Int
typealias Score = Int
enum FrameAttempt {
case strike
case spare(first: Roll)
case open(first: Roll, second: Roll)
case tenth(first: Roll, second: Roll, third: Roll?)
case inProgress(first: Roll, second: Roll?)
}
@daltonclaybrook
daltonclaybrook / TextAttributes.swift
Created January 15, 2022 19:13
An Equatable, value-semantic container for NSAttributedString attributes
import Foundation
/// An Equatable container for storing and accessing just the attributes of an NSAttributedString without
/// caring about the string itself.
struct TextAttributes: Equatable {
private var storage = NSMutableAttributedString(string: "")
mutating func setAttributes(_ attributes: [NSAttributedString.Key: Any], range: NSRange) {
preserveUniqueReferenceToStorageIfNecessary()
extendStringRangeIfNecessary(range: range)