@@ -53,6 +53,7 @@ struct CONSOLE_SCREEN_BUFFER_INFO {
53
53
#[ link( name = "kernel32" ) ]
54
54
extern "system" {
55
55
fn SetConsoleTextAttribute ( handle : HANDLE , attr : WORD ) -> BOOL ;
56
+ fn GetConsoleMode ( handle : HANDLE , mode : * mut DWORD ) -> BOOL ;
56
57
fn GetStdHandle ( which : DWORD ) -> HANDLE ;
57
58
fn GetConsoleScreenBufferInfo ( handle : HANDLE , info : * mut CONSOLE_SCREEN_BUFFER_INFO ) -> BOOL ;
58
59
}
@@ -91,24 +92,30 @@ fn bits_to_color(bits: u16) -> color::Color {
91
92
color | ( u32:: from ( bits) & 0x8 ) // copy the hi-intensity bit
92
93
}
93
94
95
+ fn get_stdout_handle ( ) -> HANDLE {
96
+ unsafe {
97
+ // Magic -11 means stdout, from
98
+ // https://docs.microsoft.com/en-us/windows/console/getstdhandle
99
+ //
100
+ // You may be wondering, "but what about stderr?", and the answer
101
+ // to that is that setting terminal attributes on the stdout
102
+ // handle also sets them for stderr, since they go to the same
103
+ // terminal! Admittedly, this is fragile, since stderr could be
104
+ // redirected to a different console. This is good enough for
105
+ // rustc though. See #13400.
106
+ GetStdHandle ( -11i32 as DWORD )
107
+ }
108
+ }
109
+
94
110
impl < T : Write + Send + ' static > WinConsole < T > {
95
111
fn apply ( & mut self ) {
96
112
let _unused = self . buf . flush ( ) ;
97
113
let mut accum: WORD = 0 ;
98
114
accum |= color_to_bits ( self . foreground ) ;
99
115
accum |= color_to_bits ( self . background ) << 4 ;
100
116
117
+ let out = get_stdout_handle ( ) ;
101
118
unsafe {
102
- // Magic -11 means stdout, from
103
- // https://docs.microsoft.com/en-us/windows/console/getstdhandle
104
- //
105
- // You may be wondering, "but what about stderr?", and the answer
106
- // to that is that setting terminal attributes on the stdout
107
- // handle also sets them for stderr, since they go to the same
108
- // terminal! Admittedly, this is fragile, since stderr could be
109
- // redirected to a different console. This is good enough for
110
- // rustc though. See #13400.
111
- let out = GetStdHandle ( -11i32 as DWORD ) ;
112
119
SetConsoleTextAttribute ( out, accum) ;
113
120
}
114
121
}
@@ -160,6 +167,21 @@ impl<T: Write + Send + 'static> Terminal for WinConsole<T> {
160
167
Ok ( true )
161
168
}
162
169
170
+ fn supports_ansi_colors ( & self ) -> bool {
171
+ // From https://docs.microsoft.com/en-us/windows/console/getconsolemode
172
+ const ENABLE_VIRTUAL_TERMINAL_PROCESSING : DWORD = 0x0004 ;
173
+
174
+ let stdout = get_stdout_handle ( ) ;
175
+ let mut mode: DWORD = 0 ;
176
+ unsafe {
177
+ if GetConsoleMode ( stdout, & mut mode) != 0 {
178
+ mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0
179
+ } else {
180
+ false
181
+ }
182
+ }
183
+ }
184
+
163
185
fn reset ( & mut self ) -> io:: Result < bool > {
164
186
self . foreground = self . def_foreground ;
165
187
self . background = self . def_background ;
0 commit comments