-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new `terminal_size_of` fuctions which use the `AsFd` and `AsHandle` traits to accept file descriptors (on Unix) and handles (on Windows). Deprecate the existing `terminal_size_using_*` functions which take raw file descriptors or handles without being `unsafe`. And update the `get_size` example to use the new `terminal_size_of` functions. The example can now be much simpler because the main API is now the same between Windows and Unix in common cases.
- Loading branch information
1 parent
b15c997
commit 56334c3
Showing
4 changed files
with
51 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,19 @@ | ||
#[cfg(windows)] | ||
fn run() { | ||
use std::os::windows::io::RawHandle; | ||
use windows_sys::Win32::System::Console::{ | ||
GetStdHandle, STD_ERROR_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, | ||
}; | ||
|
||
let stdout = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) } as RawHandle; | ||
println!( | ||
"Size from terminal_size_using_handle(stdout): {:?}", | ||
terminal_size::terminal_size_using_handle(stdout) | ||
); | ||
|
||
let stderr = unsafe { GetStdHandle(STD_ERROR_HANDLE) } as RawHandle; | ||
println!( | ||
"Size from terminal_size_using_handle(stderr): {:?}", | ||
terminal_size::terminal_size_using_handle(stderr) | ||
); | ||
|
||
let stdin = unsafe { GetStdHandle(STD_INPUT_HANDLE) } as RawHandle; | ||
fn main() { | ||
println!( | ||
"Size from terminal_size_using_handle(stdin): {:?}", | ||
terminal_size::terminal_size_using_handle(stdin) | ||
"Size from terminal_size(): {:?}", | ||
terminal_size::terminal_size() | ||
); | ||
} | ||
|
||
#[cfg(not(windows))] | ||
fn run() { | ||
use std::os::unix::io::AsRawFd; | ||
|
||
println!( | ||
"Size from terminal_size_using_fd(stdout): {:?}", | ||
terminal_size::terminal_size_using_fd(std::io::stdout().as_raw_fd()) | ||
"Size from terminal_size_of(stdout): {:?}", | ||
terminal_size::terminal_size_of(std::io::stdout()) | ||
); | ||
println!( | ||
"Size from terminal_size_using_fd(stderr): {:?}", | ||
terminal_size::terminal_size_using_fd(std::io::stderr().as_raw_fd()) | ||
"Size from terminal_size_of(stderr): {:?}", | ||
terminal_size::terminal_size_of(std::io::stderr()) | ||
); | ||
println!( | ||
"Size from terminal_size_using_fd(stdin): {:?}", | ||
terminal_size::terminal_size_using_fd(std::io::stdin().as_raw_fd()) | ||
); | ||
} | ||
|
||
fn main() { | ||
println!( | ||
"Size from terminal_size(): {:?}", | ||
terminal_size::terminal_size() | ||
"Size from terminal_size_of(stdin): {:?}", | ||
terminal_size::terminal_size_of(std::io::stdin()) | ||
); | ||
|
||
run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters