Skip to content

Instantly share code, notes, and snippets.

dduan / bird.swift
Created Nov 14, 2020
Flappy bird as a CLI app written in Swift.
View bird.swift
// This is the code for the Flappy Bird game running in a Unix terminal.
// Demo:
// To run it, simply do "swift bird.swift" in a Unix command line.
#if canImport(Darwin)
import Darwin
import Glibc
enum RawModeError: Error {
dduan / runInRawMode.swift
Last active Nov 30, 2020
Put terminal in raw mode, clear the screen, run some code. Reset the terminal to original mode when the code finish running.
View runInRawMode.swift
#if canImport(Darwin)
import Darwin
import Glibc
enum RawModeError: Error {
case notATerminal
case failedToGetTerminalSetting
case failedToSetTerminalSetting
dduan / cube.swift
Last active Nov 26, 2020
A rotating 3-D cube in terminal. Written in Swift
View cube.swift
/// A rotating 3-D cube in terminal
/// Only works on macOS
/// Run `swift cube.swift` in a terminal application to run it.
/// For controlling the cube, see comments for `Key` in code.
import Darwin
enum RawModeError: Error {
case notATerminal
case failedToGetTerminalSetting
dduan / EscapingClosurePropertyWrapper.swift
Created Jul 28, 2020
This property wrapper guarantees that a closure does nothing if its enclosing object is deinitialized.
View EscapingClosurePropertyWrapper.swift
struct Escaping {
typealias Closure = () -> Void
var store: Closure
var wrappedValue: Closure {
get { }
set { = newValue }
dduan / UISegmentedControl+VerticalLayout.swift
Last active Oct 2, 2020
Turns a UISegmentedControl into a vertical layout.
View UISegmentedControl+VerticalLayout.swift
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 / XCTest+Eventually.swift
Last active Jul 14, 2020
A simple "eventually" method to aide asynchronous testing with XCTest
View XCTest+Eventually.swift
import XCTest
extension XCTestCase {
/// Simple helper for asynchronous testing.
/// Usage in XCTestCase method:
/// func testSomething() {
/// doAsyncThings()
/// eventually {
/// /* XCTAssert goes here... */
dduan / SpeakBubble.swift
Last active Jun 20, 2020
Twitter's voice tweet UI has an interesting animation on iOS. This is an attempt to recreate that animation with SwiftUI. Looks like this
View SpeakBubble.swift
import SwiftUI
struct ChaoticPhoto: View {
let image: Image
let radius: CGFloat
@Binding var activated: Bool
@State var scale: CGFloat = 1
var body: some View {
dduan /
Created Oct 15, 2019
Run DrString anywhere docker is available!
echo 'FROM swift@sha256:c4d53af406c5dc48bd43c0d313f3ed80924eee4bf78907ce4ad6eb8f5513f376' >> temp_file
echo 'RUN git clone; cd DrString; make build; cp .build/release/drstring /bin/drstring' >> temp_file
echo 'RUN rm -rf /data' >> temp_file
echo 'ADD . /data' >> temp_file
echo 'WORKDIR /data' >> temp_file
echo 'ENTRYPOINT ["drstring"]' >> temp_file
dduan / Makefile
Created Dec 29, 2017
"Hello, World!" In WebAssembly
View Makefile
wat2wasm main.wat -o main.wasm
python -m SimpleHTTPServer
dduan / RemoveDuplicates.swift
Created Jul 30, 2019
Combine framework: remove duplicates by key paths.
View RemoveDuplicates.swift
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]
You can’t perform that action at this time.