It can't get stupider than this.
This library bridges some "inconsistencies" between systems. It has evil code in it, and that's it. I won't bother fixing anything.
Just put compat.h
into your project and include it.
#include "compat.h"
int main() {
compat_sleep(1000);
return 0;
}
By default, terminal stuff is managed in Windows via system()
and the associated programs. This is the case because
Windows does not suport ANSI escape codes (or very limited) until Windows 10 1809.
If you're sure the code will run in newer versions of Windows, add #define COMPAT_FORCE_ANSI_ESCAPE
before including compat.h
.
This will make use of ANSI escape codes and fix a bug where colors can't be resetted properly.
This is a macro to Sleep()
on Windows and usleep(ms * 1000)
on other platforms.
This is a macro to Beep()
on Windows and triggers a terminal bell on other platforms.
In the future, a ALSA / Core Audio backend will be added for Linux and MacOS to simulate Windows Beep()
This is a macro to system("cls")
on Windows and uses ANSI escape code in other platforms.
COMPAT_FORCE_ANSI_ESCAPE
applies to this macro.
This function calls system("color")
on Windows and uses ANSI escape codes in other platforms.
The arguments follow Windows's color
command: 2 characters each representing background and foreground color respectively. If an empty string is provided, the colors are reset.
COMPAT_FORCE_ANSI_ESCAPE
applies to this function.
This function uses start
semantics to open a file with its default program on Windows. It uses open
in MacOS and xdg-open
in other platforms.
This function uses _getch()
on Windows.
On other platforms, it disables canonical and echo on current TTY to read raw input.
Note: This is not guaranteed to work on platforms other than Windows, Linux and MacOS.