Advertisement
Guest User

Untitled

a guest
May 26th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include <sstream>
  2. #include <iostream>
  3. #include <unistd.h>
  4. #include <sys/ioctl.h>
  5. #include <signal.h>
  6. #include <fcntl.h>
  7. #include <termios.h>
  8.  
  9. static int done = 0;      //State variable
  10.  
  11. namespace con {
  12.     int comax() {         //Get number of columns
  13.         struct winsize w;
  14.         ioctl(0, TIOCGWINSZ, &w);
  15.         return(w.ws_col);
  16.     }
  17.    
  18.     int romax() {         //Get number of rows
  19.         struct winsize w;
  20.         ioctl(0, TIOCGWINSZ, &w);
  21.         return(w.ws_row);
  22.     }
  23.    
  24.     std::ostream& ED(std::ostream& s) {     //Clearing terminal window
  25.         return s << std::string("\033[2J");
  26.     }
  27.  
  28.     class estream {
  29.         private:
  30.             std::string escape;
  31.         public:
  32.             estream(std::string e): escape(e) {}
  33.             friend std::ostream& operator<<(std::ostream&, estream);
  34.     };
  35.    
  36.     std::ostream& operator<<(std::ostream& out, estream e) {
  37.         out << e.escape << std::flush;
  38.         return out;
  39.     }
  40.    
  41.     estream CUP(int x, int y) {     //Move the cursor
  42.         std::ostringstream sout;
  43.         sout << "\033[" << x << ";" << y << "H";
  44.         return(estream(sout.str()));
  45.     }
  46.    
  47.     estream SGR(int r) {            //Background drawing
  48.         std::ostringstream sout;
  49.         sout << "\033[" << r << "m";
  50.         return(estream(sout.str()));
  51.     }
  52.        
  53.     int kbin() {        //Set delay and non-blocking mode
  54.         char buf[2];
  55.         int n = 0;
  56.         int flags = fcntl(0, F_GETFL);
  57.         fcntl(0, F_SETFL, flags | O_NONBLOCK);
  58.         n = read(0, buf, 2);
  59.         usleep(10000);
  60.         fcntl(0, F_SETFL, flags);
  61.         return(n);
  62.     }
  63. }
  64.  
  65. void interruptor(int sig) {
  66.     done = sig;
  67.     return;
  68. }
  69.  
  70. void unset_canon_mode(struct termios* stored_settings) {    //Disable canonical mode and input visibility
  71.     struct termios new_settings;
  72.     tcgetattr(0, stored_settings);
  73.     new_settings = *stored_settings;
  74.    
  75.     new_settings.c_lflag &= ~(ICANON | ECHO);
  76.    
  77.     tcsetattr(0, TCSANOW, &new_settings);
  78.     return;
  79. }
  80.  
  81. void restore_mode(struct termios* stored_settings) {    //Return canonical mode and input visibility
  82.     tcsetattr(0, TCSANOW, stored_settings);
  83.     return;
  84. }
  85.  
  86. int main(int argc, char** argv) {
  87.     int x,y;                
  88.     int rows, columns;
  89.     int tempColor = 0;
  90.     int bgColor = 40;
  91.     struct termios st_sets;
  92.     struct termios* stored_settings = &st_sets;
  93.    
  94.     rows = con::romax();
  95.     columns = con::comax();
  96.     x = 1; y = columns;
  97.    
  98.     std::cout << con::ED << con::CUP(1,1);
  99.     signal(SIGINT, interruptor);
  100.    
  101.     unset_canon_mode(stored_settings);
  102.    
  103.     while (done < 1) {
  104.         for ( ; x <= rows; x++) {
  105.             std::cout << con::CUP(x, y) << con::SGR(bgColor) << " ";
  106.         }
  107.         x = 1;
  108.         if (--y == 0) {
  109.             y = columns;
  110.             tempColor = (tempColor + 1) % 8;
  111.             bgColor = 40 + tempColor;
  112.         }
  113.         if (con::kbin() > 0) {
  114.             break;
  115.         }
  116.     }
  117.    
  118.     std::cout << con::CUP(1, 1) << con::SGR(0) << con::ED;
  119.    
  120.     restore_mode(stored_settings);
  121.     return(0);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement