]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tool: support bold headers in Windows
authorJay Satiro <raysatiro@yahoo.com>
Tue, 14 Nov 2023 09:11:04 +0000 (04:11 -0500)
committerJay Satiro <raysatiro@yahoo.com>
Thu, 16 Nov 2023 08:47:18 +0000 (03:47 -0500)
- If virtual terminal processing is enabled in Windows then use ANSI
  escape codes Esc[1m and Esc[22m to turn bold on and off.

Suggested-by: Gisle Vanem
Ref: https://github.com/curl/curl/discussions/11770

Closes https://github.com/curl/curl/pull/12321

src/tool_cb_hdr.c
src/tool_doswin.c
src/tool_setup.h

index a310e398467b2b4dc78d6ae45ef65861812f7818..3571fd35ceeb9dcc7165ef37d9a46123625555a8 100644 (file)
@@ -42,8 +42,8 @@
 static char *parse_filename(const char *ptr, size_t len);
 
 #ifdef WIN32
-#define BOLD
-#define BOLDOFF
+#define BOLD "\x1b[1m"
+#define BOLDOFF "\x1b[22m"
 #else
 #define BOLD "\x1b[1m"
 /* Switch off bold by setting "all attributes off" since the explicit
@@ -212,7 +212,11 @@ size_t tool_header_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
     if(!outs->stream && !tool_create_output_file(outs, per->config))
       return CURL_WRITEFUNC_ERROR;
 
-    if(hdrcbdata->global->isatty && hdrcbdata->global->styled_output)
+    if(hdrcbdata->global->isatty &&
+#ifdef WIN32
+       tool_term_has_bold &&
+#endif
+       hdrcbdata->global->styled_output)
       value = memchr(ptr, ':', cb);
     if(value) {
       size_t namelen = value - ptr;
index faa5755e6a06d6773893d0855e1fd4a7af0867c2..e2f250eecc23b33a86d0bdf50fde2ec1213a38f5 100644 (file)
@@ -714,6 +714,8 @@ static struct TerminalSettings {
 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
 #endif
 
+bool tool_term_has_bold;
+
 static void restore_terminal(void)
 {
   if(InterlockedExchange(&TerminalSettings.valid, (LONG)FALSE))
@@ -733,16 +735,23 @@ static BOOL WINAPI signal_handler(DWORD type)
 static void init_terminal(void)
 {
   TerminalSettings.hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
+
   /*
    * Enable VT (Virtual Terminal) output.
    * Note: VT mode flag can be set on any version of Windows, but VT
-   * processing only performed on Win10 >= Creators Update)
+   * processing only performed on Win10 >= version 1709 (OS build 16299)
+   * Creator's Update. Also, ANSI bold on/off supported since then.
    */
-  if((TerminalSettings.hStdOut != INVALID_HANDLE_VALUE) &&
-     GetConsoleMode(TerminalSettings.hStdOut,
-                    &TerminalSettings.dwOutputMode) &&
-     !(TerminalSettings.dwOutputMode &
-       ENABLE_VIRTUAL_TERMINAL_PROCESSING)) {
+  if(TerminalSettings.hStdOut == INVALID_HANDLE_VALUE ||
+     !GetConsoleMode(TerminalSettings.hStdOut,
+                     &TerminalSettings.dwOutputMode) ||
+     !curlx_verify_windows_version(10, 0, 16299, PLATFORM_WINNT,
+                                   VERSION_GREATER_THAN_EQUAL))
+    return;
+
+  if((TerminalSettings.dwOutputMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
+    tool_term_has_bold = true;
+  else {
     /* The signal handler is set before attempting to change the console mode
        because otherwise a signal would not be caught after the change but
        before the handler was installed. */
@@ -751,6 +760,7 @@ static void init_terminal(void)
       if(SetConsoleMode(TerminalSettings.hStdOut,
                         (TerminalSettings.dwOutputMode |
                          ENABLE_VIRTUAL_TERMINAL_PROCESSING))) {
+        tool_term_has_bold = true;
         atexit(restore_terminal);
       }
       else {
index 48b3556402dccb33b92fa800393b8012e16b6632..71c9decb23e04122ac1cc9ec4d00d98a21552461 100644 (file)
@@ -70,6 +70,8 @@ extern FILE *tool_stderr;
 /* set in win32_init() */
 extern LARGE_INTEGER tool_freq;
 extern bool tool_isVistaOrGreater;
+/* set in init_terminal() */
+extern bool tool_term_has_bold;
 #endif
 
 #endif /* HEADER_CURL_TOOL_SETUP_H */