Skip to content

Instantly share code, notes, and snippets.

@adam-zethraeus
adam-zethraeus / vpn.sh
Last active January 8, 2022 22:46 — forked from skrajewski/README.md
A cli for launching/stopping Global Protect (and stopping it from auto-launching on restart)
#!/bin/bash
function setup() {
sudo mv /Library/LaunchDaemons/com.paloaltonetworks.gp.pangpsd.plist /Library/Application\ Support/PaloAltoNetworks/GlobalProtect/
sudo mv /Library/LaunchAgents/com.paloaltonetworks.gp.pangps.plist /Library/Application\ Support/PaloAltoNetworks/GlobalProtect/
sudo mv /Library/LaunchAgents/com.paloaltonetworks.gp.pangpa.plist /Library/Application\ Support/PaloAltoNetworks/GlobalProtect/
}
function up() {
@adam-zethraeus
adam-zethraeus / CachedValue.swift
Last active December 11, 2021 10:04
Strongly Typed UserDefaults
import Foundation
/// `CachedValue` provides a strongly typed interface to UserDefaults.
/// It can cache any ``Codable`` type.
///
/// Usage:
/// 1. Make a struct conforming to `CachedValue`
/// * Add a `typealias ValueType = YourType`
/// * Optionally add a `static var defaultValue: YourType = YourInstance`. (Your `value` accessor will then be non-optional.)
/// * Optionally add a `static var cacheKey: String = "a-very-specific-string-key"`. (By default your key will be your CachedValue conforming type's name.)
@adam-zethraeus
adam-zethraeus / StartupTimer.swift
Created December 18, 2021 11:30
StartupTimer.swift
import Foundation
import QuartzCore
import os.log
extension OSLog {
private static var subsystem = Bundle.main.bundleIdentifier!
fileprivate static let osLogCategoryString = "StartupTimerEvents"
static let startupTimer = OSLog(subsystem: subsystem, category: osLogCategoryString)
}
@adam-zethraeus
adam-zethraeus / AnySubject.swift
Last active January 1, 2022 20:57
AnySubject — Type Erasure for Combine's Subject protocol / CurrentValueSubject / PassthroughSubject
import Combine
import Foundation
public final class AnySubject<Output, Failure: Error>: Subject {
public typealias Output = Output
private let valueFunc: (Output) -> ()
private let completionFunc: (Subscribers.Completion<Failure>) -> ()
private let subscriptionFunc: (Subscription) -> ()
@adam-zethraeus
adam-zethraeus / UIResponderPublisherSwizzle.swift
Last active January 10, 2022 03:50
UIResponderPublisher
import Combine
import UIKit
extension UIResponder {
static let firstResponderPublisherSwizzle: Void = {
guard let originalMethod = class_getInstanceMethod(UIResponder.self, #selector(becomeFirstResponder)),
let swizzledMethod = class_getInstanceMethod(UIResponder.self, #selector(swizzled_becomeFirstResponder))
else { return }
method_exchangeImplementations(originalMethod, swizzledMethod)
}()
@adam-zethraeus
adam-zethraeus / If.swift
Created January 8, 2022 22:43
(Flat)Mappable Boolean
import Foundation
public struct If {
let ifTrue: Optional<Void>
public init(_ condition: @autoclosure () -> Bool) {
self.ifTrue = condition() ? .some(()) : .none
}
public func map<T>(_ transform: () -> T) -> T? {
@adam-zethraeus
adam-zethraeus / RandomPastelUIColor.swift
Last active March 7, 2023 09:28
UIColor extension to generate a random pastel
import UIKit
public extension UIColor {
private static var randomLightness: CGFloat {
Double.random(in: 0...1)
}
private static var randomRGB: UIColor {
UIColor(
@adam-zethraeus
adam-zethraeus / GetOrSet.swift
Created January 10, 2022 03:50
'GetOrSet' accessors for optionals and dictionaries
import Foundation
public extension Optional {
mutating func getOrSet(builder: () -> Wrapped) -> Wrapped {
switch self {
case .some(let wrapped):
return wrapped
case .none:
let value = builder()
self = .some(value)
@adam-zethraeus
adam-zethraeus / zoxide_subdir.zsh
Last active February 17, 2022 22:39
zoxide: cd into matching subdirectory
#!/bin/zsh
function zoxide_print_subdir {
echo "$(zoxide query "$(pwd)" "$@")"
}
function zoxide_subdir {
eval 'cd "$(zoxide_print_subdir "$@")"'
}
@adam-zethraeus
adam-zethraeus / unln.sh
Last active February 17, 2022 22:34
cd into un-symlinked path for current directory
#!/bin/bash
function unln {
eval 'cd "$(pwd -P)"'
}