Skip to content

Instantly share code, notes, and snippets.

View adam-zethraeus's full-sized avatar
🏔️

adamz adam-zethraeus

🏔️
View GitHub Profile
@_spi(Internals) import ComposableArchitecture
struct Bound<R: Reducer, S, V:View> {
init(store: StoreOf<R>, _ s: KeyPath<R.State, S>, _ a: @escaping (S) -> R.Action, @ViewBuilder _ vb: @escaping (Binding<S>) -> V) {
_state = Binding<S>{store.state.value[keyPath: s] } set:{ store.send(a($0)) }
self.vb = vb
}
@Binding var state: S
@ViewBuilder var vb: (Binding<S>) -> V
@adam-zethraeus
adam-zethraeus / JSON.swift
Last active December 2, 2023 20:37
JSON.swift
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
// Source: https://github.com/apple/swift-docc/blob/ddf8f0a64f4f477259ae166b84d5f064ab2887c5/bin/output-diff.swift
@adam-zethraeus
adam-zethraeus / WithState.swift
Created September 22, 2023 10:58
WithState — easy state in Xcode previews
import SwiftUI
extension View {
public func withState<Value, V: View>(_ state: Value, @ViewBuilder content: @escaping (_ state: Binding<Value>) -> V) -> some View {
modifier(WithStateModifier(state, content: content))
}
}
public struct WithState<Value, V: View>: View {
public init(_ value: Value, @ViewBuilder content builder: @escaping (_ state: Binding<Value>) -> V) {
@adam-zethraeus
adam-zethraeus / transaction.swift
Created September 15, 2023 06:33
Transaction Wrapper
public enum Condition {
case new
case safe
}
public enum Transacting<T> {
case known(T)
case changing(known: T, tentative: T)
public mutating func stage(_ value: T) {
switch self {
@adam-zethraeus
adam-zethraeus / Tuple.swift
Created September 15, 2023 06:29
Codable/Hashable Tuple
public enum Tup {
public struct A<A> {
var a: A
}
public struct B<A, B> {
var a: A
var b: B
}
public struct C<A, B, C> {
var a: A
@adam-zethraeus
adam-zethraeus / warp-pwd.sh
Created August 22, 2023 05:12
Print the current working directory of the most recent Warp terminal window from the command line
#!/usr/bin/env bash
sqlite3 "${HOME}/Library/Application Support/dev.warp.Warp-Stable/warp.sqlite" -readonly -cmd 'select cwd from terminal_panes order by id desc limit 1' '.exit'
@adam-zethraeus
adam-zethraeus / fix-google-photos-exif.sh
Created August 16, 2023 09:10
correcting google photos takeout/export exif dates using the paired json files
# this relies on photos from google takeouts each being exported along with a paired json file
# (photo.jpg.json <-> photo.jpg) and that this json file has a timestamp
fd .jpg$ -x bash -c "exiftool -AllDates=\`cat {}.json | jq '.creationTime.timestamp' | xargs -I{} date -r {} -u +%Y-%m-%dT%H:%M:%SZ\` {}"
@adam-zethraeus
adam-zethraeus / sanitize.sh
Last active August 16, 2023 08:04
sanitize file names, or sanitize recursively
#!/usr/bin/env bash
sanitize_node() {
shopt -s extglob
filename=$(basename "$1")
directory=$(dirname "$1")
if [ -f "$1" ]; then
extension=".${filename##*.}"
else
@adam-zethraeus
adam-zethraeus / Breakpoints.js
Last active July 25, 2023 14:21
media-query based conditional React components
import { useState, useEffect } from "react";
const makeObservable = (target) => {
let listeners = [];
let value = target;
const getter = () => {
return value;
};
@adam-zethraeus
adam-zethraeus / usePolling.js
Last active July 15, 2023 03:16
usePolling React hook
import { useEffect, useState } from "react";
const usePolling = (interval = null, pollingFunction, deps = []) => {
const [subscription, setSubscription] = useState(null);
useEffect(() => {
if (!!interval) {
const id = setInterval(pollingFunction, interval);
setSubscription(id);
}
return () => {