Skip to content

Migrate from winapi to windows-sys. #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ edition = "2018"
[target.'cfg(not(windows))'.dependencies.libc]
version = "0.2"

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.36.0"
features = [
"handleapi",
"processenv",
"winbase",
"wincon",
"winnt",
"Win32_Foundation",
"Win32_System_Console",
]
5 changes: 3 additions & 2 deletions examples/get_size.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#[cfg(windows)]
fn run() {
use std::os::windows::io::RawHandle;
use winapi::um::processenv::GetStdHandle;
use winapi::um::winbase::{STD_ERROR_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE};
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!(
Expand Down
11 changes: 5 additions & 6 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use std::os::windows::io::RawHandle;
/// Note that this returns the size of the actual command window, and
/// not the overall size of the command window buffer
pub fn terminal_size() -> Option<(Width, Height)> {
use winapi::um::processenv::GetStdHandle;
use winapi::um::winbase::STD_OUTPUT_HANDLE;
use windows_sys::Win32::System::Console::{GetStdHandle, STD_OUTPUT_HANDLE};

let handle = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) as RawHandle };

Expand All @@ -18,13 +17,13 @@ pub fn terminal_size() -> Option<(Width, Height)> {
///
/// If the given handle is not a tty, returns `None`
pub fn terminal_size_using_handle(handle: RawHandle) -> Option<(Width, Height)> {
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
use winapi::um::wincon::{
use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE;
use windows_sys::Win32::System::Console::{
GetConsoleScreenBufferInfo, CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT,
};

// convert between winapi::um::winnt::HANDLE and std::os::windows::raw::HANDLE
let hand = handle as winapi::um::winnt::HANDLE;
// convert between windows_sys::Win32::Foundation::HANDLE and std::os::windows::raw::HANDLE
let hand = handle as windows_sys::Win32::Foundation::HANDLE;

if hand == INVALID_HANDLE_VALUE {
return None;
Expand Down