Skip to content

Instantly share code, notes, and snippets.

View allenhumphreys's full-sized avatar

Allen Humphreys allenhumphreys

  • Seegrid
  • Indianapolis, IN
View GitHub Profile
@allenhumphreys
allenhumphreys / CreateCroppedPixelBufferBiPlanar.c
Last active May 3, 2024 06:58
Functions in Swift/C to Crop and Scale CVPixelBuffers in the BiPlanar formats
CVPixelBufferRef createCroppedPixelBufferBiPlanar(CVPixelBufferRef srcPixelBuffer, CGRect croppingRect, CGSize scaleSize) {
OSType inputPixelFormat = CVPixelBufferGetPixelFormatType(srcPixelBuffer);
assert(inputPixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
|| inputPixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange);
size_t inputWidth = CVPixelBufferGetWidth(srcPixelBuffer);
size_t inputHeight = CVPixelBufferGetHeight(srcPixelBuffer);
assert(CGRectGetMinX(croppingRect) >= 0);
@allenhumphreys
allenhumphreys / FILE+intercept.swift
Last active November 26, 2020 19:10
Intercepting file descriptors such as stderr or stdout
import Darwin
import Foundation
import SystemPackage
/// Utilities for intercepting the contents written to a file descriptor, useful for testing
/// the output of command line utilities or programs that interact heavily with standard out on POSIX systems
extension UnsafeMutablePointer where Pointee == FILE {
/// Convenience method for intercepting the contents of a file descriptor as a String with the specified encoding
func interceptString(encoding: String.Encoding = .utf8, _ intercepted: () -> Void) throws -> String? {
@allenhumphreys
allenhumphreys / main.py
Created June 23, 2021 15:47
Python decorator to inject values into a an azure function trigger entry point
def service_bus_client(_fnc=None, conn_str: str = "", topic_name: str = ""):
def decorate(decorated_func: Callable):
topic_client = AsyncServiceBusTopicClient.from_connection_string(
conn_str=conn_str,
topic_name=topic_name,
)
@wraps(decorated_func)
async def wrapper(*args, **kwargs):
@allenhumphreys
allenhumphreys / FocusTracking.swift
Last active October 11, 2022 19:34
SwiftUI Focus 2-way Binding
protocol FocusTracking<FocusField>: AnyObject {
associatedtype FocusField: Hashable
var focusedField: FocusField? { get set }
}
struct FocusTrackingView<Content, Target: FocusTracking>: View where Content: View, Target.FocusField: Hashable {
@FocusState.Binding var focusedField: Target.FocusField?
let content: Content
let boundTo: Target
@allenhumphreys
allenhumphreys / ContentView.swift
Created October 31, 2022 16:11
SwiftUI Custom Button Implementation
//
// ContentView.swift
// GoGoButtons
//
// Created by Allen Humphreys on 10/31/22.
//
import SwiftUI
extension Color {
@allenhumphreys
allenhumphreys / App.swift
Created May 12, 2023 18:37
Swift 5.9 Observability
import SwiftUI
@available(iOS 9999, *)
@main
struct ObservabilityApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
@allenhumphreys
allenhumphreys / os_log conversion.txt
Created July 13, 2023 20:34
Converting os_log to Logger via RegEx
os_log\(\s*".*"\s*,\nlog: ((self\.)?log), type: \.((fault|error|debug|info))[^\)]*\)
os_log\(\s*".*"\s*,\s*log: ((self\.)?log),\s*type: \.((fault|error|debug|info))[^\)](?!=\n)*\) // figuring out the look ahead. Need to match all non ) that
os_log\(\s*".*"\s*,\s*log: ((self\.)?log),\s*type: \.((fault|error|debug|info))([^\)]?!\)|(.|\n)*
([^\)]?!\)|(.|\n)* // match everything except close parethesis unless the close paren is not followed by a close paren
// paren not preceded by a paren
@allenhumphreys
allenhumphreys / Content.swift
Created August 16, 2023 14:28
SwiftUI Button Action Interposing
// Button styles are necessarily exclusive. If you return a Button from `makeBody`
// the next button style on the style stack
public struct TrackedButtonStyle: PrimitiveButtonStyle {
let action: () -> Void
public func makeBody(configuration: Configuration) -> some View {
Button {
action()
configuration.trigger()
@allenhumphreys
allenhumphreys / BaselineAdjustedVerticallyCenteredLabel.swift
Created July 21, 2024 19:50
A custom layout that allows for a vertically center-aligned text, next to an SF symbol
import SwiftUI
struct ContentView: View {
@AppStorage("showAlignments") var showAlignments = true
@AppStorage("showBorders") var showBorders = true
@State var iconScale: Image.Scale = .large
@State var fontStyle: Font.TextStyle = .body
@State var icon: String = "square.and.arrow.up"
let icons: [String] = ["square.and.arrow.up", "rectangle.and.pencil.and.ellipsis"]
@allenhumphreys
allenhumphreys / EvenlyDividedHStack.swift
Created October 21, 2024 20:33
A custom horizontal stack layout that has different apporptioning behavior than HStack.
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(alignment: .leading, spacing: 10) {
Text("Plain HStack")
HStack(alignment: .lastTextBaseline, spacing: 0) {
Group {
Label {
Text("Filtersaf afdsa aallll.")