Skip to content

Instantly share code, notes, and snippets.

Avatar
👋
Hi!

Sam Soffes soffes

👋
Hi!
View GitHub Profile
@soffes
soffes / fib.rb
Created Mar 3, 2021
Fibonacci without recursion
View fib.rb
# 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 Jan 8, 2021 — forked from loudmouth/Decode Array<Any> and Dictionary<String, Any> Swift
Decode Array<Any> and Dictionary<String, Any> Swift
View Decoders+JSON.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 Oct 26, 2020
Open in Safari UIActivity
View SafariActivity.swift
import UIKit
final class SafariActivity: UIActivity {
// MARK: - Properties
var url: URL?
// MARK: - UIActivity
View ActivityIndicator.swift
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
Open snapshot diffs in Kaleidoscope
View README.md

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:

@−
View canRecordScreen.swift
// 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 Oct 10, 2019
I'm trying to figure out how Xcode formats floats in its XIB/Storyboard XML. So far, I'm stumped.
View test.py
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 Feb 22, 2021 — forked from orta/highlight.swift
Swift implementation to highlight Cocoa UI elements (http://stackoverflow.com/a/25984748/316803)
View highlight.swift
// 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
View Readme.markdown

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 Jul 19, 2017
Vagrantfile for working with Swift
View Vagrantfile
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