Browse Source

Support styled output in Windows consoles

default_compile_flags
vector-of-bool 3 years ago
parent
commit
0b87d6efcc
4 changed files with 37 additions and 2 deletions
  1. +2
    -0
      src/dds.main.cpp
  2. +2
    -0
      src/dds/util/output.hpp
  3. +4
    -0
      src/dds/util/output.nix.cpp
  4. +29
    -2
      src/dds/util/output.win.cpp

+ 2
- 0
src/dds.main.cpp View File

#include <dds/cli/dispatch_main.hpp> #include <dds/cli/dispatch_main.hpp>
#include <dds/cli/options.hpp> #include <dds/cli/options.hpp>
#include <dds/util/log.hpp> #include <dds/util/log.hpp>
#include <dds/util/output.hpp>
#include <dds/util/signal.hpp> #include <dds/util/signal.hpp>


#include <debate/debate.hpp> #include <debate/debate.hpp>
std::setlocale(LC_CTYPE, ".utf8"); std::setlocale(LC_CTYPE, ".utf8");


dds::install_signal_handlers(); dds::install_signal_handlers();
dds::enable_ansi_console();


dds::cli::options opts; dds::cli::options opts;
debate::argument_parser parser; debate::argument_parser parser;

+ 2
- 0
src/dds/util/output.hpp View File



namespace dds { namespace dds {


void enable_ansi_console() noexcept;

bool stdout_is_a_tty() noexcept; bool stdout_is_a_tty() noexcept;


} // namespace dds } // namespace dds

+ 4
- 0
src/dds/util/output.nix.cpp View File



using namespace dds; using namespace dds;


void dds::enable_ansi_console() noexcept {
// unix consoles generally already support ANSI control chars by default
}

bool dds::stdout_is_a_tty() noexcept { return ::isatty(STDOUT_FILENO) != 0; } bool dds::stdout_is_a_tty() noexcept { return ::isatty(STDOUT_FILENO) != 0; }


#endif #endif

+ 29
- 2
src/dds/util/output.win.cpp View File

#include <dds/util/output.hpp>

#if _WIN32 #if _WIN32


#include <dds/util/output.hpp>
#include <windows.h>

void dds::enable_ansi_console() noexcept {
auto stdio_console = ::GetStdHandle(STD_OUTPUT_HANDLE);
if (stdio_console == INVALID_HANDLE_VALUE) {
// Oh well...
return;
}
DWORD mode = 0;
if (!::GetConsoleMode(stdio_console, &mode)) {
// Failed to get the mode?
return;
}
// Set the bit!
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
::SetConsoleMode(stdio_console, mode);
}


bool dds::stdout_is_a_tty() noexcept { bool dds::stdout_is_a_tty() noexcept {
// XXX: Newer Windows consoles support ANSI color, so this should be made smarter // XXX: Newer Windows consoles support ANSI color, so this should be made smarter
return false;
auto stdio_console = ::GetStdHandle(STD_OUTPUT_HANDLE);
if (stdio_console == INVALID_HANDLE_VALUE) {
return false;
}
DWORD mode = 0;
if (!::GetConsoleMode(stdio_console, &mode)) {
// Failed to get the mode
return false;
}
return (mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING);
} }


#endif #endif

Loading…
Cancel
Save