Skip to content

Commit

Permalink
colorize cpp get_line on all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed Oct 23, 2023
1 parent ab6cd2b commit b4ca4d1
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion gnucash/gnucash-commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ static std::string empty_string{};
/* This static indicates the debugging module that this .o belongs to. */
static QofLogModule log_module = GNC_MOD_GUI;

#ifdef _WIN32
struct ConsoleStruct
{
public:
bool has_ansi () { return m_has_ansi; };
private:
HANDLE m_stdoutHandle = INVALID_HANDLE_VALUE;
DWORD m_outModeInit = 0;
bool m_has_ansi = false;
ConsoleStruct () : m_stdoutHandle {GetStdHandle(STD_OUTPUT_HANDLE)}
{
if (m_stdoutHandle != INVALID_HANDLE_VALUE && GetConsoleMode(m_stdoutHandle, &m_outModeInit))
{
SetConsoleMode (m_stdoutHandle, m_outModeInit | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
m_has_ansi = true;
}
}
~ConsoleStruct ()
{
if (m_stdoutHandle == INVALID_HANDLE_VALUE)
return;
printf ("\x1b[0m");
SetConsoleMode (m_stdoutHandle, m_outModeInit);
}
} ansi_codes;
#else
struct ConsoleStruct
{
bool has_ansi () { return true; };
} ansi_codes;
#endif

static int
cleanup_and_exit_with_failure (QofSession *session)
{
Expand Down Expand Up @@ -110,7 +142,8 @@ static std::string
get_line (const char* prompt)
{
std::string rv;
std::cout << prompt;
std::cout << (ansi_codes.has_ansi() ? "\x1b[1;33m" : "") << prompt
<< (ansi_codes.has_ansi() ? "\x1b[m" : "");
if (!std::getline (std::cin, rv))
gnc_shutdown_cli (1);
return rv;
Expand Down

0 comments on commit b4ca4d1

Please sign in to comment.