v1-0001-command-line-colorization-on-windows.patch
application/octet-stream
Filename: v1-0001-command-line-colorization-on-windows.patch
Type: application/octet-stream
Part: 0
Patch
Format: unified
Series: patch v1-0001
| File | + | − |
|---|---|---|
| doc/src/sgml/release-12.sgml | 4 | 0 |
| src/common/logging.c | 32 | 0 |
diff --git a/doc/src/sgml/release-12.sgml b/doc/src/sgml/release-12.sgml
index 68949b2026..26b94bf4e4 100644
--- a/doc/src/sgml/release-12.sgml
+++ b/doc/src/sgml/release-12.sgml
@@ -3825,6 +3825,10 @@ Author: Peter Eisentraut <peter@eisentraut.org>
For example, the default behavior is equivalent to
<literal>PG_COLORS="error=01;31:warning=01;35:locus=01"</literal>.
</para>
+
+ <para>
+ Notably, VT100 support is required for the colorization on Windows.
+ </para>
</listitem>
</itemizedlist>
diff --git a/src/common/logging.c b/src/common/logging.c
index 895da7150e..8cb59fb609 100644
--- a/src/common/logging.c
+++ b/src/common/logging.c
@@ -32,6 +32,31 @@ static const char *sgr_locus = NULL;
#define ANSI_ESCAPE_FMT "\x1b[%sm"
#define ANSI_ESCAPE_RESET "\x1b[0m"
+#ifdef WIN32
+/*
+ * Check Windows support for VT100
+ */
+static bool
+enable_vt_mode()
+{
+ /* Check stderr */
+ HANDLE hOut = GetStdHandle(STD_ERROR_HANDLE);
+ DWORD dwMode = 0;
+
+ if (hOut == INVALID_HANDLE_VALUE)
+ return false;
+
+ if (!GetConsoleMode(hOut, &dwMode))
+ return false;
+
+ dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
+ if (!SetConsoleMode(hOut, dwMode))
+ return false;
+
+ return true;
+}
+#endif
+
/*
* This should be called before any output happens.
*/
@@ -47,6 +72,13 @@ pg_logging_init(const char *argv0)
progname = get_progname(argv0);
__pg_log_level = PG_LOG_INFO;
+#ifdef WIN32
+ bool vt_mode = enable_vt_mode();
+
+ if(!vt_mode)
+ pg_color_env = NULL;
+#endif
+
if (pg_color_env)
{
if (strcmp(pg_color_env, "always") == 0 ||