1
1
use super :: { Height , Width } ;
2
- use std:: os:: windows:: io:: RawHandle ;
2
+ use std:: os:: windows:: io:: { AsHandle , AsRawHandle , BorrowedHandle , RawHandle } ;
3
3
4
4
/// Returns the size of the terminal.
5
5
///
@@ -14,17 +14,17 @@ pub fn terminal_size() -> Option<(Width, Height)> {
14
14
GetStdHandle , STD_ERROR_HANDLE , STD_INPUT_HANDLE , STD_OUTPUT_HANDLE ,
15
15
} ;
16
16
17
- if let Some ( size) =
18
- terminal_size_using_handle ( unsafe { GetStdHandle ( STD_OUTPUT_HANDLE ) as RawHandle } )
19
- {
17
+ if let Some ( size) = terminal_size_of ( unsafe {
18
+ BorrowedHandle :: borrow_raw ( GetStdHandle ( STD_OUTPUT_HANDLE ) as RawHandle )
19
+ } ) {
20
20
Some ( size)
21
- } else if let Some ( size) =
22
- terminal_size_using_handle ( unsafe { GetStdHandle ( STD_ERROR_HANDLE ) as RawHandle } )
23
- {
21
+ } else if let Some ( size) = terminal_size_of ( unsafe {
22
+ BorrowedHandle :: borrow_raw ( GetStdHandle ( STD_ERROR_HANDLE ) as RawHandle )
23
+ } ) {
24
24
Some ( size)
25
- } else if let Some ( size) =
26
- terminal_size_using_handle ( unsafe { GetStdHandle ( STD_INPUT_HANDLE ) as RawHandle } )
27
- {
25
+ } else if let Some ( size) = terminal_size_of ( unsafe {
26
+ BorrowedHandle :: borrow_raw ( GetStdHandle ( STD_INPUT_HANDLE ) as RawHandle )
27
+ } ) {
28
28
Some ( size)
29
29
} else {
30
30
None
@@ -34,14 +34,14 @@ pub fn terminal_size() -> Option<(Width, Height)> {
34
34
/// Returns the size of the terminal using the given handle, if available.
35
35
///
36
36
/// If the given handle is not a tty, returns `None`
37
- pub fn terminal_size_using_handle ( handle : RawHandle ) -> Option < ( Width , Height ) > {
37
+ pub fn terminal_size_of < Handle : AsHandle > ( handle : Handle ) -> Option < ( Width , Height ) > {
38
38
use windows_sys:: Win32 :: Foundation :: INVALID_HANDLE_VALUE ;
39
39
use windows_sys:: Win32 :: System :: Console :: {
40
40
GetConsoleScreenBufferInfo , CONSOLE_SCREEN_BUFFER_INFO , COORD , SMALL_RECT ,
41
41
} ;
42
42
43
43
// convert between windows_sys::Win32::Foundation::HANDLE and std::os::windows::raw::HANDLE
44
- let hand = handle as windows_sys:: Win32 :: Foundation :: HANDLE ;
44
+ let hand = handle. as_handle ( ) . as_raw_handle ( ) as windows_sys:: Win32 :: Foundation :: HANDLE ;
45
45
46
46
if hand == INVALID_HANDLE_VALUE {
47
47
return None ;
@@ -68,3 +68,18 @@ pub fn terminal_size_using_handle(handle: RawHandle) -> Option<(Width, Height)>
68
68
let h: Height = Height ( ( csbi. srWindow . Bottom - csbi. srWindow . Top + 1 ) as u16 ) ;
69
69
Some ( ( w, h) )
70
70
}
71
+
72
+ /// Returns the size of the terminal using the given handle, if available.
73
+ ///
74
+ /// The given handle must be an open handle.
75
+ ///
76
+ /// If the given handle is not a tty, returns `None`
77
+ ///
78
+ /// # Safety
79
+ ///
80
+ /// `handle` must be a valid open file handle.
81
+ #[ deprecated( note = "Use `terminal_size_of` instead.
82
+ Use `BorrowedHandle::borrow_raw` to convert a raw handle into a `BorrowedHandle` if needed." ) ]
83
+ pub unsafe fn terminal_size_using_handle ( handle : RawHandle ) -> Option < ( Width , Height ) > {
84
+ terminal_size_of ( BorrowedHandle :: borrow_raw ( handle) )
85
+ }
0 commit comments