44
55use std:: io;
66use std:: io:: prelude:: * ;
7+ use std:: os:: windows:: { io:: AsRawHandle , raw:: HANDLE } ;
78
89use super :: color;
910use super :: Terminal ;
@@ -21,7 +22,6 @@ type SHORT = i16;
2122type WORD = u16 ;
2223type DWORD = u32 ;
2324type BOOL = i32 ;
24- type HANDLE = * mut u8 ;
2525
2626#[ allow( non_snake_case) ]
2727#[ repr( C ) ]
@@ -54,7 +54,6 @@ struct CONSOLE_SCREEN_BUFFER_INFO {
5454extern "system" {
5555 fn SetConsoleTextAttribute ( handle : HANDLE , attr : WORD ) -> BOOL ;
5656 fn GetConsoleMode ( handle : HANDLE , mode : * mut DWORD ) -> BOOL ;
57- fn GetStdHandle ( which : DWORD ) -> HANDLE ;
5857 fn GetConsoleScreenBufferInfo ( handle : HANDLE , info : * mut CONSOLE_SCREEN_BUFFER_INFO ) -> BOOL ;
5958}
6059
@@ -92,29 +91,14 @@ fn bits_to_color(bits: u16) -> color::Color {
9291 color | ( u32:: from ( bits) & 0x8 ) // copy the hi-intensity bit
9392}
9493
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-
11094impl < T : Write + Send + ' static > WinConsole < T > {
11195 fn apply ( & mut self ) {
11296 let _unused = self . buf . flush ( ) ;
11397 let mut accum: WORD = 0 ;
11498 accum |= color_to_bits ( self . foreground ) ;
11599 accum |= color_to_bits ( self . background ) << 4 ;
116100
117- let out = get_stdout_handle ( ) ;
101+ let out = std :: io :: stdout ( ) . as_raw_handle ( ) ;
118102 unsafe {
119103 SetConsoleTextAttribute ( out, accum) ;
120104 }
@@ -126,11 +110,10 @@ impl<T: Write + Send + 'static> WinConsole<T> {
126110
127111 let fg;
128112 let bg;
113+ let stdout = std:: io:: stdout ( ) . as_raw_handle ( ) ;
129114 unsafe {
130115 let mut buffer_info = MaybeUninit :: < CONSOLE_SCREEN_BUFFER_INFO > :: uninit ( ) ;
131- if GetConsoleScreenBufferInfo ( GetStdHandle ( -11i32 as DWORD ) , buffer_info. as_mut_ptr ( ) )
132- != 0
133- {
116+ if GetConsoleScreenBufferInfo ( stdout, buffer_info. as_mut_ptr ( ) ) != 0 {
134117 let buffer_info = buffer_info. assume_init ( ) ;
135118 fg = bits_to_color ( buffer_info. wAttributes ) ;
136119 bg = bits_to_color ( buffer_info. wAttributes >> 4 ) ;
@@ -171,7 +154,7 @@ impl<T: Write + Send + 'static> Terminal for WinConsole<T> {
171154 // From https://docs.microsoft.com/en-us/windows/console/getconsolemode
172155 const ENABLE_VIRTUAL_TERMINAL_PROCESSING : DWORD = 0x0004 ;
173156
174- let stdout = get_stdout_handle ( ) ;
157+ let stdout = std :: io :: stdout ( ) . as_raw_handle ( ) ;
175158 let mut mode: DWORD = 0 ;
176159 unsafe {
177160 if GetConsoleMode ( stdout, & mut mode) != 0 {
0 commit comments