Skip to content

Instantly share code, notes, and snippets.

View davidbalbert's full-sized avatar

David Albert davidbalbert

View GitHub Profile
//
// ContentView.swift
// UnicodeExplorer
//
// Created by David Albert on 5/1/24.
//
import SwiftUI
struct ContentView: View {
@davidbalbert
davidbalbert / Text+String.swift
Last active April 21, 2024 03:12
Hack to extract a String from SwiftUI.Text
// Cursed! Do not use!
// Supports most, but not all Text initializers.
import SwiftUI
extension FormatStyle {
func format(any value: Any) -> FormatOutput? {
if let v = value as? FormatInput {
return format(v)
@davidbalbert
davidbalbert / miniKanren.swift
Last active April 4, 2024 19:47
miniKanren
// miniKanren.swift, written by David Albert, and released into the public domain.
//
// An implementation of the miniKanren relational programming language. Somewhat
// reductively, relational programs can be run both forwards and backwards. In
// addition to asking "what does this function return when passed these arguments,"
// you can also ask "what arguments to this function return the following value?"
// This lets you ask questions like like "what pairs of numbers sum to 12," which
// if you allow for negative numbers has an infinite number of solutions.
//
// Some mind bending miniKanren programs that others have written include: a
@davidbalbert
davidbalbert / AppKitAutoscroll.swift
Last active April 4, 2024 16:31
Classic AppKit autoscroll
import Cocoa
class MyAutoscrollingView: NSView {
override func mouseDown(with event: NSEvent) {
// mouseDown-specific logic here
var mouseEvent = event
var done = false
NSEvent.startPeriodicEvents(afterDelay: 0.1, withPeriod: 0.05)
@davidbalbert
davidbalbert / gist:6815258
Last active February 29, 2024 16:12
How to install custom SSL certificates on an ASUS RT-N66U running asuswrt-merlin
###########################################
# IMPORTANT NOTE:
#
# As of asuswrt-merlin 380.67 Beta, you
# can now configure SSL certificates from
# the Webui, making these instructions
# unnecessary.
###########################################
@davidbalbert
davidbalbert / UTTypeGraph.swift
Last active February 4, 2024 17:54
UTType graphviz
import UniformTypeIdentifiers
// As of macOS 14.3
extension UTType {
static var allTypes: [UTType] {
[
.item,
.content,
.compositeContent,
.diskImage,
//
// OnWindowClose.swift
//
// Created by David Albert on 12/16/23.
//
import SwiftUI
import AppKit
struct WindowReader: NSViewRepresentable {
@davidbalbert
davidbalbert / BTree.swift
Created December 11, 2023 17:47
Partially working BigIndexSet + LineCache
extension Range where Bound == Int {
init<Summary>(_ range: Range<BTreeNode<Summary>.Index>, in root: BTreeNode<Summary>) where Summary: BTreeSummary {
range.lowerBound.validate(for: root)
range.upperBound.validate(for: root)
self = range.lowerBound.position..<range.upperBound.position
}
}
@davidbalbert
davidbalbert / Chunk.swift
Created November 28, 2023 21:32
Original fixup(withPrevious: Chunk)
extension Chunk {
mutating func fixup(withPrevious prev: Chunk) -> Bool {
var i = string.startIndex
var first: String.Index?
var old = startBreakState
var new = prev.endBreakState
startBreakState = new
#!/bin/sh
# For Ubiquiti EdgeOS
#
# This script creates a directory /config/scripts/post-commit.d. Any script you put
# in that directory will get run every time you commit a new configuration.
#
# Installation:
# - Put this script in /config/scripts/post-config.d. Note that this is a different
# directory from the directory this creates.