Skip to content

Instantly share code, notes, and snippets.

View drewmccormack's full-sized avatar

Drew McCormack drewmccormack

View GitHub Profile
@drewmccormack
drewmccormack / email-list-set-operations.sh
Created January 18, 2018 10:20
Common set operations to perform on email lists. Assumes one email per line, and handles case insensitivity.
# Enter two file names as arguments.
# Will print out the ones that appear in both files.
# This is a set intersection.
emails-common-to-both () {
comm -12 -i <(sort -u -f "$1") <(sort -u -f "$2");
};
# Enter two filenames.
# Will print emails from first that don't appear in second.
# This is like a set subtraction.
@drewmccormack
drewmccormack / SimpleStruct.swift
Created February 2, 2019 16:39
Simple example of multi-threading with mutable struct.
import Foundation
struct Small {
var i: Int
var j: Int
}
var s = Small(i: 1, j: 2)
DispatchQueue.global(qos: .background).async {
@drewmccormack
drewmccormack / MVVMSwiftUI.swift
Created February 25, 2020 18:39
Simple example of MMVM with SwiftUI.
import Foundation
import SwiftUI
import Combine
let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return dateFormatter
}()
@drewmccormack
drewmccormack / extract-glossaries.py
Created February 19, 2020 14:21
Extracts terms from Apple lg glossary files, combining into a JSON file
#!/usr/bin/env python
import sys, os.path
import os
import json
from xml.dom.minidom import parse
rootdir = os.getcwd()
lgPaths = ["iOS/AuthKitUI.lg", "iOS/CalendarUIKit.lg", "iOS/iCloudDriveSettings.lg", "iOS/MobileNotes.lg", "macOS/CalendarUI.lg", "macOS/Finder_FE.lg", "macOS/Finder_FinderKit.lg", "macOS/iCloudPrefPane.lg", "macOS/Notes.lg", "macOS/Reminders.lg", "macOS/TextEdit.lg"]
languageDirs = ["Dutch", "French"]
Here is some SwiftUI code I have.
------
NavigationView {
List(translations) { translation in
Button(action: {
self.dataSource.selectedTranslationId = translation.id
}) {
TranslationCell(translation: translation)
}
}
@drewmccormack
drewmccormack / LetTheCoWGo.swift
Created February 2, 2019 10:25
Demonstrates how a Swift value constant can mutate when using Copy-on-Write (CoW) and multi-threading.
import Foundation
struct NuclearPowerStationOperator {
class Storage {
var turnOffCores: Bool = false
func copy() -> Storage {
let new = Storage()
new.turnOffCores = turnOffCores
return new
@drewmccormack
drewmccormack / MultidimensionalOptionalFlatMap.swift
Created November 17, 2017 17:14
Use flatMap with multiple optional variables, ensuring all are non-nil before making a function call.
func flatMap<U, V, T>(_ i: U?, _ j: V?, block: (U, V)->T) -> T? {
var result: T?
if let i = i, let j = j { result = block(i,j) }
return result
}
func flatMap<U, V, W, T>(_ i: U?, _ j: V?, _ k: W?, block: (U, V, W)->T) -> T? {
var result: T?
if let i = i, let j = j, let k = k { result = block(i,j,k) }
@drewmccormack
drewmccormack / verifyPaddleSignature.swift
Last active March 13, 2021 20:27
How to verify the Paddle webhook signature in Swift 3 using the Vapor packages CTLS and Crypto (include OpenSSL)
/// Verify the signature passed with the Paddle request parameters.
/// Details here: https://paddle.com/docs/reference-verifying-webhooks
func verifyPaddleSignature(inParameters parameters: [String:String]) throws -> Bool {
guard let signatureString = parameters[PaddleParameter.signature.rawValue],
let signature = Data(base64Encoded: signatureString) else {
return false
}
// Need to gather sorted parameters
var signatureParameters = parameters
@drewmccormack
drewmccormack / jiggle-wifi.sh
Last active March 13, 2021 20:27
Keeps your OS X WiFi connected to the interwebs.
#!/usr/bin/env bash
# If your OS X WiFi connection stops working, but can be fixed by
# toggling WiFi off and then on, this script can help prevent mouse arm.
# Pings Apple's server repeatedly, toggling WiFi off/on when a connection times out.
while true ; do
curl --head --silent --connect-timeout 10 http://www.apple.com/my-wifi-keeps-dropping-out > /dev/null
if [ $? -ne 0 ] ; then
networksetup -setairportpower en1 off
networksetup -setairportpower en1 on
@drewmccormack
drewmccormack / gist:250542102aa77d700195
Created February 25, 2015 14:40
Determine if a CGImage is fully opaque
BOOL CGImageIsOpaque(CGImageRef image)
{
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
unsigned char pixelData[width * height];
CGContextRef context = CGBitmapContextCreate( pixelData, width, height, 8, width, NULL, (kCGBitmapAlphaInfoMask & kCGImageAlphaOnly) );
CGContextDrawImage( context, CGRectMake(0, 0, width, height), image );
CGContextRelease( context );
for (NSInteger i = 0; i < width * height; ++i) {