Skip to content

Instantly share code, notes, and snippets.

@dduan
dduan / SwiftChartPong.swift
Last active January 19, 2024 13:51
Pong Game implemented with Swift Charts.
import SwiftUI
import Charts
import Combine
import Foundation
final class GameState: ObservableObject {
struct Player {
var position: Int
var halfSize: Int = 150
@dduan
dduan / RemoveDuplicates.swift
Created July 30, 2019 19:00
Combine framework: remove duplicates by key paths.
import Combine
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher {
func removeDuplicates<Property>(by keyPath: KeyPath<Output, Property>)
-> Publishers.RemoveDuplicates<Self> where Property: Equatable
{
return self.removeDuplicates {
$0[keyPath: keyPath] == $1[keyPath: keyPath]
}
@dduan
dduan / Makefile
Created December 29, 2017 04:13
"Hello, World!" In WebAssembly
compile:
wat2wasm main.wat -o main.wasm
serve:
python -m SimpleHTTPServer
@dduan
dduan / UISegmentedControl+VerticalLayout.swift
Last active April 19, 2023 14:50
Turns a UISegmentedControl into a vertical layout.
import UIKit
extension UISegmentedControl {
func goVertical() {
self.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
for segment in self.subviews {
for segmentSubview in segment.subviews {
if segmentSubview is UILabel {
(segmentSubview as UILabel).transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
}
@dduan
dduan / runCommand.swift
Last active April 1, 2023 16:17
How to fork()+execv() in Swift
import Foundation
func withCStrings(_ strings: [String], scoped: ([UnsafeMutablePointer<CChar>?]) throws -> Void) rethrows {
let cStrings = strings.map { strdup($0) }
try scoped(cStrings + [nil])
cStrings.forEach { free($0) }
}
enum RunCommandError: Error {
case WaitPIDError
@dduan
dduan / runInRawMode.swift
Last active February 3, 2023 00:49
Put terminal in raw mode, clear the screen, run some code. Reset the terminal to original mode when the code finish running.
#if canImport(Darwin)
import Darwin
#else
import Glibc
#endif
enum RawModeError: Error {
case notATerminal
case failedToGetTerminalSetting
case failedToSetTerminalSetting
@dduan
dduan / XCTest+Eventually.swift
Last active January 11, 2023 00:26
A simple "eventually" method to aide asynchronous testing with XCTest
import XCTest
extension XCTestCase {
/// Simple helper for asynchronous testing.
/// Usage in XCTestCase method:
/// func testSomething() {
/// doAsyncThings()
/// eventually {
/// /* XCTAssert goes here... */
@dduan
dduan / resize_svg.py
Last active April 15, 2022 19:13
Python script that resizes SVG to new canvas size
"""
Resize a SVG file to a new size. Coordinates of paths and gradient definitions get transposed to corresponding
values in the new canvas. Everything else remain unchanged.
"""
import re
from argparse import ArgumentParser
from xml.etree import ElementTree
@dduan
dduan / Makefile
Created December 29, 2021 08:48
How to link to libdispatch on Linux - an example Makefile
SHELL = /bin/bash
ifeq ($(shell uname),Darwin)
EXTRA_SWIFT_FLAGS = "--disable-sandbox"
else
SWIFT_TOOLCHAIN = "$(shell dirname $(shell swift -print-target-info | grep runtimeResourcePath | cut -f 2 -d ':' | cut -f 2 -d '"'))"
EXTRA_SWIFT_FLAGS = -Xcxx -I${SWIFT_TOOLCHAIN}/swift -Xcxx -I${SWIFT_TOOLCHAIN}/swift/Block
endif
.PHONY: test
@dduan
dduan / QuickOutline.swift
Last active February 10, 2021 09:50
This script takes a JSON text file with content in {"string:["a","b",…]} format and generates Quick/BDD style test outlines.
#!/usr/bin/env xcrun swift
/*
usage:
1. chmod +x QuickOutline.swift
2. ./QuickOutline.swift outline.json
this will generate BDD/[Quick](https://github.com/Quick/Quick) style test outlines according to outline.json
an example outline.json: