Skip to content

Commit

Permalink
Enable VT code based highlighting on Windows if supported (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcleod committed Aug 25, 2024
1 parent dbf2eec commit f33f20e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 49 deletions.
36 changes: 11 additions & 25 deletions src/FGJSBBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,17 @@ namespace JSBSim {
CLASS IMPLEMENTATION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

#ifndef _MSC_VER
char FGJSBBase::highint[5] = {27, '[', '1', 'm', '\0' };
char FGJSBBase::halfint[5] = {27, '[', '2', 'm', '\0' };
char FGJSBBase::normint[6] = {27, '[', '2', '2', 'm', '\0' };
char FGJSBBase::reset[5] = {27, '[', '0', 'm', '\0' };
char FGJSBBase::underon[5] = {27, '[', '4', 'm', '\0' };
char FGJSBBase::underoff[6] = {27, '[', '2', '4', 'm', '\0' };
char FGJSBBase::fgblue[6] = {27, '[', '3', '4', 'm', '\0' };
char FGJSBBase::fgcyan[6] = {27, '[', '3', '6', 'm', '\0' };
char FGJSBBase::fgred[6] = {27, '[', '3', '1', 'm', '\0' };
char FGJSBBase::fggreen[6] = {27, '[', '3', '2', 'm', '\0' };
char FGJSBBase::fgdef[6] = {27, '[', '3', '9', 'm', '\0' };
#else
char FGJSBBase::highint[5] = {'\0' };
char FGJSBBase::halfint[5] = {'\0' };
char FGJSBBase::normint[6] = {'\0' };
char FGJSBBase::reset[5] = {'\0' };
char FGJSBBase::underon[5] = {'\0' };
char FGJSBBase::underoff[6] = {'\0' };
char FGJSBBase::fgblue[6] = {'\0' };
char FGJSBBase::fgcyan[6] = {'\0' };
char FGJSBBase::fgred[6] = {'\0' };
char FGJSBBase::fggreen[6] = {'\0' };
char FGJSBBase::fgdef[6] = {'\0' };
#endif
char FGJSBBase::highint[5] = {27, '[', '1', 'm', '\0' };
char FGJSBBase::halfint[5] = {27, '[', '2', 'm', '\0' };
char FGJSBBase::normint[6] = {27, '[', '2', '2', 'm', '\0' };
char FGJSBBase::reset[5] = {27, '[', '0', 'm', '\0' };
char FGJSBBase::underon[5] = {27, '[', '4', 'm', '\0' };
char FGJSBBase::underoff[6] = {27, '[', '2', '4', 'm', '\0' };
char FGJSBBase::fgblue[6] = {27, '[', '3', '4', 'm', '\0' };
char FGJSBBase::fgcyan[6] = {27, '[', '3', '6', 'm', '\0' };
char FGJSBBase::fgred[6] = {27, '[', '3', '1', 'm', '\0' };
char FGJSBBase::fggreen[6] = {27, '[', '3', '2', 'm', '\0' };
char FGJSBBase::fgdef[6] = {27, '[', '3', '9', 'm', '\0' };

const string FGJSBBase::needed_cfg_version = "2.0";
const string FGJSBBase::JSBSim_version = JSBSIM_VERSION " " __DATE__ " " __TIME__ ;
Expand Down
16 changes: 16 additions & 0 deletions src/JSBSim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ INCLUDES
# include <sys/time.h>
#endif

// The flag ENABLE_VIRTUAL_TERMINAL_PROCESSING is not defined for MinGW < 7.0.0
#if defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 7
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4
#endif

#include <iostream>
#include <cstdlib>

Expand Down Expand Up @@ -360,6 +365,17 @@ int real_main(int argc, char* argv[])
FDMExec->GetPropertyManager()->Tie("simulation/frame_start_time", &actual_elapsed_time);
FDMExec->GetPropertyManager()->Tie("simulation/cycle_duration", &cycle_duration);

// Check whether to disable console highlighting output on Windows.
// Support was added to Windows for Virtual Terminal codes by a particular
// Windows 10 release.
#ifdef _WIN32
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hStdOut, &dwMode);
if ((dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0)
nohighlight = true;
#endif

if (nohighlight) FDMExec->disableHighLighting();

if (simulation_rate < 1.0 )
Expand Down
24 changes: 0 additions & 24 deletions tests/unit_tests/FGLogTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,7 @@ void testRedFormat() {
log << JSBSim::LogFormat::RESET;
}
std::cout.rdbuf(cout_buffer);
#ifdef _MSC_VER
TS_ASSERT_EQUALS(buffer.str(), "Hello, World!");
#else
TS_ASSERT_EQUALS(buffer.str(), "\033[31mHello, World!\033[0m");
#endif
}

void testCyanFormat() {
Expand All @@ -337,11 +333,7 @@ void testCyanFormat() {
log << JSBSim::LogFormat::RESET;
}
std::cout.rdbuf(cout_buffer);
#ifdef _MSC_VER
TS_ASSERT_EQUALS(buffer.str(), "Hello, World!");
#else
TS_ASSERT_EQUALS(buffer.str(), "\033[34mHello, World!\033[0m");
#endif
}

void testBoldFormat() {
Expand All @@ -356,11 +348,7 @@ void testBoldFormat() {
log << JSBSim::LogFormat::RESET;
}
std::cout.rdbuf(cout_buffer);
#ifdef _MSC_VER
TS_ASSERT_EQUALS(buffer.str(), "Hello, World!");
#else
TS_ASSERT_EQUALS(buffer.str(), "\033[1mHello, World!\033[0m");
#endif
}

void testNormalFormat() {
Expand All @@ -375,11 +363,7 @@ void testNormalFormat() {
log << JSBSim::LogFormat::RESET;
}
std::cout.rdbuf(cout_buffer);
#ifdef _MSC_VER
TS_ASSERT_EQUALS(buffer.str(), "Hello, World!");
#else
TS_ASSERT_EQUALS(buffer.str(), "\033[22mHello, World!\033[0m");
#endif
}

void testUnderlineFormat() {
Expand All @@ -394,11 +378,7 @@ void testUnderlineFormat() {
log << JSBSim::LogFormat::UNDERLINE_OFF;
}
std::cout.rdbuf(cout_buffer);
#ifdef _MSC_VER
TS_ASSERT_EQUALS(buffer.str(), "Hello, World!");
#else
TS_ASSERT_EQUALS(buffer.str(), "\033[4mHello, World!\033[24m");
#endif
}

void testDefaultFormat() {
Expand All @@ -413,10 +393,6 @@ void testDefaultFormat() {
log << JSBSim::LogFormat::RESET;
}
std::cout.rdbuf(cout_buffer);
#ifdef _MSC_VER
TS_ASSERT_EQUALS(buffer.str(), "Hello, World!");
#else
TS_ASSERT_EQUALS(buffer.str(), "\033[39mHello, World!\033[0m");
#endif
}
};

0 comments on commit f33f20e

Please sign in to comment.