Skip to content

Instantly share code, notes, and snippets.

View helje5's full-sized avatar

Helge Heß helje5

View GitHub Profile
@helje5
helje5 / Tows.swift
Last active June 12, 2022 06:33
An 82-liner SwiftUI script similar to CodeCows 🐮
import SwiftUI
import cows // @AlwaysRightInstitute
struct ContentView: View {
@State var searchString = ""
@State var matches = allCows
@State var selectedCow : String?
let font = Font(NSFont
@helje5
helje5 / cal.swift
Created February 20, 2022 15:13
A completely incomplete implementation of `cal` using Swift / Foundation.Calendar
#!/usr/bin/swift
import Foundation
extension DateInterval {
func containsOpen(_ date: Date) -> Bool { date >= start && date < end }
}
struct MonthCalendar {
@helje5
helje5 / main.swift
Last active January 12, 2022 18:44
Using async/await concurrency on iOS 14 and before
// Created by Helge Heß 2021-06-17
import Foundation
// They use obfuscated names to hide it from us!
import JavaScriptCore
/// Setup our async/await runtime.
let runtime = JSContext()!
@helje5
helje5 / KeyPathMirror.swift
Created January 28, 2018 17:16
Swift Key Path Memory Layout Decoding
import Foundation
// https://github.com/apple/swift/blob/master/docs/ABI/KeyPaths.md
// https://bugs.swift.org/browse/SR-5689
public struct KeyPathMirror<T: AnyKeyPath> : CustomStringConvertible {
public let subject : T
@helje5
helje5 / CliptIt.swift
Created July 17, 2020 15:55
Demo for SwiftBlocksUI: Clip Slack messages using a Message Action
#!/usr/bin/swift sh
import SwiftBlocksUI // @SwiftBlocksUI ~> 0.8.0
dotenv.config()
struct ClipItForm: Blocks {
@State(\.messageText) var messageText
@State var importance = "medium"
@helje5
helje5 / SlashCows.swift
Created July 17, 2020 15:52
ASCII Cows for Slack, as a Slash Command - a SwiftBlocksUI demo
#!/usr/bin/swift sh
import cows // @AlwaysRightInstitute ~> 1.0.0
import SwiftBlocksUI // @SwiftBlocksUI ~> 0.8.0
dotenv.config()
struct CowMessage: Blocks {
@Environment(\.messageText) private var query
@helje5
helje5 / ZzViewHolder.swift
Last active September 13, 2019 01:32
NSDictionaryOfVariableBindings for Swift
/**
* NSDictionaryOfVariableBindings for Swift
*
* In Objective-C you could use macros to quickly get a Dictionary of views,
* keyed on their names in the sourcecode. Like so:
*
* UILabel *label = [UILabel autolayoutView];
* NSDictionary *views = NSDictionaryOfVariableBindings(label);
*
* Which you could then use in, for example, VFL:
@helje5
helje5 / AppDelegate.swift
Last active March 22, 2019 14:10
Dock gets stuck on drag
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window : NSWindow!
@IBOutlet weak var tableView : NSTableView!
func applicationDidFinishLaunching(_ aNotification: Notification) {
@helje5
helje5 / TestView.swift
Created November 27, 2018 09:45
Nested NSStackViews lead to ambiguous layout
import Cocoa
final class TestView : NSView {
static func makeAndAttachToView(_ rootView: NSView) {
let view = TestView()
view.translatesAutoresizingMaskIntoConstraints = false
rootView.addSubview(view)
NSLayoutConstraint.activate([
@helje5
helje5 / InPlaceEnumEditing.swift
Created April 7, 2018 11:20
In-place editing of associated values in enums
struct MyArray<Element> : CustomStringConvertible {
class Storage<T> {
var values = ContiguousArray<T>()
}
var storage = Storage<Element>()
init<T: Sequence>(contentsOf s: T) where T.Element == Element {
for item in s { storage.values.append(item) }