4
4
5
5
use std:: io;
6
6
use std:: io:: prelude:: * ;
7
+ use std:: os:: windows:: { io:: AsRawHandle , raw:: HANDLE } ;
7
8
8
9
use super :: color;
9
10
use super :: Terminal ;
@@ -21,7 +22,6 @@ type SHORT = i16;
21
22
type WORD = u16 ;
22
23
type DWORD = u32 ;
23
24
type BOOL = i32 ;
24
- type HANDLE = * mut u8 ;
25
25
26
26
#[ allow( non_snake_case) ]
27
27
#[ repr( C ) ]
@@ -54,7 +54,6 @@ struct CONSOLE_SCREEN_BUFFER_INFO {
54
54
extern "system" {
55
55
fn SetConsoleTextAttribute ( handle : HANDLE , attr : WORD ) -> BOOL ;
56
56
fn GetConsoleMode ( handle : HANDLE , mode : * mut DWORD ) -> BOOL ;
57
- fn GetStdHandle ( which : DWORD ) -> HANDLE ;
58
57
fn GetConsoleScreenBufferInfo ( handle : HANDLE , info : * mut CONSOLE_SCREEN_BUFFER_INFO ) -> BOOL ;
59
58
}
60
59
@@ -92,29 +91,14 @@ fn bits_to_color(bits: u16) -> color::Color {
92
91
color | ( u32:: from ( bits) & 0x8 ) // copy the hi-intensity bit
93
92
}
94
93
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
-
110
94
impl < T : Write + Send + ' static > WinConsole < T > {
111
95
fn apply ( & mut self ) {
112
96
let _unused = self . buf . flush ( ) ;
113
97
let mut accum: WORD = 0 ;
114
98
accum |= color_to_bits ( self . foreground ) ;
115
99
accum |= color_to_bits ( self . background ) << 4 ;
116
100
117
- let out = get_stdout_handle ( ) ;
101
+ let out = std :: io :: stdout ( ) . as_raw_handle ( ) ;
118
102
unsafe {
119
103
SetConsoleTextAttribute ( out, accum) ;
120
104
}
@@ -126,11 +110,10 @@ impl<T: Write + Send + 'static> WinConsole<T> {
126
110
127
111
let fg;
128
112
let bg;
113
+ let stdout = std:: io:: stdout ( ) . as_raw_handle ( ) ;
129
114
unsafe {
130
115
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 {
134
117
let buffer_info = buffer_info. assume_init ( ) ;
135
118
fg = bits_to_color ( buffer_info. wAttributes ) ;
136
119
bg = bits_to_color ( buffer_info. wAttributes >> 4 ) ;
@@ -171,7 +154,7 @@ impl<T: Write + Send + 'static> Terminal for WinConsole<T> {
171
154
// From https://docs.microsoft.com/en-us/windows/console/getconsolemode
172
155
const ENABLE_VIRTUAL_TERMINAL_PROCESSING : DWORD = 0x0004 ;
173
156
174
- let stdout = get_stdout_handle ( ) ;
157
+ let stdout = std :: io :: stdout ( ) . as_raw_handle ( ) ;
175
158
let mut mode: DWORD = 0 ;
176
159
unsafe {
177
160
if GetConsoleMode ( stdout, & mut mode) != 0 {
0 commit comments