Last active
June 15, 2018 00:12
-
-
Save j2doll/547ab3dc38d700d427decbf8b1a414e4 to your computer and use it in GitHub Desktop.
Jump to ncurses
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
| /* | |
| curhello.cpp | |
| ========== | |
| (c) Copyright Paul Griffiths 1999 | |
| Email: mail@paulgriffiths.net | |
| "Hello, world!", ncurses style. | |
| */ | |
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #include <unistd.h> /* for sleep() */ | |
| #include <curses.h> | |
| int main(int argc, char *argv[]) | |
| { | |
| WINDOW * mainwin = NULL; | |
| // Initialize ncurses | |
| if ( (mainwin = initscr()) == NULL ) { | |
| fprintf(stderr, "Error initialising ncurses.\n"); | |
| exit(EXIT_FAILURE); | |
| } | |
| /* Display "Hello, world!" in the centre of the | |
| screen, call refresh() to show our changes, and | |
| sleep() for a few seconds to get the full screen effect */ | |
| mvaddstr(2, 3, "Hello, world!"); | |
| refresh(); | |
| sleep(3); | |
| // Clean up after ourselves | |
| delwin(mainwin); | |
| endwin(); | |
| refresh(); | |
| return EXIT_SUCCESS; | |
| } |
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
| /* | |
| curin1.cpp | |
| ======== | |
| (c) Copyright Paul Griffiths 1999 | |
| Email: mail@paulgriffiths.net | |
| Demonstrating basic ncurses single key input. | |
| */ | |
| #include <stdlib.h> | |
| #include <ctype.h> | |
| #include <curses.h> | |
| char * intprtkey(int ch); | |
| int main(void) { | |
| WINDOW * mainwin; | |
| int ch; | |
| /* Initialize ncurses */ | |
| if ( (mainwin = initscr()) == NULL ) { | |
| fprintf(stderr, "Error initializing ncurses.\n"); | |
| exit(EXIT_FAILURE); | |
| } | |
| noecho(); /* Turn off key echoing */ | |
| keypad(mainwin, TRUE); /* Enable the keypad for non-char keys */ | |
| /* Print a prompt and refresh() the screen */ | |
| mvaddstr(5, 10, "Press a key ('q' to quit)..."); | |
| mvprintw(7, 10, "You pressed: "); | |
| refresh(); | |
| /* Loop until user presses 'q' */ | |
| while ( (ch = getch()) != 'q' ) { | |
| /* Delete the old response line, and print a new one */ | |
| deleteln(); | |
| mvprintw(7, 10, "You pressed: 0x%x (%s)", ch, intprtkey(ch)); | |
| refresh(); | |
| } | |
| /* Clean up after ourselves */ | |
| delwin(mainwin); | |
| endwin(); | |
| refresh(); | |
| return EXIT_SUCCESS; | |
| } | |
| /* Struct to hold keycode/keyname information */ | |
| struct keydesc { | |
| int code; | |
| char name[20]; | |
| }; | |
| /* Returns a string describing a character passed to it */ | |
| char * intprtkey(int ch) { | |
| /* Define a selection of keys we will handle */ | |
| static struct keydesc keys[] = { { KEY_UP, "Up arrow" }, | |
| { KEY_DOWN, "Down arrow" }, | |
| { KEY_LEFT, "Left arrow" }, | |
| { KEY_RIGHT, "Right arrow" }, | |
| { KEY_HOME, "Home" }, | |
| { KEY_END, "End" }, | |
| { KEY_BACKSPACE, "Backspace" }, | |
| { KEY_IC, "Insert" }, | |
| { KEY_DC, "Delete" }, | |
| { KEY_NPAGE, "Page down" }, | |
| { KEY_PPAGE, "Page up" }, | |
| { KEY_F(1), "Function key 1" }, | |
| { KEY_F(2), "Function key 2" }, | |
| { KEY_F(3), "Function key 3" }, | |
| { KEY_F(4), "Function key 4" }, | |
| { KEY_F(5), "Function key 5" }, | |
| { KEY_F(6), "Function key 6" }, | |
| { KEY_F(7), "Function key 7" }, | |
| { KEY_F(8), "Function key 8" }, | |
| { KEY_F(9), "Function key 9" }, | |
| { KEY_F(10), "Function key 10" }, | |
| { KEY_F(11), "Function key 11" }, | |
| { KEY_F(12), "Function key 12" }, | |
| { -1, "<unsupported>" } | |
| }; | |
| static char keych[2] = {0}; | |
| if ( isprint(ch) && !(ch & KEY_CODE_YES)) { | |
| /* If a printable character */ | |
| keych[0] = ch; | |
| return keych; | |
| } | |
| else { | |
| /* Non-printable, so loop through our array of structs */ | |
| int n = 0; | |
| do { | |
| if ( keys[n].code == ch ) | |
| return keys[n].name; | |
| n++; | |
| } while ( keys[n].code != -1 ); | |
| return keys[n].name; | |
| } | |
| return NULL; /* We shouldn't get here */ | |
| } |
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
| # | |
| # Makefile | |
| # | |
| all: | |
| gcc -o curhello curhello.cpp -lncurses | |
| gcc -o curin1 curin1.cpp -lncurses | |
| clean: | |
| rm -f *.o | |
| rm -f *.a | |
| rm -f curhello | |
| rm -f curin1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment