Fossil SCM

Check if VT100 support is available to restore compatibility of console output with Legacy Consoles (from Windows XP to Windows 11).

florian 2025-03-24 07:16 trunk
Commit 360abc5e827dd94bc640821989252ac4fe360c12525684e0e3061724acd815cc
1 file changed +39
--- src/terminal.c
+++ src/terminal.c
@@ -144,12 +144,51 @@
144144
/*
145145
** Return true if it is reasonable is emit VT100 escape codes.
146146
*/
147147
int terminal_is_vt100(void){
148148
char *zNoColor;
149
+#ifdef _WIN32
150
+ if( !win32_terminal_is_vt100(1) ) return 0;
151
+#endif /* _WIN32 */
149152
if( !fossil_isatty(1) ) return 0;
150153
zNoColor =fossil_getenv("NO_COLOR");
151154
if( zNoColor==0 ) return 1;
152155
if( zNoColor[0]==0 ) return 1;
153156
if( is_false(zNoColor) ) return 1;
154157
return 0;
155158
}
159
+
160
+#ifdef _WIN32
161
+/*
162
+** Return true if the Windows console supports VT100 escape codes.
163
+**
164
+** Support for VT100 escape codes is enabled by default in Windows Terminal
165
+** on Windows 10 and Windows 11, and disabled by default in Legacy Consoles
166
+** and on older versions of Windows. Programs can turn on VT100 support for
167
+** Legacy Consoles using the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag.
168
+**
169
+** NOTE: If this function needs to be called in more complex scenarios with
170
+** reassigned stdout and stderr streams, the following CRT calls are useful
171
+** to translate from CRT streams to file descriptors and to Win32 handles:
172
+**
173
+** HANDLE hOutputHandle = (HANDLE)_get_osfhandle(_fileno(<FILE*>));
174
+*/
175
+#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
176
+#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
177
+#endif
178
+int win32_terminal_is_vt100(int fd){
179
+ HANDLE hConsole = NULL;
180
+ DWORD dwConsoleMode;
181
+ switch( fd ){
182
+ case 1:
183
+ hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
184
+ break;
185
+ case 2:
186
+ hConsole = GetStdHandle(STD_ERROR_HANDLE);
187
+ break;
188
+ }
189
+ if( GetConsoleMode(hConsole,&dwConsoleMode) ){
190
+ return (dwConsoleMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)!=0;
191
+ }
192
+ return 0;
193
+}
194
+#endif /* _WIN32 */
156195
--- src/terminal.c
+++ src/terminal.c
@@ -144,12 +144,51 @@
144 /*
145 ** Return true if it is reasonable is emit VT100 escape codes.
146 */
147 int terminal_is_vt100(void){
148 char *zNoColor;
 
 
 
149 if( !fossil_isatty(1) ) return 0;
150 zNoColor =fossil_getenv("NO_COLOR");
151 if( zNoColor==0 ) return 1;
152 if( zNoColor[0]==0 ) return 1;
153 if( is_false(zNoColor) ) return 1;
154 return 0;
155 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
--- src/terminal.c
+++ src/terminal.c
@@ -144,12 +144,51 @@
144 /*
145 ** Return true if it is reasonable is emit VT100 escape codes.
146 */
147 int terminal_is_vt100(void){
148 char *zNoColor;
149 #ifdef _WIN32
150 if( !win32_terminal_is_vt100(1) ) return 0;
151 #endif /* _WIN32 */
152 if( !fossil_isatty(1) ) return 0;
153 zNoColor =fossil_getenv("NO_COLOR");
154 if( zNoColor==0 ) return 1;
155 if( zNoColor[0]==0 ) return 1;
156 if( is_false(zNoColor) ) return 1;
157 return 0;
158 }
159
160 #ifdef _WIN32
161 /*
162 ** Return true if the Windows console supports VT100 escape codes.
163 **
164 ** Support for VT100 escape codes is enabled by default in Windows Terminal
165 ** on Windows 10 and Windows 11, and disabled by default in Legacy Consoles
166 ** and on older versions of Windows. Programs can turn on VT100 support for
167 ** Legacy Consoles using the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag.
168 **
169 ** NOTE: If this function needs to be called in more complex scenarios with
170 ** reassigned stdout and stderr streams, the following CRT calls are useful
171 ** to translate from CRT streams to file descriptors and to Win32 handles:
172 **
173 ** HANDLE hOutputHandle = (HANDLE)_get_osfhandle(_fileno(<FILE*>));
174 */
175 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
176 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
177 #endif
178 int win32_terminal_is_vt100(int fd){
179 HANDLE hConsole = NULL;
180 DWORD dwConsoleMode;
181 switch( fd ){
182 case 1:
183 hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
184 break;
185 case 2:
186 hConsole = GetStdHandle(STD_ERROR_HANDLE);
187 break;
188 }
189 if( GetConsoleMode(hConsole,&dwConsoleMode) ){
190 return (dwConsoleMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)!=0;
191 }
192 return 0;
193 }
194 #endif /* _WIN32 */
195

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button