Skip to content

Instantly share code, notes, and snippets.

View jpmhouston's full-sized avatar

Pierre Houston jpmhouston

View GitHub Profile
@jpmhouston
jpmhouston / HyperlinkTextField.swift
Last active April 7, 2024 14:39
HyperlinkTextField
//
// HyperlinkTextField.swift
// Cleepp
//
// Created by Pierre Houston on 2024-03-28.
// Copyright © 2024 Bananameter Labs. All rights reserved.
//
// Based on snippits and tips from
// https://stackoverflow.com/a/56854375/592739
// https://gist.github.com/mminer/597c1b2c40adcf3c319f7feeade62ed4
@jpmhouston
jpmhouston / HyperlinkTextView.swift
Last active April 7, 2024 14:43
HyperlinkTextView
//
// BareTextView.swift
// Cleepp
//
// Created by Pierre Houston on 2024-03-27.
// Copyright © 2024 Bananameter Labs. All rights reserved.
//
// Based on snippits from https://stackoverflow.com/a/56854375/592739
// and https://stackoverflow.com/a/14469815/592739
// and https://gist.github.com/mminer/597c1b2c40adcf3c319f7feeade62ed4
@jpmhouston
jpmhouston / ContainsInverted.swift
Last active April 19, 2024 18:35
Swift methods implementing the inverse of contains()
// Insired by https://forums.swift.org/t/iscontained-in-array-extension-of-equatable/20223/28
// Exercise for the reader: define operators ∈ and ∉
//
// Created primarily for excluded, which I wanted to use in an
// expression like:
// `let foos = things.compactMap({ $0.foo?.excluded(from: [a,b,c]) })`
// I then went on to generalize to included(in:) and all the
// variations below, and found an even better way to write similar
// expressions: `let x = y.excluding([u,v,w])`
//
@jpmhouston
jpmhouston / selection.fish
Created August 24, 2020 01:10
Fish shell function that outputs linefeed delimited paths of the finder selection
# Useful to refer to finder selections from the fish shell command line, like:
# $ file (selection)
# /Users/me/Documents/Files/Throwback.dvtcolortheme: XML 1.0 document text, ASCII text
# /Users/me/Documents/Files/Test.sparsebundle/: directory
#
function selection
osascript -e "tell application \"Finder\" to set s to the selection as alias list" -e "repeat with f in s" -e "set contents of f to POSIX path of f" -e "end repeat" -e "set AppleScript's text item delimiters to linefeed" -e "s as string"
end
@jpmhouston
jpmhouston / Result+succeeded.swift
Created July 4, 2020 05:59
Helpful Result extension functions failed() and suceeded() which return an optional of either the failure error or success value
extension Result {
func failed(else handleSuccess: ((Success) -> Void)? = nil) -> Failure? {
switch self {
case .success(let value):
handleSuccess?(value)
return nil
case .failure(let error):
return error
}
}
@jpmhouston
jpmhouston / gist:db1fa96997cef811bc3251b6d50e99d6
Created January 30, 2019 18:20
Data extension method hexEncodedString
extension Data {
struct HexEncodingOptions: OptionSet {
let rawValue: Int
static let uppercase = HexEncodingOptions(rawValue: 0)
static let lowercase = HexEncodingOptions(rawValue: 1 << 0)
}
func hexEncodedString(options: HexEncodingOptions = []) -> String {
let hexDigits = Array((options.contains(.lowercase) ? "0123456789abcdef" : "0123456789ABCDEF").utf16)
var dump: [unichar] = []
var ascii: [unichar] = []
@jpmhouston
jpmhouston / gist:de82ffa91a9fdb026b39bc6ad65c7c0c
Created May 11, 2017 22:12
convert TimeInterval to DispatchTimeInterval
public extension TimeInterval {
public var dispatchInterval: DispatchTimeInterval {
let microseconds = Int64(self * TimeInterval(USEC_PER_SECS)) // perhaps use nanoseconds, though would more often be > Int.max
return microseconds < Int.max ? DispatchTimeInterval.microseconds(Int(microseconds)) : DispatchTimeInterval.seconds(Int(self))
}
}
@jpmhouston
jpmhouston / ColorPHTextField.swift
Last active September 15, 2016 23:52
UITextField subclass adding placeholderColor property
// TODO: link here to SO post that inspired this
// ^ that source worked fine when setting placeholderColor sometime after
// placeholder or attributedPlaceholder, but not the other way around.
// this is an attempt to fix this, it might work.
class ColorPHTextField : UITextField {
@IBInspectable var placeholderColor: UIColor? {
didSet {
applyPlaceholderColor()
@jpmhouston
jpmhouston / gist:f25d560cbd771d0f74f3d7b2d0ce6fbd
Last active August 8, 2016 20:20
Swift Array element(at:), why is this missing from the standard library?
extension Array {
func element(at index: Int) -> Element? {
return index >= 0 && index < count ? self[index] : nil
}
}
@jpmhouston
jpmhouston / NSManagedObject+JPHClone.h
Last active May 19, 2017 18:46
NSManagedObject+JPHClone
//
// NSManagedObject+Clone.h
// TResActivityLog
//
// Created by Pierre Houston on 2016-05-30.
// Copyright © 2016 Resilience software. All rights reserved.
//
// When no context provided, uses [NSManagedObjectContext MR_defaultContext].
// Note, I was going to add unique prefix "jph_" to these category method names,
// but currently I've left those out.