Skip to content

Instantly share code, notes, and snippets.


Alexis Gallagher algal

  • San Francisco
View GitHub Profile
algal / ScaleAspectFitImageView.swift
Last active Aug 7, 2022
UIImageView subclass that works with Auto Layout to express its desired aspect ratio
View ScaleAspectFitImageView.swift
import UIKit
// known-good: Xcode 8.2.1
UIImageView subclass which works with Auto Layout to try
to maintain the same aspect ratio as the image it displays.
This is unlike the usual behavior of UIImageView, where the
algal / PrintToStdErr.swift
Last active Jul 27, 2022
print to stderr in Swift 3
View PrintToStdErr.swift
// known-good: Xcode 8, Swift 3
import Foundation
var standardError = FileHandle.standardError
extension FileHandle : TextOutputStream {
public func write(_ string: String) {
guard let data = .utf8) else { return }
algal / Zipping.swift
Created Feb 17, 2019
Zip files on iOS, without using external libraries and without interoperating with the low-level Compression framework
View Zipping.swift
// Zipping.swift
// known-good: Swift 4.2
// Alexis Gallagher
import Foundation
public extension URL {
/// Creates a zip archive of the file or folder represented by this URL and returns a references to the zipped file
algal / nginx-cors.conf
Created Apr 29, 2013
nginx configuration for CORS (Cross-Origin Resource Sharing), with an origin whitelist, and HTTP Basic Access authentication allowed
View nginx-cors.conf
# A CORS (Cross-Origin Resouce Sharing) config for nginx
# == Purpose
# This nginx configuration enables CORS requests in the following way:
# - enables CORS just for origins on a whitelist specified by a regular expression
# - CORS preflight request (OPTIONS) are responded immediately
# - Access-Control-Allow-Credentials=true for GET and POST requests
algal / colorize-emacs.bashsource
Last active May 1, 2022
Setting up truecolor (24 bit color) in emacs in the terminal, under iTerm2,, and others.
View colorize-emacs.bashsource
# sourcing this file will define a bash functions that
# tries to run subsequent calls to emacs with 24 bit color.
# It sets TERM=xterm-emacs-leg if
# - we've created a user-local terminfo record for xterm-emacs-leg, and
# - we're using iTerm2 or something has set COLORTERM=truecolor
# This will cause emacs to use 24 bit color only when it will work,
# inside or outside of tmux. I haven't found a way to auto-detect yet.
View MP4Writer.swift
import Foundation
import AVFoundation
import ImageIO
import MobileCoreServices
import BespokeCore
struct FrameInfo {
var frame:CGImage
var frameDuration:TimeInterval
algal /
Last active Apr 2, 2022
Taking website screenshots, in Chrome or Safari, including simulating iPhones

Taking website screenshots

These are instructions for taking screenshots of an entire webpage, not just the part of the webpage visible in the browser.

Website Screenshots in Safari

This requires Safari 11.3, which comes on macOS 10.3.4.

  1. Open the website in Safari
  2. If needed, go Safari > Preferences > Advanced > Show Develop Menu in Menu Bar
algal / loadPoints.swift
Created Dec 28, 2018
Read vertex positions with Model I/O
View loadPoints.swift
// known-good: Swift 4.2, macOS
import Foundation
import ModelIO
private func LogDebug(_ s:Any) -> Void { print(s) }
private func LogInfo(_ s:Any) -> Void { print(s) }
private func LogError(_ s:Any) -> Void { print(s) }
struct POINT3D {
var x:Float
algal / vmstatistics64.swift
Last active Feb 23, 2022
Get virtual memory usage on iOS or macOS
View vmstatistics64.swift
import Darwin
import Foundation
// known good: Swift 5
// runs on macOS, probably works on iOS (but haven't tried)
/// Wraps `host_statistics64`, and provides info on virtual memory
/// - Returns: a `vm_statistics64`, or nil if the kernel reported an error
algal / PKCS12.swift
Last active Feb 16, 2022
Reading PKCS12 with Swift in Foundation
View PKCS12.swift
// xcode 7.3
import Foundation
Struct representing values returned by `SecPKCS12Import` from the Security framework.
This is what Cocoa and CocoaTouch can tell you about a PKCS12 file.