Skip to content

Instantly share code, notes, and snippets.

View PadraigK's full-sized avatar

Padraig PadraigK

View GitHub Profile
@krzyzanowskim
krzyzanowskim / StringGetSizeThatFits.swift
Last active November 12, 2023 14:51
Calculate frame of String, that fits given width
// Excerpt from https://github.com/krzyzanowskim/CoreTextWorkshop
// Licence BSD-2 clause
// Marcin Krzyzanowski marcin@krzyzanowskim.com
func getSizeThatFits(_ attributedString: NSAttributedString, maxWidth: CGFloat) -> CGSize {
let framesetter = CTFramesetterCreateWithAttributedString(attributedString)
let rectPath = CGRect(origin: .zero, size: CGSize(width: maxWidth, height: 50000))
let ctFrame = CTFramesetterCreateFrame(framesetter, CFRange(), CGPath(rect: rectPath, transform: nil), nil)
@zonble
zonble / KKSimplePlayer.swift
Created April 21, 2016 16:34
Using AudioQueue and Swift to do a simple stream player
import Foundation
import AudioToolbox
class KKSimplePlayer: NSObject {
var URL: NSURL
var URLSession: NSURLSession!
var packets = [NSData]()
var audioFileStreamID: AudioFileStreamID = nil
var outputQueue: AudioQueueRef = nil
var streamDescription: AudioStreamBasicDescription?
@rgm
rgm / gist:e670f333ae91553031a8
Last active November 10, 2023 21:33
FizzBuzz in the untyped λ-calculus, as approximated by Ruby stabby procs. courtesy of @tomstuart.
-> k { -> f { -> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }]
} [-> f { -> l { -> x { -> g { -> b { b }[-> p { p[-> x { -> y { x } }]
}[l]][x] [-> y { g[f[-> l { -> p { p[-> x { -> y { y } }] }[-> p { p[-> x { ->
y { y } }] } [l]] }[l]][x][g]][-> l { -> p { p[-> x { -> y { x } }] }[-> p {
p[-> x { -> y { y } }] }[l]] }[l]][y] }] } } } }][k][-> x { -> y { -> f {
f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][-> l { -> x { -> l {
-> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { ->
f { f[x][y] } } } [x][l]] } }[l][f[x]] } }] } }[-> f { -> x { f[-> y { x[x][y]
}] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m { ->
n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n {
@farktronix
farktronix / gist:92c3b1a1901eec6c6f2e
Created June 4, 2014 18:04
dispatch_group based state machine
//
// StateMachineExample
//
// Created by Jacob Farkas on 6/4/14.
//
#import <Foundation/Foundation.h>
#define DownloadTempPath "/tmp/tacosXXXXXX"
@prachigauriar
prachigauriar / QLCDispatchAsyncWithCompletionAndTimeout.m
Last active May 28, 2022 18:23
Examples in Objective-C and Swift that use dispatch_async and dispatch_semaphore to cleanly replicate the behavior of ‑[NSCondition waitUntilDate:].
//
// QLCDispatchAsyncWithCompletionAndTimeout.m
// Blocks-WaitUntilDate
//
// Created by Prachi Gauriar on 12/23/2013.
// Copyright (c) 2013 Prachi Gauriar.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
@steipete
steipete / PSPDFUIKitMainThreadGuard.m
Last active May 27, 2024 12:11
This is a guard that tracks down UIKit access on threads other than main. This snippet is taken from the commercial iOS PDF framework http://pspdfkit.com, but relicensed under MIT. Works because a lot of calls internally call setNeedsDisplay or setNeedsLayout. Won't catch everything, but it's very lightweight and usually does the job.You might n…
// Taken from the commercial iOS PDF framework http://pspdfkit.com.
// Copyright (c) 2014 Peter Steinberger, PSPDFKit GmbH. All rights reserved.
// Licensed under MIT (http://opensource.org/licenses/MIT)
//
// You should only use this in debug builds. It doesn't use private API, but I wouldn't ship it.
// PLEASE DUPE rdar://27192338 (https://openradar.appspot.com/27192338) if you would like to see this in UIKit.
#import <objc/runtime.h>
#import <objc/message.h>
@rgreenjr
rgreenjr / postgres_queries_and_commands.sql
Last active July 3, 2024 11:43
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT procpid, age(clock_timestamp(), query_start), usename, current_query
FROM pg_stat_activity
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;
-- show running queries (9.2)
SELECT pid, age(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'