mirror of
https://github.com/silverqx/TinyORM.git
synced 2026-05-06 16:49:19 -05:00
explicitly enable vt100 on MSYS2 ucrt64
The vt100 processing is not enabled on the MSYS2 ucrt64 by default so enable it explicitly. - also moved terminal initialization code to the Tom::Terminal class
This commit is contained in:
@@ -104,11 +104,6 @@ namespace Concerns
|
||||
/*! Alias for the commands' base class. */
|
||||
using Command = Commands::Command;
|
||||
|
||||
/* Application initialization */
|
||||
#ifdef _WIN32
|
||||
/*! Prepare console input/output character encoding. */
|
||||
void initializeConsoleEncoding() const;
|
||||
#endif
|
||||
/*! Fix m_argc/m_argv data members if the argv is empty. */
|
||||
void fixEmptyArgv();
|
||||
/*! Processes the specified function at application's normal exit. */
|
||||
|
||||
@@ -31,6 +31,9 @@ namespace Tom
|
||||
/*! Default destructor. */
|
||||
inline ~Terminal() = default;
|
||||
|
||||
/*! Prepare the console terminal. */
|
||||
static void initialize();
|
||||
|
||||
/*! Supports the given output ansi colors? (ansi is disabled for non-tty). */
|
||||
bool hasColorSupport(std::ostream &cout = std::cout) const;
|
||||
/*! Supports the given output ansi colors? (ansi is disabled for non-tty),
|
||||
@@ -75,6 +78,16 @@ namespace Tom
|
||||
bool hasVt100Support(std::wostream &wcout) const;
|
||||
#endif
|
||||
|
||||
/* Terminal initialization */
|
||||
#ifdef _WIN32
|
||||
/*! Enable the UTF-8 console input/output character encoding. */
|
||||
static void enableUtf8ConsoleEncoding();
|
||||
#endif
|
||||
#ifdef __MINGW32__
|
||||
/*! Enable the virtual terminal processing on the out/err output streams. */
|
||||
static void enableVt100Support();
|
||||
#endif
|
||||
|
||||
/*! Cache for detected ansi output. */
|
||||
mutable std::unordered_map<std::ostream *, bool> m_isAnsiOutput {};
|
||||
/*! Cache for detected ansi output, wide version. */
|
||||
|
||||
@@ -3,13 +3,6 @@
|
||||
#include <QCoreApplication>
|
||||
#include <QDebug>
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <qt_windows.h>
|
||||
|
||||
# include <fcntl.h>
|
||||
# include <io.h>
|
||||
#endif
|
||||
|
||||
#include <orm/databasemanager.hpp>
|
||||
#include <orm/utils/type.hpp>
|
||||
#include <orm/version.hpp>
|
||||
@@ -30,6 +23,7 @@
|
||||
#include "tom/commands/migrations/statuscommand.hpp"
|
||||
#include "tom/migrationrepository.hpp"
|
||||
#include "tom/migrator.hpp"
|
||||
#include "tom/terminal.hpp"
|
||||
#ifndef TINYTOM_TESTS_CODE
|
||||
# include "tom/version.hpp"
|
||||
#endif
|
||||
@@ -81,10 +75,8 @@ Application::Application(int &argc, char **argv, std::shared_ptr<DatabaseManager
|
||||
, m_migrationTable(std::move(migrationTable))
|
||||
, m_migrations(std::move(migrations))
|
||||
{
|
||||
#ifdef _WIN32
|
||||
// Prepare console input/output character encoding
|
||||
initializeConsoleEncoding();
|
||||
#endif
|
||||
// Enable UTF-8 encoding and vt100 support
|
||||
Terminal::initialize();
|
||||
|
||||
// Following is not relevant in the auto test executables
|
||||
#ifndef TINYTOM_TESTS_CODE
|
||||
@@ -157,23 +149,6 @@ void Application::enableInUnitTests() noexcept
|
||||
|
||||
/* protected */
|
||||
|
||||
/* Application initialization */
|
||||
|
||||
#ifdef _WIN32
|
||||
void Application::initializeConsoleEncoding() const
|
||||
{
|
||||
// Set it here so the user doesn't have to deal with this
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
|
||||
/* UTF-8 encoding is corrupted for narrow input functions, needed to use wcin/wstring
|
||||
for an input, input will be in the unicode encoding then needed to translate
|
||||
unicode to utf8, eg. by QString::fromStdWString(), WideCharToMultiByte(), or
|
||||
std::codecvt(). It also works with msys2 ucrt64 gcc/clang. */
|
||||
SetConsoleCP(CP_UTF8);
|
||||
_setmode(_fileno(stdin), _O_WTEXT);
|
||||
}
|
||||
#endif
|
||||
|
||||
void Application::fixEmptyArgv()
|
||||
{
|
||||
constexpr const auto *const empty = "";
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <qt_windows.h>
|
||||
|
||||
# include <fcntl.h>
|
||||
# include <io.h>
|
||||
#elif defined(__linux__)
|
||||
# include <sys/ioctl.h>
|
||||
#endif
|
||||
|
||||
#include <io.h>
|
||||
|
||||
#include <orm/utils/type.hpp>
|
||||
|
||||
#include "tom/exceptions/invalidargumenterror.hpp"
|
||||
@@ -21,6 +22,17 @@ namespace Tom
|
||||
|
||||
// I can tell that ansi logic detection in this class is a real porn 😎
|
||||
|
||||
void Terminal::initialize()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
enableUtf8ConsoleEncoding();
|
||||
#endif
|
||||
|
||||
#ifdef __MINGW32__
|
||||
enableVt100Support();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Terminal::hasColorSupport(std::ostream &cout) const
|
||||
{
|
||||
auto *const coutPointer = std::addressof(cout);
|
||||
@@ -205,6 +217,38 @@ bool Terminal::hasVt100Support(std::wostream &wcout) const
|
||||
return (mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) ==
|
||||
ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
||||
}
|
||||
|
||||
/* Terminal initialization */
|
||||
|
||||
void Terminal::enableUtf8ConsoleEncoding()
|
||||
{
|
||||
// Set it here so the user doesn't have to deal with this
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
|
||||
/* UTF-8 encoding is corrupted for narrow input functions, needed to use wcin/wstring
|
||||
for an input, input will be in the unicode encoding then needed to translate
|
||||
unicode to utf8, eg. by QString::fromStdWString(), WideCharToMultiByte(), or
|
||||
std::codecvt(). It also works with msys2 ucrt64 gcc/clang. */
|
||||
SetConsoleCP(CP_UTF8);
|
||||
_setmode(_fileno(stdin), _O_WTEXT);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __MINGW32__
|
||||
void Terminal::enableVt100Support()
|
||||
{
|
||||
/* The vt100 is disabled by default on MSYS2 so have to be explicitly enabled:
|
||||
https://github.com/msys2/msys2-runtime/issues/91 */
|
||||
DWORD mode = 0;
|
||||
auto *const stdOutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
GetConsoleMode(stdOutHandle, &mode);
|
||||
SetConsoleMode(stdOutHandle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
|
||||
|
||||
mode = 0;
|
||||
auto *const stdErrHandle = GetStdHandle(STD_ERROR_HANDLE);
|
||||
GetConsoleMode(stdErrHandle, &mode);
|
||||
SetConsoleMode(stdErrHandle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace Tom
|
||||
|
||||
Reference in New Issue
Block a user