Kod:
#ifndef _SHEC_HPP_
#define _SHEC_HPP_
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#ifdef WIN32
#define WIN_OS
#endif
#ifdef WIN_OS
#include <windows.h>
#include <conio.h>
#include <time.h>
#else
#include <termios.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#endif
//prototypes
namespace Shec
{
namespace ForegroundColors
{
enum Colors {
#ifdef WIN_OS
BLACK = 0, RED = 4, GREEN = 2, BLUE = 1, MAGNETA = 5, CYAN = 3, WHITE = 15
#else
BLACK = 30, RED, GREEN, BROWN, BLUE, MAGNETA, CYAN, WHITE
#endif
};
};
namespace BackgroundColors
{
enum Colors {
BLACK = 40, RED, GREEN, BROWN, BLUE, MAGNETA, CYAN, WHITE
};
};
typedef std::string string;
const long CLOCKS_PER_MILISEC = CLOCKS_PER_SEC / 1000;
class Console
{
public:
template<typename T>
static void Write(T Item);
template<typename T>
static void WriteLine(T Item);
template<typename T>
static void Read(T &Item);
template<typename T>
static void ReadLine(T &Item);
static const char GetKey();
static void SetTextColor(int Color);
static void SetBackgroundColor(int Color);
static void ClearScreen();
static void ShowCursor();
static void HideCursor();
static void Wait(int Miliseconds);
static void Pause(const char Text[]);
static void GotoXY(const int x, const int y);
static void WriteXY(const char Text[], const int x, const int y);
};
};
//linux config stuff (some C code)
#ifndef WIN_OS
static struct termios old, new_s;
void initTermios(int echo)
{
tcgetattr(0, &old);
new_s = old;
new_s.c_lflag &= ~ICANON;
new_s.c_lflag &= echo ? ECHO : ~ECHO;
tcsetattr(0, TCSANOW, &new_s);
}
void resetTermios(void)
{
tcsetattr(0, TCSANOW, &old);
}
char _getch()
{
static char ch;
initTermios(0);
ch = getchar();
resetTermios();
return ch;
}
#endif
//bodies
template<typename T>
void Shec::Console::Write(T Item)
{
std::cout << Item;
}
template<typename T>
void Shec::Console::WriteLine(T Item)
{
std::cout << Item << std::endl;
}
template<typename T>
void Shec::Console::Read(T &Item)
{
std::cin >> Item;
}
template<typename T>
void Shec::Console::ReadLine(T &Item)
{
std::cin >> Item;
std::cin.ignore();
}
const char Shec::Console::GetKey()
{
return _getch();
}
void Shec::Console::SetTextColor(int Color)
{
#ifdef OS_WIN
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Color);
#else
std::cout << "\033[22;" << Color << 'm';
#endif
}
void Shec::Console::SetBackgroundColor(int Color)
{
#ifdef OS_WIN
#else
std::cout << "\033[32;" << Color << 'm';
#endif
}
void Shec::Console::ClearScreen()
{
#ifdef OS_WIN
system("cls");
#else
system("clear");
#endif
}
void Shec::Console::ShowCursor()
{
#ifdef OS_WIN
static HANDLE hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
static CONSOLE_CURSOR_INFO hCCI;
GetConsoleCursorInfo(hConsoleOut, &hCCI);
if (hCCI.bVisible != TRUE)
{
hCCI.bVisible = TRUE;
SetConsoleCursorInfo(hConsoleOut, &hCCI);
}
#else
printf("\e[?25h");
#endif
}
void Shec::Console::HideCursor()
{
#ifdef OS_WIN
static HANDLE hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
static CONSOLE_CURSOR_INFO hCCI;
GetConsoleCursorInfo(hConsoleOut, &hCCI);
hCCI.bVisible = FALSE;
SetConsoleCursorInfo(hConsoleOut, &hCCI);
#else
printf("\e[?25l");
#endif
}
void Shec::Console::Wait(int Miliseconds)
{
clock_t EndTime;
EndTime = clock() + Miliseconds * CLOCKS_PER_MILISEC;
do; while(clock() < EndTime);
}
void Shec::Console::Pause(const char Text[] = NULL)
{
if(Text != NULL)
std::cout << Text;
GetKey();
}
void Shec::Console::GotoXY(const int x, const int y)
{
#ifdef OS_WIN
COORD c = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
#else
printf("%c[%d;%df",0x1B,y,x);
#endif
}
void Shec::Console::WriteXY(const char Text[], const int x, const int y)
{
GotoXY(x, y);
std::cout << Text;
}
#endif