Skip to content

Instantly share code, notes, and snippets.

View davidbalbert's full-sized avatar

David Albert davidbalbert

View GitHub Profile
@davidbalbert
davidbalbert / MyAutoscrollingView.swift
Last active March 18, 2024 21:52
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 / 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,
@davidbalbert
davidbalbert / Text+String.swift
Last active January 19, 2024 17:40
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)
//
// 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.
#!/bin/sh
set -e
# NOTE: This is no longer necessary! When the IPv6 rollout started, the issue went away.
# I'm just keeping the script here for posterity.
# Verizon Fios's router on the other end of the ONT (one hop upstream of this router) responds to
# any ICMP echo request with an ICMP echo reply, no matter who the intended host is. This breaks
# any traceroute that uses ICMP, including mtr, making it look like the destination host is one
@davidbalbert
davidbalbert / EmptyContainer.swift
Created July 27, 2023 19:23
Conflicting conformances
import Cocoa
struct EmptyContainer<T> {
}
typealias IntContainer = EmptyContainer<Int>
extension IntContainer: Sequence {
struct Iterator: IteratorProtocol {
func next() -> Int? {
@davidbalbert
davidbalbert / notifier.go
Last active April 22, 2023 15:55
Broadcast notifier sketches
// Lightly modified from the slides for "Rethinking Classical Concurrency Patterns"
package sync
import "context"
type state struct {
seq int64
changed chan struct{} // closed upon notify
}