Skip to content

Instantly share code, notes, and snippets.

View avaidyam's full-sized avatar

Aditya Vaidyam avaidyam

View GitHub Profile
@avaidyam
avaidyam / daemon.sh
Created December 27, 2016 02:22
Daemonize any tool!
#!/bin/bash
# modify this command to select what tool to daemonize.
CMD=top
# daemonizer:
case "$1" in
start) # start daemon
if [ -e "daemon.lock" ];
@avaidyam
avaidyam / Binding.swift
Last active June 28, 2017 10:09
A test of a KVO-based bindings replacement.
import Foundation
/// A `Binding` connects two objects' properties such that if one object's property
/// value were to ever update, the other object's property value would do so as well.
/// Thus, both objects' properties are kept in sync. The objects and their properties
/// need not be the same, however, their individual properties' types must be the same.
public class Binding<T: NSObject, U: NSObject, X, Y> {
/// Describes the initial state the `Binding` should follow. That is, upon creation
/// whether to set the "left hand side" object's value to the "right hand side"'s
@avaidyam
avaidyam / KeyValueCoderTests.swift
Last active May 29, 2021 13:46
Key-Value Encoding in Swift 4
import Foundation
import XCTest
class TestKeyValueEncoder : XCTestCase {
func testEncodingTopLevelEmptyStruct() {
let empty = EmptyStruct()
_testRoundTrip(of: empty)
}
@avaidyam
avaidyam / Animatable.swift
Created August 22, 2017 00:09
A quick test of a declarative? animation interface.
import Cocoa
import QuartzCore
// CAMediaTiming stuff?
// custom interps?
// isAdditive, isCumulative, timingFunction
// auto-fill-forward without keeping anim
// auto-fill-backward before start of anim
// delegate + removedOnCompletion ignored in groups...
// - use CACurrentMediaTime() and beginTime offset for everything
@avaidyam
avaidyam / people_pa.js
Created January 15, 2018 04:04
Google People API v2 (Internal) Reference
{
kind: "discovery#restDescription",
name: "people_pa",
version: "v2",
rootUrl: "https://people-pa.googleapis.com/",
servicePath: "",
batchPath: "batch",
id: "people_pa:v2",
parameters: {
"$.xgafv": {
@avaidyam
avaidyam / async.swift
Created January 29, 2018 04:10
Swift (naive) partial application for async functions
import Dispatch
/// Manages async state for a function from its exterior.
fileprivate final class AsyncSlot<T> {
private let semaphore = DispatchSemaphore(value: 0)
private var value: T! = nil
fileprivate func fill(_ value: T) {
self.value = value
self.semaphore.signal()
@avaidyam
avaidyam / Example.m
Last active February 26, 2018 00:56
Automatically map between an NSObject and a dictionary representation, including custom properties and excluding cyclic ones.
@implementation ExampleClass
+ (void)load {
NSArray *ignoreList = [NSArray arrayWithObjects:
[[NSIgnoredClassKey alloc] initWithClassName:@"GPBOneofDescriptor" key:@"containingOneof"],
nil];
NSArray *includeList = [NSArray arrayWithObjects:
[[NSIncludedClassKey alloc] initWithClassName:@"GPBEnumDescriptor" key:@"namedValues" block:^(id obj)
{
#pragma clang diagnostic push
@avaidyam
avaidyam / RegisterEventHotKey.c
Created March 10, 2018 02:08
The decompiled and cleaned source for RegisterEventHotKey and SCRKeyboardRegisterForHotKeysWithConnection.
class HotInputDataUnknown1 {
0x4
0x8
0x10
}
class HotInputData {
UInt32 unknown; // + 0x0
UInt32 modifiers; // + 0x4
UInt32 keyCode; // + 0x8
@avaidyam
avaidyam / runtime-class.m
Created March 11, 2018 19:53
An attempt at creating a subclass of an unknown/hidden NSProxy type.
func test() {
// Allocate a new class-pair from an unknown superclass type.
let selector = Selector(("doStuff"))
let oldClass_: AnyClass = Something.self
let newClass_: AnyClass = objc_allocateClassPair(NSClassFromString("_NSObjectAnimator"), "MyNewClass", 0)!
// Copy instance and class methods over.
let m = class_getInstanceMethod(oldClass_, selector)!
class_addMethod(newClass_, selector, method_getImplementation(m), method_getTypeEncoding(m))
@avaidyam
avaidyam / animate_dark.swift
Last active February 11, 2022 14:20
Animated dark mode transition
// swiftc this file & mv to /usr/local/bin
import Cocoa
typealias NSGlobalPreferenceTransitionBlock = @convention(block) () -> Void
@objc protocol NSGlobalPreferenceTransitionProtocol: NSObjectProtocol {
static func transition() -> AnyObject
func waitForTransitionWithCompletionHandler(_ arg1: @escaping NSGlobalPreferenceTransitionBlock)
func postChangeNotification(_ arg1: UInt64, completionHandler arg2: @escaping NSGlobalPreferenceTransitionBlock)
}
let NSGlobalPreferenceTransition: AnyClass = NSClassFromString("NSGlobalPreferenceTransition")!