similar to gotoxy; minimal screen control for wyse source code

This snippet submitted by Mike Kinney on 2005-02-22. It has been viewed 35719 times.
Rating of 5.3 with 215 votes

 #include <cstdio>
#include <string>
#include <sstream>
#include <iostream>
#include <cstdlib> // for getenv()
#include <unistd.h> // for sleep()

using std::string;
using std::cout;
using std::endl;
using std::flush;
using std::ostringstream;

string term=\"vt\"; // can be \"vt\" or \"wyse\"

// TODO: want to support two types: xterm/vt and wy50/wy60

// wyse escapes: http://vt100.net/docs/vt510-rm/chapter12
// vt escapes: source: http://rtfm.etla.org/xterm/ctlseq.htm

template<typename T> string toString(const T& x) {
   ostringstream oss;
   oss << x;
   return oss.str();
}

void clearScreen(void) {
   string s;
   if (term == \"vt\") { s=\"\033[2J\"; }
   if (term == \"wyse\") { s=\"\033+\"; }
   cout << s;
}

void clearToEOL(void) {
   string s;
   if (term == \"vt\") { s=\"\033[0K\"; }
   if (term == \"wyse\") { s=\"\033cO\"; }
   cout << s;
}

void mode132(void) {
   string s;
   if (term == \"vt\") { s=\"\033[?3h\"; }
   if (term == \"wyse\") { s=\"\033`;\"; }
   cout << s;
}

void mode80(void) {
   string s;
   if (term == \"vt\") { s=\"\033[?3l\"; }
   if (term == \"wyse\") { s=\"\033`:\"; }
   cout << s;
}

void gotoxy(int row, int col) {
   string s;
   if (term == \"vt\") { s=\"\033[\" + toString(row) + \";\" + toString(col) + \"H\"; }
   if (term == \"wyse\") { s=\"\033a\" + toString(row) + \"R\" + toString(col) + \"C\"; }
   cout << s;
}

void line(int x) {
   for (int i=0; i<x; ++i) {
      cout << \"x\";
   }
   cout << flush;
}

void getTerm(void) {
  string TERM=getenv(\"TERM\");
  //cout << \"TERM:\" << TERM << endl;
  if ((TERM==\"xterm\") || ((TERM.length() > 3) && (TERM.substr(0,2)==\"vt\"))) {
     term=\"vt\";
  }
  if ((TERM==\"wyse50\") || ((TERM.length() > 3) && (TERM.substr(0,2)==\"wy\"))) {
     term=\"wyse\";
  }
}

int main(int argc, char *argv[]) {

   getTerm();
   clearScreen();

   gotoxy(5,5);
   cout << \"(at 5,5)\" << flush;
   sleep(1);

   gotoxy(10,20);
   cout << \"(at 10,20)\" << flush;
   sleep(1);

   gotoxy(10,20);
   clearToEOL();
   cout << \"aa\" << flush;
   sleep(1);

   mode132();
   clearScreen();
   gotoxy(1,1);
   line(132);
   cout << \"in 132 mode!\" << flush;
   sleep(1);

   mode80();
   clearScreen();
   gotoxy(1,1);
   line(80);
   cout << \"back in 80 mode!\" << flush;
   sleep(1);

   cout << endl << flush;
   clearScreen();
   return(0);
}

 




More C and C++ source code snippets