Skip to content

Instantly share code, notes, and snippets.

View soffes's full-sized avatar

Sam Soffes soffes

View GitHub Profile
@soffes
soffes / fib.rb
Created March 3, 2021 02:56
Fibonacci without recursion
# From this delightful video https://www.youtube.com/watch?v=ghxQA3vvhsk
PHI = (1 + Math.sqrt(5)) / 2
def fib(n)
(((PHI**n) - ((-1 / PHI)**n)) / Math.sqrt(5)).to_i
end
(1...10).map { |n| fib(n) }
# => [1, 1, 2, 3, 5, 8, 13, 21, 34]
@soffes
soffes / Decoders+JSON.swift
Last active January 8, 2021 12:51 — forked from loudmouth/Decode Array<Any> and Dictionary<String, Any> Swift
Decode Array<Any> and Dictionary<String, Any> Swift
import Foundation
// Inspired by https://gist.github.com/loudmouth/332e8d89d8de2c1eaf81875cfcd22e24
private struct JSONCodingKeys: CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
@soffes
soffes / SafariActivity.swift
Created October 26, 2020 22:19
Open in Safari UIActivity
import UIKit
final class SafariActivity: UIActivity {
// MARK: - Properties
var url: URL?
// MARK: - UIActivity
import SwiftUI
import UIKit
public struct ActivityIndicator: UIViewRepresentable {
// MARK: - Properties
public let style: UIActivityIndicatorView.Style
@Binding public var isAnimating: Bool
private var color: UIColor?
@soffes
soffes / README.md
Last active May 7, 2020 16:49
Open snapshot diffs in Kaleidoscope

Apparently, SnapshotTesting has this built-in. 🤦


While working with snapshot tests, it can be hard to debug what the problem is. This script allows you to quickly open the diff in a visual tool, Kaleidoscope, to compare the visual changes.

The output of SnapshotTesting when a snapshot doesn’t match looks like this:

@−
// From https://stackoverflow.com/a/58985069/118631
@available(macOS 10.15, *)
func canRecordScreen() -> Bool {
let runningApplication = NSRunningApplication.current
let processIdentifier = runningApplication.processIdentifier
guard let windows = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID)
as? [[String: AnyObject]] else
{
@soffes
soffes / test.py
Created October 10, 2019 17:45
I'm trying to figure out how Xcode formats floats in its XIB/Storyboard XML. So far, I'm stumped.
import re
# I'm trying to figure out how Xcode formats floats in its XIB/Storyboard XML. So far, I'm stumped.
# Data from Xcode
data = values = ["0.0", "0.0039215686274509803", "0.0078431372549019607", "0.011764705882352941", "0.0039215686274509803", "0.019607843137254902", "0.023529411764705882", "0.027450980392156862", "0.031372549019607843", "0.035294117647058823", "0.039215686274509803", "0.043137254901960784", "0.047058823529411764", "0.050980392156862744", "0.054901960784313725", "0.058823529411764705", "0.062745098039215685", "0.066666666666666666", "0.070588235294117646", "0.074509803921568626", "0.078431372549019607", "0.082352941176470587", "0.086274509803921567", "0.090196078431372548", "0.094117647058823528", "0.098039215686274508", "0.10196078431372549", "0.10588235294117647", "0.10980392156862745", "0.11372549019607843", "0.11764705882352941", "0.12156862745098039", "0.12549019607843137", "0.12941176470588234", "0.13333333333333333", "0.13725490196078433", "0.14117647058823529", "0.1450980392
@soffes
soffes / highlight.swift
Last active November 16, 2021 07:34 — forked from orta/highlight.swift
Swift implementation to highlight Cocoa UI elements (http://stackoverflow.com/a/25984748/316803)
// Taken from:
// https://gist.github.com/joelcox/28de2f0cb21ea47bd789
NSColor.selectedMenuItemColor.set()
NSBezierPath(rect: rect).fill()
if rect.height > 1 {
let currentControlTint = NSColor.currentControlTint
let startingAlpha: CGFloat = currentControlTint == .blueControlTint ? 0.16 : 0.09
@soffes
soffes / Readme.markdown
Created July 3, 2018 23:25 — forked from calebd/Readme.markdown
Run Loop Source

CFRunLoopSource is cool. It lets you build behavior similar to the mechanisms that drive setNeedsLayout and setNeedsDisplay in UIKit.

I found myself in need of something like this a couple of times. It's great to know that no matter how many times I say I need to update something, I will get a single callback at the end of the run loop that gives me a chance to perform my work.

Here is a little Swift wrapper that makes the API easier to deal with.


Updated for Swift 4

@soffes
soffes / Vagrantfile
Last active July 19, 2017 01:29
Vagrantfile for working with Swift
Vagrant.configure(2) do |config|
config.vm.box = "boxcutter/ubuntu1610"
config.ssh.pty = true
config.vm.provision "shell", inline: <<-SHELL
su vagrant
cd
sudo apt-get -y update
sudo apt-get install -y git python-dev libcurl3 clang
git clone https://github.com/kylef/swiftenv.git ~/.swiftenv
echo 'export SWIFTENV_ROOT="$HOME/.swiftenv"' >> ~/.bash_profile