Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@humblehacker
humblehacker / next-karabiner-profile.sh
Created January 14, 2024 18:05
Choose next Karabiner profile
#!/usr/bin/env zsh
alias kb="/Library/Application\ Support/org.pqrs/Karabiner-Elements/bin/karabiner_cli"
# Get the list of items
IFS=$'\n' items=($(kb --list-profile-names))
# Get the current item
current_item=$(kb --show-current-profile-name)
@humblehacker
humblehacker / make-opencv2-xcframework.md
Last active January 22, 2024 07:07
Make opencv2.xcframework for iOS arm64, and iOSSimulator arm64 & x86_64

I had some trouble attempting to build an XCFramework of OpenCV, but I finally got a successful build with the following steps:

  1. Clone the OpenCV repo:
    git clone git@github.com:opencv/opencv.git
    cd opencv
    
  2. Disable building with libjpeg-turbo[^1] by applying a patch:
    git apply path/to/opencv-5-28-50.patch
@humblehacker
humblehacker / mutablelivedata.md
Last active March 25, 2023 02:14
Don't expose MutableLiveData
@humblehacker
humblehacker / Kotlin generic type constraints are ignored when the inferred type is an interface.md
Last active December 18, 2022 17:37
Kotlin generic type constraints are ignored when the inferred type is an interface.md

tl;dr: I expect the following code to fail to compile as NotABlob fails the Blob type constraint:

open class Blob
interface NotABlob

fun <T: Blob> foo(): T {
   TODO()
}
@humblehacker
humblehacker / kotlin-inject-composable-viewmodel.md
Last active March 17, 2022 13:56
Injecting view models into Composable functions with kotlin-inject

According to the docs, the non-DI way to associate an view model with an @Composable is via the viewModel() function from androidx.lifecycle.viewmodel.compose.viewModel, like this:

@Composable
fun ProfileScreen(val viewModel: ProfileViewModel = viewModel() {
  ...

But if your view model requires dependencies, you have to pass a subclass of ViewModelProvider.Factory that holds the dependency and knows how to construct the view model for you. Like this:

@humblehacker
humblehacker / CVPixelBufferDeepCopy.swift
Last active February 13, 2022 20:35
Creates a deep copy of a CVPixelBuffer. Compatible with Swift 2.3.
extension CVPixelBuffer
{
/// Deep copy a CVPixelBuffer:
/// http://stackoverflow.com/questions/38335365/pulling-data-from-a-cmsamplebuffer-in-order-to-create-a-deep-copy
func copy() -> CVPixelBuffer
{
precondition(CFGetTypeID(self) == CVPixelBufferGetTypeID(), "copy() cannot be called on a non-CVPixelBuffer")
var _copy: CVPixelBuffer?
@humblehacker
humblehacker / Sequence_associateBy.swift
Created June 27, 2017 23:11
Swift 3 implementation of Kotlin's associateBy for transforming a Sequence to a Dictionary
public
extension Sequence
{
/// Returns a Dictionary using the result of `keySelector` as the key, and the result of `valueTransform` as the value
public func associateBy<T, K: Hashable, V>(_ keySelector: (T) -> K, _ valueTransform: (T) -> V) -> [K:V] where T == Iterator.Element
{
var dict: [K:V] = [:]
for element in self {
dict[keySelector(element)] = valueTransform(element)
}
@humblehacker
humblehacker / LaunchVSMac.applescript
Last active June 26, 2020 00:11
Given an absolute path, line number, and column number, launch Visual Studio for Mac and go there.
on run {filepath, linenumber, columnnumber}
tell application "Visual Studio" to activate
tell application "System Events"
-- ⌘O File → Open...
keystroke "o" using command down
delay 1
@humblehacker
humblehacker / George
Created November 12, 2019 23:25
George holder
george
@humblehacker
humblehacker / swiftui-prefs.swift
Created January 2, 2020 21:42
Demonstrates setting a preference based on an action
import SwiftUI
struct ContentView: View {
var body: some View {
WrapperView {
VStack {
PrefView()
PrefView()
}
}