Ver código fonte

Support styled output in Windows consoles

default_compile_flags
vector-of-bool 3 anos atrás
pai
commit
0b87d6efcc
4 arquivos alterados com 37 adições e 2 exclusões
  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 Ver arquivo

@@ -1,6 +1,7 @@
#include <dds/cli/dispatch_main.hpp>
#include <dds/cli/options.hpp>
#include <dds/util/log.hpp>
#include <dds/util/output.hpp>
#include <dds/util/signal.hpp>

#include <debate/debate.hpp>
@@ -35,6 +36,7 @@ int main_fn(std::string_view program_name, const std::vector<std::string>& argv)
std::setlocale(LC_CTYPE, ".utf8");

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

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

+ 2
- 0
src/dds/util/output.hpp Ver arquivo

@@ -2,6 +2,8 @@

namespace dds {

void enable_ansi_console() noexcept;

bool stdout_is_a_tty() noexcept;

} // namespace dds

+ 4
- 0
src/dds/util/output.nix.cpp Ver arquivo

@@ -6,6 +6,10 @@

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; }

#endif

+ 29
- 2
src/dds/util/output.win.cpp Ver arquivo

@@ -1,10 +1,37 @@
#include <dds/util/output.hpp>

#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 {
// 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

Carregando…
Cancelar
Salvar