Skip to content

Instantly share code, notes, and snippets.

@kirkbyo
kirkbyo / DynamicSpacerHeightLayoutManager.swift
Created January 16, 2022 20:57
Layout according to offset without having elements overlap. Helper for SwiftUI implementation
class DynamicSpacerHeightLayoutManager<ID: Equatable & Hashable>: ObservableObject {
struct Element {
let offset: CGFloat
let height: CGFloat
}
@Published private var orderedByOffset = OrderedDictionary<ID, Element>()
// insert: O(n) <- can be optimized further
// append: O(1)
// update offset: O(n) <- can be optimized furhter
@kirkbyo
kirkbyo / custom-nstextblock-bug.swift
Created July 15, 2021 19:53
NSTextView AppKit, custom subclass of NSTextBlock bug
import SwiftUI
struct ContentView: View {
var body: some View {
NativeEditor()
.frame(maxWidth: 400, maxHeight: 600)
}
}
struct NativeEditor: NSViewRepresentable {
function I = simp(f, a, b, n)
I = 0;
dx = (b-a) / n;
for i = 1:2:n
xi = a + (i - 1) * dx;
xi_1 = a + (i * dx);
xi_2 = a + (i + 1) * dx;
I = I + (1/3) * (f(xi) + 4 * f(xi_1) + f(xi_2)) * dx;
end
@kirkbyo
kirkbyo / LeftAlignedCollectionView.swift
Created November 12, 2018 15:44
Left Aligned UICollectionView
class LeftAlignedCollectionViewLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElements(in: rect)
var leftMargin = sectionInset.left
var maxY: CGFloat = -1.0
attributes?.forEach { layoutAttribute in
guard layoutAttribute.representedElementCategory == .cell else { return }
if layoutAttribute.frame.origin.y >= maxY {
extension Dictionary where Value: Hashable {
/// Groups keys with common values together. Example:
/// ```
/// let groceryList = ["Apple": 2, "Oranges": 3, "Pears": 2]
/// groceryList.keysGroupedByValue
/// // => [2: ["Apple", "Pears"], 3: ["Oranges"]]
/// ```
public var keysGroupedByValue: [Value: [Key]] {
var commonalities: [Value: [Key]] = [:]
for (key, value) in self {
@kirkbyo
kirkbyo / .bash_profile
Last active August 31, 2021 22:51
Terminal Setup
# Output color
export CLICOLOR=1
export LSCOLORS=dxfxcxdxbxegedabagacad
# Prompt Color
PS1='\[\e[0;32m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[1;32m\]\$\[\e[m\] \[\e[1;37m\]'
alias atom="open -a atom.app"
alias grm='git checkout master && git pull origin master && git checkout - && git rebase master'
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Other -->
<meta charset="utf-8">
<meta name="description" content="This is an example of a meta description. This will often show up in search results.">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="main.css">
</head>
<body>
@kirkbyo
kirkbyo / ioslocaleidentifiers.tsv
Last active May 5, 2017 22:53 — forked from jacobbubu/ioslocaleidentifiers.csv
iOS Locale Identifiers
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
mr Marathi
bs Bosnian
ee_TG Ewe (Togo)
ms Malay
kam_KE Kamba (Kenya)
mt Maltese
ha Hausa
es_HN Spanish (Honduras)
ml_IN Malayalam (India)
ro_MD Romanian (Moldova)
@kirkbyo
kirkbyo / colors.txt
Last active April 26, 2017 03:19
Collection of colors from https://www.materialui.co/colors
Hex format:
['#f44336', '#E91E63', '#9C27B0', '#673AB7', '#3F51B5', '#2196F3', '#03A9F4', '#00BCD4', '#009688', '#4CAF50', '#8BC34A', '#CDDC39', '#FFEB3B', '#FFC107', '#FF9800', '#FF5722', '#795548', '#9E9E9E', '#607D8B', '#ffebee', '#FCE4EC', '#F3E5F5', '#EDE7F6', '#E8EAF6', '#E3F2FD', '#E1F5FE', '#E0F7FA', '#E0F2F1', '#E8F5E9', '#F1F8E9', '#F9FBE7', '#FFFDE7', '#FFF8E1', '#FFF3E0', '#FBE9E7', '#EFEBE9', '#FAFAFA', '#ECEFF1', '#ffcdd2', '#F8BBD0', '#E1BEE7', '#D1C4E9', '#C5CAE9', '#BBDEFB', '#B3E5FC', '#B2EBF2', '#B2DFDB', '#C8E6C9', '#DCEDC8', '#F0F4C3', '#FFF9C4', '#FFECB3', '#FFE0B2', '#FFCCBC', '#D7CCC8', '#F5F5F5', '#CFD8DC', '#ef9a9a', '#F48FB1', '#CE93D8', '#B39DDB', '#9FA8DA', '#90CAF9', '#81D4FA', '#80DEEA', '#80CBC4', '#A5D6A7', '#C5E1A5', '#E6EE9C', '#FFF59D', '#FFE082', '#FFCC80', '#FFAB91', '#BCAAA4', '#EEEEEE', '#B0BEC5', '#e57373', '#F06292', '#BA68C8', '#9575CD', '#7986CB', '#64B5F6', '#4FC3F7', '#4DD0E1', '#4DB6AC', '#81C784', '#AED581', '#DCE775', '#FFF176', '#FFD54F', '#FFB74D', '#FF8A65',
@kirkbyo
kirkbyo / list.py
Last active August 29, 2015 14:28
Writes all files that end with a certain extentsion to a .txt file
import os
# Writes all files that end with a certain extentsion to a .txt file
# Works in both Python 2 and Python 3
extension = ".png"
fileList = os.listdir('Outflow-Icons')
for fichier in fileList[:]: # filelist[:] makes a copy of filelist.
if not(fichier.endswith(extension)):
fileList.remove(fichier)