Debian Codegraph @

debian/rust-vendor/terminal_size/src/windows.rs

raw
1 use super::{Height, Width};
2 use std::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle, RawHandle};
3
4 /// Returns the size of the terminal.
5 ///
6 /// This function checks the stdout, stderr, and stdin streams (in that order).
7 /// The size of the first stream that is a TTY will be returned. If nothing
8 /// is a TTY, then `None` is returned.
9 ///
10 /// Note that this returns the size of the actual command window, and
11 /// not the overall size of the command window buffer
12 pub fn terminal_size() -> Option<(Width, Height)> {
13 use windows_sys::Win32::System::Console::{
14 GetStdHandle, STD_ERROR_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
15 };
16
17 if let Some(size) = terminal_size_of(unsafe {
18 BorrowedHandle::borrow_raw(GetStdHandle(STD_OUTPUT_HANDLE) as RawHandle)
19 }) {
20 Some(size)
21 } else if let Some(size) = terminal_size_of(unsafe {
22 BorrowedHandle::borrow_raw(GetStdHandle(STD_ERROR_HANDLE) as RawHandle)
23 }) {
24 Some(size)
25 } else if let Some(size) = terminal_size_of(unsafe {
26 BorrowedHandle::borrow_raw(GetStdHandle(STD_INPUT_HANDLE) as RawHandle)
27 }) {
28 Some(size)
29 } else {
30 None
31 }
32 }
33
34 /// Returns the size of the terminal using the given handle, if available.
35 ///
36 /// If the given handle is not a tty, returns `None`
37 pub fn terminal_size_of<Handle: AsHandle>(handle: Handle) -> Option<(Width, Height)> {
38 use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE;
39 use windows_sys::Win32::System::Console::{
40 GetConsoleScreenBufferInfo, CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT,
41 };
42
43 // convert between windows_sys::Win32::Foundation::HANDLE and std::os::windows::raw::HANDLE
44 let hand = handle.as_handle().as_raw_handle() as windows_sys::Win32::Foundation::HANDLE;
45
46 if hand == INVALID_HANDLE_VALUE {
47 return None;
48 }
49
50 let zc = COORD { X: 0, Y: 0 };
51 let mut csbi = CONSOLE_SCREEN_BUFFER_INFO {
52 dwSize: zc,
53 dwCursorPosition: zc,
54 wAttributes: 0,
55 srWindow: SMALL_RECT {
56 Left: 0,
57 Top: 0,
58 Right: 0,
59 Bottom: 0,
60 },
61 dwMaximumWindowSize: zc,
62 };
63 if unsafe { GetConsoleScreenBufferInfo(hand, &mut csbi) } == 0 {
64 return None;
65 }
66
67 let w: Width = Width((csbi.srWindow.Right - csbi.srWindow.Left + 1) as u16);
68 let h: Height = Height((csbi.srWindow.Bottom - csbi.srWindow.Top + 1) as u16);
69 Some((w, h))
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 }