Skip to content

Instantly share code, notes, and snippets.

@jbadger3
Created November 29, 2022 21:56
Show Gist options
  • Save jbadger3/1c48673f9f1fd1fe5f79e0b39ef7bbf0 to your computer and use it in GitHub Desktop.
Save jbadger3/1c48673f9f1fd1fe5f79e0b39ef7bbf0 to your computer and use it in GitHub Desktop.
A swift script used to demonstrate stream buffering and flushing
#!/usr/bin/swift
import Foundation
let arguments = CommandLine.arguments
let startingValue = Int(arguments[1])!
var flush = false
if arguments.count == 3 {
flush = arguments[2] == "-f" ? true : false
}
countDown(from: startingValue, flush: flush)
func countDown(from count: Int, flush: Bool = false) {
for i in (1...count).reversed() {
print(i)
if flush {
fflush(stdout)
}
Thread.sleep(forTimeInterval: 1.0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment