Advertisement
Guest User

NCURSES.shit

a guest
Nov 30th, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. /*
  2. NCURSES.shit
  3.  
  4. You'll need to import these:
  5. */
  6.  
  7. #include <stdlib.h>
  8. #include <curses.h>
  9.  
  10. /* in your makefile you also need to add this at the end of the CFLAGS */
  11. -lcurses
  12.  
  13. /* To start a NCURSES window (this is what makes the terminal window blank and not operate like bash), use this: */
  14.  
  15. initscr();
  16.  
  17. /* Here are some other things I did on create: */
  18.  
  19. keypad(stdscr, TRUE);   /* enable keyboard mapping */
  20. raw();                  /* enables raw input, which we want */
  21. nonl();                 /* tell curses not to do NL->CR/NL on output (this was in an example idk it works with it) */
  22. cbreak();               /* take input chars one at a time, no wait for \n */
  23. noecho();               /* do not echo input */
  24. curs_set(0);            /* hide cursor */
  25.  
  26. /* Handy things you'll need: */
  27.  
  28. /*TO END AN NCURSES SESSION*/
  29. endwin();
  30. /* then print out your results and they'll appear in the normal terminal, then specify exit(0) */
  31.  
  32. /* TO CLEAR THE SCREEN */
  33. clear();
  34.  
  35. int termRows, termCols;
  36. getmaxyx(stdscr, termRows, termCols);
  37. /*stdscr is built in you don't need to define it,
  38. this does what read does and puts the current number of rows and columns into the specified variables */
  39.  
  40. int ch = getch(); /* this waits for a key input, then you can check ch == stuff and handle arrow keys */
  41.  
  42. /* speaking of arrow keys, NCURSES has some great built in things */
  43.  
  44. /* ch == KEY_LEFT || ch == KEY_RIGHT || ch == KEY_UP || ch == KEY_DOWN all work as expected */
  45.  
  46. /* ch == ' ' checks for spacebar. ch == KEY_BACKSPACE works for backspace. */
  47. /* HOWEVER KEY_ENTER exists but doesn't work. \n didn't work for me either. */
  48.  
  49. /* through testing and printing out ch, I learned that, at least on the linux lab: */
  50. /* ch == 13 checks for the enter key pressed*/
  51. /* ch == 27 checks for the ESC pressed but has a full second delay for reasons relating to "you're not supposed to use the ESC key" */
  52.  
  53. /* ABSOLUTELY NEEDED */
  54.  
  55. /* By default the "cursor" is at 0, 0 (given as row, col aka y, x) */
  56. addch('a');
  57. /* that will add the character a to 0, 0, then automatically move the cursor to 0, 1 */
  58.  
  59. addstr("Hello world!");
  60. /* that will add the string "Hello world!" to wherever the cursor is, moving it to the end of that line. It might wrap if you let it get to the end of the window and cause problems. */
  61.  
  62. move(y, x);
  63. /* THIS IS A BIG ONE, and it lets you manually change rows to print each option to a row, or move say 25 columns right to start printing to a new column */
  64.  
  65. /* HIGHLIGHTING AND UNDERLINING */
  66. /* This is super easy. Before you call addch or addstr, use */
  67. attron(A_UNDERLINE);
  68. /*this will make text after it underlined. To turn off underlining, use */
  69. attroff(A_UNDERLINE);
  70.  
  71. /* for highlighting, the commands are */
  72. attron(A_STANDOUT);
  73. attroff(A_STANDOUT);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement