Last active
October 9, 2019 18:29
-
-
Save m66n/6186df9d47fd2229ef2a856e4874213e to your computer and use it in GitHub Desktop.
curses window border example
This file contains 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 python3 | |
import curses | |
import sys | |
def create_win(nlines, ncols, begin_y, begin_x): | |
win = curses.newwin(nlines, ncols, begin_y, begin_x) | |
win.border() | |
win.refresh() | |
return win | |
def erase_win(win): | |
win.border(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ') | |
win.refresh() | |
def app(stdscr): | |
curses.curs_set(0) | |
nlines = 3 | |
ncols = 10 | |
begin_y = (curses.LINES - nlines) // 2 | |
begin_x = (curses.COLS - ncols) // 2 | |
message = 'Press F1 to exit' | |
stdscr.addstr(curses.LINES - 1, | |
(curses.COLS - len(message)) // 2, | |
message) | |
stdscr.refresh() | |
win = create_win(nlines, ncols, begin_y, begin_x) | |
ch = stdscr.getch() | |
while ch != curses.KEY_F1: | |
if ch == curses.KEY_LEFT: | |
begin_x = max(0, begin_x - 1) | |
if ch == curses.KEY_RIGHT: | |
begin_x = min(curses.COLS - ncols, begin_x + 1) | |
if ch == curses.KEY_UP: | |
begin_y = max(0, begin_y - 1) | |
if ch == curses.KEY_DOWN: | |
begin_y = min(curses.LINES - 1 - nlines, begin_y + 1) | |
if win.getbegyx() != (begin_y, begin_x): | |
erase_win(win) | |
win = create_win(nlines, ncols, begin_y, begin_x) | |
ch = stdscr.getch() | |
def main(): | |
curses.wrapper(app) | |
return 0 | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment