Skip to content

Instantly share code, notes, and snippets.

@thara
Last active April 10, 2020 14:51
Show Gist options
  • Save thara/1b0e66f01170d78c3ca94bdd287289e1 to your computer and use it in GitHub Desktop.
Save thara/1b0e66f01170d78c3ca94bdd287289e1 to your computer and use it in GitHub Desktop.
Read every character from stdin
import Foundation
let stdin = FileHandle.standardInput
var term = termios()
tcgetattr(stdin.fileDescriptor, &term)
term.c_lflag &= ~(UInt(ECHO | ICANON)) // Noecho & Noncanonical
tcsetattr(stdin.fileDescriptor, TCSAFLUSH, &term);
defer {
tcsetattr(stdin.fileDescriptor, TCSAFLUSH, &term);
}
var char: UInt8 = 0
while read(stdin.fileDescriptor, &char, 1) == 1 {
if char == 0x04 { // detect EOF (Ctrl+D)
break
}
print(char)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment