Advertisement
Guest User

Untitled

a guest
Nov 18th, 2013
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. /* ncurses C++
  2.  *
  3.  * A general purpose example of using ncurses in C++ e.g. with STL strings.
  4.  * I guess whatever license ncurses uses applies, otherwise public domain.
  5.  */
  6.  
  7. # include <algorithm>
  8. # include <iostream>
  9. # include <fstream>
  10. # include <iterator>
  11. # include <string>
  12. # include <sstream>
  13. # include <vector>
  14. # include <ncurses.h> // Macros cause compatibility issues with sstream. If you must use them together, include ncurses second.
  15. // Most of these includes/headers/libraries/etc. are not required here. If it doesn't compile without it, you need it.
  16.  
  17. bool curses_started = 0; // Redundant added safety.
  18. bool input_mode_command = 1; // Standard terminal input (1) vs. ncurses reading every character (0).
  19. int row, col;
  20. std::string input; // Global variable to hold current input. String type because UNIX philosophy.
  21.  
  22. auto getinput () -> void // Gets one unit of input at a time. Whether that is one line or one character is dependent on the input mode.
  23. {
  24.     char ch;
  25.     input.clear();
  26.     if (input_mode_command)
  27.     {
  28.         while (1) // Assumes (default) position of cursor is at printing location.
  29.         {
  30.             ch = getch();
  31.             if (ch == '\n') // Keeps buffering input until end of line. Check done after acquiring input char (thus if inside while) to prevent it from being discarded automatically.
  32.             {
  33.                 break;
  34.             }
  35.             if (ch == '\a' || ch == '\b') // Ensure normal attempts at backspace are caught.
  36.             {
  37.                 if (!input.empty()) // pop_back will cause a crash when attempting to remove stuff from an empty vector.
  38.                 {
  39.                     input.pop_back(); // Removes previously entered character from buffered input.
  40.                     addch('\b'); // Actual backspacing from user's perspective in these 5 lines.
  41.                     addch(' ');
  42.                     int y, x;
  43.                     getyx (stdscr, y, x);
  44.                     move (y, x-1); // More than one line of user input deemed unlikely/useless.
  45.                 }
  46.             }
  47.             else
  48.             {
  49.                 input.push_back(ch);
  50.                 addch(ch);
  51.             }
  52.         }
  53.     }
  54.     else
  55.     {
  56.         ch = getch();
  57.         input.push_back(ch);
  58.     }
  59. }
  60.  
  61. // Alternative functions to ncurses output functions with same naming scheme using STL strings follow. Done for C++11 compatibility and ease-of-use.
  62.  
  63. auto printstr (const std::string &str) -> void
  64. {
  65.     for (char ch : str)
  66.     {
  67.         addch (ch);
  68.     }
  69. }
  70.  
  71. auto mvprintstr (int y, int x, const std::string &str) -> void
  72. {
  73.     move (y, x);
  74.     printstr (str);
  75. }
  76.  
  77. auto mvwprintstr (WINDOW *win, int y, int x, const std::string &str) -> void
  78. {
  79.     wmove (win, y, x);
  80.     printstr (str);
  81. }
  82.  
  83. auto wprintstr (WINDOW *win, const std::string &str) -> void
  84. {
  85.     int y, x;
  86.     getyx (win, y, x);
  87.     mvwprintstr (win, y, x, str);
  88. }
  89.  
  90. auto endCurses () -> void
  91. {
  92.     if (curses_started && !isendwin())
  93.     {
  94.         endwin();
  95.     }
  96. }
  97.  
  98. auto startCurses () -> void
  99. {
  100.     if (curses_started)
  101.     {
  102.         refresh();
  103.     }
  104.     else // IMO most powerful defaults
  105.     {
  106.         initscr();
  107.         raw();
  108.         noecho();
  109.         keypad(stdscr, 1);
  110.         atexit(endCurses);
  111.         curses_started = 1;
  112.     }
  113. }
  114.  
  115. auto init_color_pairs () -> void // To initialise all colors using a easy-to-memorize naming scheme.
  116. {
  117.     short f, b; // For foreground and background respectively.
  118.     for ( f = 0; f < COLORS; ++f )
  119.     {
  120.         for ( b = 0; b < COLORS; ++b )
  121.         {
  122.             init_pair ( f * COLORS + b, f, b );
  123.         }
  124.     }
  125. }
  126.  
  127. auto main () -> int
  128. {
  129.     startCurses();
  130.     if(!has_colors()) // Harsh redundant safety check.
  131.     {
  132.         endCurses();
  133.         std::cout << "Your terminal does not support color" << std::endl;
  134.         return 1;
  135.     }
  136.     start_color();
  137.     init_color_pairs();
  138.     getmaxyx(stdscr,row,col);
  139.     while (1)
  140.     {
  141.         getinput();
  142.         if (input == ":exit") // Makes rerunning tests easy when program messes up but doesn't crash.
  143.         {
  144.             return 0;
  145.         }
  146.         printstr (input); // Insert your "apply input" function(s) here.
  147.     }
  148.     endCurses(); // Even more redundant safety. Also because the return is mandatory anyways.
  149.     return 1;
  150. }
  151.  
  152. /* Further notes:
  153.  *  Use of windows and other things could be generalised better.
  154.  *  This is a non-exhausive and amateur example.
  155.  *  A lot of this was taken from the public domain.
  156.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement