Last active
April 10, 2020 14:51
-
-
Save thara/1b0e66f01170d78c3ca94bdd287289e1 to your computer and use it in GitHub Desktop.
Read every character from stdin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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