Skip to content

Instantly share code, notes, and snippets.

View mayoff's full-sized avatar
😷
status messages are fun

Rob Mayoff mayoff

😷
status messages are fun
View GitHub Profile
import UIKit
class FloatingButtonController: UIViewController {
private(set) var button: UIButton!
required init?(coder aDecoder: NSCoder) {
fatalError()
}
@mayoff
mayoff / gist:1138816
Last active December 8, 2023 22:00
AppleScript to make Google Chrome open/reload a URL
(* To the extent possible under law, Rob Mayoff has waived all copyright and related or neighboring rights to “AppleScript to make Google Chrome open/reload a URL”. This work is published from: United States. https://creativecommons.org/publicdomain/zero/1.0/ *)
tell application "Google Chrome"
activate
set theUrl to "http://tycho.usno.navy.mil/cgi-bin/timer.pl"
if (count every window) = 0 then
make new window
end if
set found to false
@mayoff
mayoff / minimal-metal.swift
Created December 23, 2017 06:43
Hello, Triangle! (MetalKit + Swift 4)
import Cocoa
import MetalKit
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, MTKViewDelegate {
weak var window: NSWindow!
weak var metalView: MTKView!
let device = MTLCreateSystemDefaultDevice()!
var commandQueue: MTLCommandQueue!
var pipelineState: MTLRenderPipelineState!
@mayoff
mayoff / !README.md
Last active August 14, 2023 15:09
Debugging Objective-C blocks in lldb

The attached lldb command pblock command lets you peek inside an Objective-C block. It tries to tell you where to find the source code for the block, and the values captured by the block when it was created.

Consider this example program:

#import <Foundation/Foundation.h>

@interface Foo: NSObject
@end

@implementation Foo

@mayoff
mayoff / UIBezierPath+forEachElement.h
Created December 2, 2012 06:51
dragging an object along a CGPath on iOS demo
#import <UIKit/UIKit.h>
@interface UIBezierPath (forEachElement)
- (void)forEachElement:(void (^)(CGPathElement const *element))block;
@end
@mayoff
mayoff / UnitPoint.angle.swift
Last active July 16, 2023 15:07
point on unit square at a given angle
import SwiftUI
extension UnitPoint {
/// - returns: The point on the perimeter of the unit square that is at angle `angle` relative to the center of the unit square.
init(_ angle: Angle) {
// Inspired by https://math.stackexchange.com/a/4041510/399217
// Also see https://www.desmos.com/calculator/k13553cbgk
let s = sin(angle.radians)
let c = cos(angle.radians)
@mayoff
mayoff / libfuzzer.md
Created April 8, 2023 16:28 — forked from schwa/libfuzzer.md
Using LLVM's libfuzzer with Swift

Using LLVM's libfuzzer with Swift

Background

Of the many tools available for fuzzing, I've found that LLVM's libfuzzer is the easiest to use for Swift on macOS. Other tools seem to have a list of requirements taht are difficult to meet. libfuzzer on the other hand is built into LLVM and is available on macOS in the custom Swift toolchains: https://www.swift.org/download/

In this document I'll describe how to use libfuzzer with Swift and Swift Packages.

I used this setup to fuzz an SVG Renderer package that I am building. I was able to find and fix a number of bugs in my SVG parsing code using libfuzzer in basically no time at all.

@mayoff
mayoff / SteppingSubscriber.swift
Created December 18, 2019 06:50
Custom Combine operator for stepping a publisher's signals through function
// See https://stackoverflow.com/q/59367202/77567
import Combine
import Foundation
extension Publisher {
func step(with stepper: @escaping (SteppingSubscriber<Output, Failure>.Event) -> ()) -> AnyCancellable {
let subscriber = SteppingSubscriber<Output, Failure>(stepper: stepper)
self.subscribe(subscriber)
return .init(subscriber)
@mayoff
mayoff / makeAnimatedGif.m
Created February 16, 2013 23:00
Example of creating an animated GIF on iOS, with no 3rd-party code required. This should also be easy to port to OS X.
#import <UIKit/UIKit.h>
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
static UIImage *frameImage(CGSize size, CGFloat radians) {
UIGraphicsBeginImageContextWithOptions(size, YES, 1); {
[[UIColor whiteColor] setFill];
UIRectFill(CGRectInfinite);
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(gc, size.width / 2, size.height / 2);