Last active
July 7, 2020 01:23
-
-
Save epohs/a773be18828e613a23d8 to your computer and use it in GitHub Desktop.
How to setup and use ncurses in a Swift script
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
#!/usr/bin/env xcrun swift -i | |
import Foundation | |
import Darwin.ncurses | |
initscr() // Init window. Must be first | |
cbreak() | |
noecho() // Don't echo user input | |
nonl() // Disable newline mode | |
intrflush(stdscr, true) // Prevent flush | |
keypad(stdscr, true) // Enable function and arrow keys | |
curs_set(1) // Set cursor to invisible | |
addstr("Press 'q' to Quit.") | |
move(5, 0) | |
addstr("Hello") | |
move(5, 5) | |
addstr(" World!") | |
refresh() | |
// Wait for user input | |
// Exit on 'q' | |
while true { | |
switch getch() { | |
case Int32(UnicodeScalar("q").value): | |
endwin() | |
exit(EX_OK) | |
default: true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment