Skip to content

Instantly share code, notes, and snippets.

View Uncommon's full-sized avatar

David Catmull Uncommon

View GitHub Profile
@Uncommon
Uncommon / OneWordTextStorage.swift
Created October 16, 2019 17:09
Custom text storage
// https://stackoverflow.com/a/32671117/310159
/// Abstract custom text storage
class CustomTextStorage: NSTextStorage
{
var contents: NSMutableAttributedString
override var string: String { return contents.string }
override init(attributedString attrStr: NSAttributedString)
@Uncommon
Uncommon / IntAndString.swift
Created July 15, 2019 21:02
Enum backed with Int and String
import Foundation
protocol IntAndString: RawRepresentable where RawValue == (Int, String)
{
static var invalidInt: Int { get }
static var invalidString: String { get }
}
extension IntAndString
{
@Uncommon
Uncommon / XTRepository+Iterators.swift
Created April 25, 2019 17:58
Creating a generic factory
import Foundation
extension XTRepository
{
/// Branches is a sequence, not a collection, because the API does not provide
/// a count or indexed access.
public struct BranchSequence<BranchType: Branch>: Sequence
{
public typealias Element = BranchType
import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
let viewController = ViewController()
let window = NSWindow(contentRect: NSMakeRect(0, 0, NSScreen.main()!.frame.size.width, NSScreen.main()!.frame.size.height), styleMask: [.titled, .closable, .miniaturizable, .resizable], backing: NSBackingStoreType.buffered, defer: false)
func applicationDidFinishLaunching(_ aNotification: Notification) {
viewController.view = NSView(frame: NSMakeRect(0, 0, window.frame.size.width, window.frame.size.height))
viewController.view.wantsLayer = true
@Uncommon
Uncommon / gist:6942068
Created October 11, 2013 21:09
Tinted image
- (NSImage *)tintedImageWithColor:(NSColor *)tint
{
NSSize size = [self size];
NSRect imageBounds = NSMakeRect(0, 0, size.width, size.height);
NSImage *copiedImage = [self copy];
[copiedImage lockFocus];
[tint set];
@Uncommon
Uncommon / base64.m
Created October 6, 2011 21:55
Base64
static char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@implementation Base64
+ (NSString*)
encode:(NSData*)plainText
{
int encodedLength = (((([plainText length] % 3) + [plainText length]) / 3) * 4) + 1;
unsigned char *outputBuffer = malloc(encodedLength);
unsigned char *inputBuffer = (unsigned char*)[plainText bytes];
@Uncommon
Uncommon / uncommon.cfg
Created June 28, 2011 23:16
Uncrustify config for my coding style
# Uncrustify 0.58
#
# General options
#
# The type of line endings
newlines = auto # auto/lf/crlf/cr
# The original size of tabs in the input
@Uncommon
Uncommon / CursorImages.m
Created May 27, 2011 02:59
Write tiffs for all standard NSCursor images
NSArray *cursorNames = [NSArray arrayWithObjects:
@"arrowCursor",
@"IBeamCursor",
@"pointingHandCursor",
@"closedHandCursor",
@"openHandCursor",
@"resizeLeftCursor",
@"resizeRightCursor",
@"resizeLeftRightCursor",
@"resizeUpCursor",