I just installed Ubuntu 12.04 (Precise) on both my netbook and desktop. I haven't used a Linux OS for a while and I wanted to see how different it would be when coding. I knew some things that Window's can do, so I wanted to try to code them on Linux as well.
I've been fond of the conio.h header file from back when I first learned programming and thought it was just awesome. The more and more I learn, the more I try to get away from it. I have doing research on clearing consoles on Linux and ran across a small piece of code:
cout << "\x1B[2J";
. I decided to do some digging and found out it was actually and ANSI escape code. That got me thinking.
Nowadays, the odds that a compiler isn't ANSI compliant is slim to none (I know of none) so I figured, "What better way to make something more standard?" I don't believe compilers are required to be ANSI compliant (I could be wrong) but with very few not being compliant, I figure it would cover more OS's than even doing OS specific libraries (windows.h, termios.h, etc.). (Am I wrong that ANSI is compiler dependent and not C++ dependent?)
For the last few hours, I was having a good bit of fun with the ANSI codes so I started typing up my header file again.
Here is the beta, if you will:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
namespace vp {
void consolecursor(bool show = true) {
if (show)
std::cout << "\x1B[25h";
else
std::cout << "\x1B[25l";
}
bool gotoxy(unsigned short x = 1, unsigned short y = 1) {
if ((x == 0) || (y == 0))
return false;
std::cout << "\x1B[" << y << ";" << x << "H";
}
void clearscreen(bool moveToStart = true) {
std::cout << "\x1B[2J";
if (moveToStart)
gotoxy(1,1);
}
enum colors { BLACK = 0, RED, GREEN, YELLOW, BLUE, PURPLE, CYAN, GREY,
LIGHTGREY, LIGHTRED, LIGHTGREEN, LIGHTYELLOW, LIGHTBLUE,
LIGHTPURPLE, LIGHTCYAN, WHITE, DEFAULT };
void consolecolor(colors textColor = DEFAULT, colors backgroundColor = DEFAULT) {
// Set default foreground color
if (textColor == DEFAULT)
std::cout << "\x1B[39m";
// Set bright foreground color
else if (textColor > GREY) {
// Set bright mode
std::cout << "\x1B[1m";
// Set color
std::cout << "\x1B[3" << textColor - LIGHTGREY << "m";
}
// Set normal foreground color
else {
// Set normal mode
std::cout << "\x1B[22m";
// Set color
std::cout << "\x1B[3" << textColor << "m";
}
// Set default background color
if (backgroundColor == DEFAULT)
std::cout << "\x1B[49m";
// Set bright background color
else if (backgroundColor > GREY) {
// Set bright mode
std::cout << "\x1B[1m";
// Set color
std::cout << "\x1B[4" << backgroundColor - LIGHTGREY << "m";
}
// Set normal background color
else {
// Set normal mode
std::cout << "\x1B[22m";
// Set color
std::cout << "\x1B[4" << backgroundColor << "m";
}
}
void setconsoletextcolor(colors textColor) {
// Set bright foreground color
if (textColor > GREY) {
// Set bright mode
std::cout << "\x1B[1m";
// Set color
std::cout << "\x1B[3" << textColor - LIGHTGREY << "m";
}
// Set normal foreground color
else {
// Set normal mode
std::cout << "\x1B[22m";
// Set color
std::cout << "\x1B[3" << textColor << "m";
}
}
// Numerical color setter
/*bool setconsoletextcolor(int textcolor = -1) {
if (((textcolor < 0) || (textcolor > 7)) && (textcolor != -1))
return false;
std::cout << "\x1B[3" << textcolor << "m";
return true;
}*/
}
|
Essentially, I'm pasting this to show some people (who never knew about ANSI, like myself) what you can do with ANSI. I also wanted to see what opinions you guys have of the code, things that can be added, removed, etc. I did try to error test it, but it's getting late so I decided to turn in.
Tomorrow, I'm going to look at Duoas' code again for Linux PressAnyKey Code to see if I can figure out how to get it to work like the Window's OS version. In the meantime, I welcome Criticism.
~Volatile Pulse