Skip to content
console.h 2.26 KiB
Newer Older
Daniel Lins de's avatar
Daniel Lins de committed
#include <string>
#include <iostream>

// ANSI codes

namespace console {

	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;
	}*/

	void eraser() {
		std::cout << "\033[A\033[2K";
	}

}