Skip to content

Instantly share code, notes, and snippets.

@epohs
Last active July 7, 2020 01:23
Show Gist options
  • Save epohs/a773be18828e613a23d8 to your computer and use it in GitHub Desktop.
Save epohs/a773be18828e613a23d8 to your computer and use it in GitHub Desktop.
How to setup and use ncurses in a Swift script
#!/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