Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Darwin
- var orig_termios = termios()
- func reset_terminal_mode() {
- tcsetattr(0, TCSANOW, &orig_termios)
- }
- func set_conio_terminal_mode() {
- tcgetattr(0, &orig_termios)
- var new_termios = orig_termios
- atexit(reset_terminal_mode)
- cfmakeraw(&new_termios)
- tcsetattr(0, TCSANOW, &new_termios)
- }
- func kbhit() -> Bool {
- var fds = [ pollfd(fd: STDIN_FILENO, events: Int16(POLLIN), revents: 0) ]
- let res = poll(&fds, 1, 0)
- return res > 0
- }
- func getChar() -> Int {
- var c: UInt8 = 0
- let r = read(0, &c, 1)
- if r < 0 {
- return r
- } else {
- return Int(c)
- }
- }
- print("Type anything: ", terminator: "")
- fflush(stdout)
- set_conio_terminal_mode()
- var key: Int
- while true {
- if kbhit() {
- key = getChar()
- if key == 13 { // return key
- break
- } else {
- print(String(UnicodeScalar(key)!)+", ", terminator:"")
- fflush(stdout)
- }
- } else {
- print(".", terminator: "")
- fflush(stdout)
- usleep(10)
- print(".", terminator: "")
- fflush(stdout)
- usleep(10)
- print(".", terminator: "")
- fflush(stdout)
- usleep(10)
- print("\u{1B}[3D", terminator: "")
- fflush(stdout)
- usleep(10)
- }
- }
- print("\r")
- reset_terminal_mode()
Advertisement
Add Comment
Please, Sign In to add comment