From b13831dc92a3f8393419c4b5aebc4a899e2e62a3 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 19 Nov 2009 20:29:11 -0500 Subject: [PATCH] worked through the printw_example exercise, added python version --- basics/printw_example.c | 27 +++++++++++++-------------- basics/printw_example.py | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 basics/printw_example.py diff --git a/basics/printw_example.c b/basics/printw_example.c index 3f6d2e5..310c40d 100644 --- a/basics/printw_example.c +++ b/basics/printw_example.c @@ -1,20 +1,19 @@ -#include /* ncurses.h includes stdio.h */ +#include #include int main() { - char mesg[]="Just a string"; /* message to be appeared on the screen */ - int row,col; /* to store the number of rows and * - * the number of colums of the screen */ - initscr(); /* start the curses mode */ - getmaxyx(stdscr,row,col); /* get the number of rows and columns */ - mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); - /* print the message at the center of the screen */ - mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col); - printw("Try resizing your window(if possible) and then run this program again"); - refresh(); - getch(); - endwin(); + char mesg[] = "Just a string"; + int row, col; - return 0; + initscr(); + getmaxyx(stdscr, row, col); + mvprintw(row / 2, (col - strlen(mesg)) / 2, "%s", mesg); + mvprintw(row - 2, 0, "This screen has %d rows and %d columns\n", row, col); + printw("Try resizing your window (if possible) and then run this program again"); + refresh(); + getch(); + endwin(); + + return 0; } diff --git a/basics/printw_example.py b/basics/printw_example.py new file mode 100644 index 0000000..1770617 --- /dev/null +++ b/basics/printw_example.py @@ -0,0 +1,25 @@ +import sys +import curses + + +def printw_example(stdscr): + mesg = "Just a string" + row, col = stdscr.getmaxyx() + stdscr.addstr(row / 2, (col - len(mesg)) / 2, mesg) + stdscr.addstr(row - 2, 0, "This screen has {0} rows and {1} " + "columns\n".format(row, col)) + stdscr.addstr(row - 1, 0, "Try resizing your window (if possible) and then" + "run this program again") + stdscr.refresh() + stdscr.getch() + + +def main(): + curses.wrapper(printw_example) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) + +# vim:filetype=python