Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sstream>
- #include <iostream>
- #include <unistd.h>
- #include <sys/ioctl.h>
- #include <signal.h>
- #include <fcntl.h>
- #include <termios.h>
- static int done = 0; //State variable
- namespace con {
- int comax() { //Get number of columns
- struct winsize w;
- ioctl(0, TIOCGWINSZ, &w);
- return(w.ws_col);
- }
- int romax() { //Get number of rows
- struct winsize w;
- ioctl(0, TIOCGWINSZ, &w);
- return(w.ws_row);
- }
- std::ostream& ED(std::ostream& s) { //Clearing terminal window
- return s << std::string("\033[2J");
- }
- class estream {
- private:
- std::string escape;
- public:
- estream(std::string e): escape(e) {}
- friend std::ostream& operator<<(std::ostream&, estream);
- };
- std::ostream& operator<<(std::ostream& out, estream e) {
- out << e.escape << std::flush;
- return out;
- }
- estream CUP(int x, int y) { //Move the cursor
- std::ostringstream sout;
- sout << "\033[" << x << ";" << y << "H";
- return(estream(sout.str()));
- }
- estream SGR(int r) { //Background drawing
- std::ostringstream sout;
- sout << "\033[" << r << "m";
- return(estream(sout.str()));
- }
- int kbin() { //Set delay and non-blocking mode
- char buf[2];
- int n = 0;
- int flags = fcntl(0, F_GETFL);
- fcntl(0, F_SETFL, flags | O_NONBLOCK);
- n = read(0, buf, 2);
- usleep(10000);
- fcntl(0, F_SETFL, flags);
- return(n);
- }
- }
- void interruptor(int sig) {
- done = sig;
- return;
- }
- void unset_canon_mode(struct termios* stored_settings) { //Disable canonical mode and input visibility
- struct termios new_settings;
- tcgetattr(0, stored_settings);
- new_settings = *stored_settings;
- new_settings.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(0, TCSANOW, &new_settings);
- return;
- }
- void restore_mode(struct termios* stored_settings) { //Return canonical mode and input visibility
- tcsetattr(0, TCSANOW, stored_settings);
- return;
- }
- int main(int argc, char** argv) {
- int x,y;
- int rows, columns;
- int tempColor = 0;
- int bgColor = 40;
- struct termios st_sets;
- struct termios* stored_settings = &st_sets;
- rows = con::romax();
- columns = con::comax();
- x = 1; y = columns;
- std::cout << con::ED << con::CUP(1,1);
- signal(SIGINT, interruptor);
- unset_canon_mode(stored_settings);
- while (done < 1) {
- for ( ; x <= rows; x++) {
- std::cout << con::CUP(x, y) << con::SGR(bgColor) << " ";
- }
- x = 1;
- if (--y == 0) {
- y = columns;
- tempColor = (tempColor + 1) % 8;
- bgColor = 40 + tempColor;
- }
- if (con::kbin() > 0) {
- break;
- }
- }
- std::cout << con::CUP(1, 1) << con::SGR(0) << con::ED;
- restore_mode(stored_settings);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement