// clearscreen.cpp
#include <iostream>
#include <string>
#if defined(__WIN32__) || defined(__WIN32) || defined(_WIN32) || defined(WIN32) || defined(__TOS_WIN__) || defined(__WINDOWS__)
#ifndef __WIN32__
#define __WIN32__
#endif
#include <windows.h>
#else
#include <unistd.h>
#ifdef _POSIX_VERSION
#ifndef __UNIX__
#define __UNIX__
#endif
#include <term.h>
#endif
#endif
#if !defined(__WIN32__) && !defined(__UNIX__)
#warning Works best on a Windows or POSIX platform.
#endif
void clearscreen()
{
#if defined(__WIN32__)
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE) return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut,
(TCHAR) ' ',
cellCount,
homeCoords,
&count
)) return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)) return;
/* Move the cursor home */
SetConsoleCursorPosition( hStdOut, homeCoords );
#elif defined(__UNIX__)
static char* console_clearscreen = 0;
if (!console_clearscreen)
{
int result;
setupterm( NULL, STDOUT_FILENO, &result );
if (result > 0)
console_clearscreen = tigetstr( "clear" );
}
if (console_clearscreen)
putp( console_clearscreen );
#else
std::cout << std::string( 100, '\n' );
#endif
}
// end clearscreen.cpp