Skip to content

Commit

Permalink
Address Issue #10
Browse files Browse the repository at this point in the history
  • Loading branch information
Nopvzutys committed Jan 3, 2025
1 parent d72ec08 commit f4073c6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 78 deletions.
24 changes: 1 addition & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 8 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,13 @@ version = "0.29.0"
default-features = false
features = ["fs", "term"]

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3.9"
[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.59.0"
default-features = false
features = [
"consoleapi",
"errhandlingapi",
"handleapi",
"impl-default",
"lmapibuf",
"lmserver",
"lmwksta",
"processenv",
"winbase",
"wincon",
"Win32_Foundation",
"Win32_System_Console",
"Win32_System_SystemInformation",
"Win32_System_SystemServices",
"Win32_NetworkManagement_NetManagement",
]
2 changes: 1 addition & 1 deletion examples/clscli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn main() -> Result<(), Error> {

Ok(())
} else {
println!("Usage: cargo run --example cli -- <variant>\nWhere <variant> is one of the ClearScreen enum variants, same casing, or 'auto'.\nI recommend piping into `hexdump -C` to see what’s happening.");
println!("Usage: cargo run --example clscli -- <variant>\nWhere <variant> is one of the ClearScreen enum variants, same casing, or 'auto'.\nI recommend piping into `hexdump -C` to see what’s happening.");
Ok(())
}
}
Expand Down
84 changes: 42 additions & 42 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,41 +837,33 @@ mod unix {
}

#[cfg(windows)]
/// TODO: Safety comments in unsafe {}
mod win {
use super::Error;

use std::{convert::TryFrom, io, mem::size_of, ptr};

use winapi::{
shared::minwindef::{DWORD, FALSE},
um::{
consoleapi::{GetConsoleMode, SetConsoleMode},
handleapi::INVALID_HANDLE_VALUE,
lmapibuf::{NetApiBufferAllocate, NetApiBufferFree},
lmserver::{NetServerGetInfo, MAJOR_VERSION_MASK, SERVER_INFO_101, SV_PLATFORM_ID_NT},
lmwksta::{NetWkstaGetInfo, WKSTA_INFO_100},
processenv::GetStdHandle,
winbase::{VerifyVersionInfoW, STD_OUTPUT_HANDLE},
wincon::{
ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT,
ENABLE_VIRTUAL_TERMINAL_PROCESSING,
},
winnt::{
VerSetConditionMask, HANDLE, OSVERSIONINFOEXW, POSVERSIONINFOEXW, ULONGLONG,
VER_GREATER_EQUAL, VER_MAJORVERSION, VER_MINORVERSION, VER_SERVICEPACKMAJOR,
},
},
use std::{io, ptr};

use windows_sys::Win32::Foundation::{FALSE, HANDLE, INVALID_HANDLE_VALUE};
use windows_sys::Win32::NetworkManagement::NetManagement::{
NetApiBufferAllocate, NetApiBufferFree, NetServerGetInfo, NetWkstaGetInfo,
MAJOR_VERSION_MASK, SERVER_INFO_101, SV_PLATFORM_ID_NT, WKSTA_INFO_100,
};
use windows_sys::Win32::System::Console::{
GetConsoleMode, GetStdHandle, SetConsoleMode, CONSOLE_MODE, ENABLE_ECHO_INPUT,
ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT, ENABLE_VIRTUAL_TERMINAL_PROCESSING,
STD_OUTPUT_HANDLE,
};
use windows_sys::Win32::System::SystemInformation::{
VerSetConditionMask, VerifyVersionInfoW, OSVERSIONINFOEXW, VER_MAJORVERSION,
VER_MINORVERSION, VER_SERVICEPACKMAJOR,
};
use windows_sys::Win32::System::SystemServices::VER_GREATER_EQUAL;

#[cfg(feature = "windows-console")]
use winapi::um::{
wincon::{
FillConsoleOutputAttribute, FillConsoleOutputCharacterW, GetConsoleScreenBufferInfo,
ScrollConsoleScreenBufferW, SetConsoleCursorPosition, CONSOLE_SCREEN_BUFFER_INFO,
PCONSOLE_SCREEN_BUFFER_INFO,
},
wincontypes::{CHAR_INFO_Char, CHAR_INFO, COORD, SMALL_RECT},
winnt::SHORT,
use windows_sys::Win32::System::Console::{
FillConsoleOutputAttribute, FillConsoleOutputCharacterW, GetConsoleScreenBufferInfo,
ScrollConsoleScreenBufferW, SetConsoleCursorPosition, CHAR_INFO, CHAR_INFO_0,
CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT,
};

fn console_handle() -> Result<HANDLE, Error> {
Expand All @@ -883,7 +875,7 @@ mod win {

#[cfg(feature = "windows-console")]
fn buffer_info(console: HANDLE) -> Result<CONSOLE_SCREEN_BUFFER_INFO, Error> {
let csbi: PCONSOLE_SCREEN_BUFFER_INFO = ptr::null_mut();
let csbi: *mut CONSOLE_SCREEN_BUFFER_INFO = ptr::null_mut();
if unsafe { GetConsoleScreenBufferInfo(console, csbi) } == FALSE {
return Err(io::Error::last_os_error().into());
}
Expand Down Expand Up @@ -928,12 +920,13 @@ mod win {
// Scroll it upwards off the top of the buffer with a magnitude of the entire height.
let target = COORD {
X: 0,
Y: (0 - csbi.dwSize.Y) as SHORT,
Y: (0 - csbi.dwSize.Y) as i16,
};

// Fill with empty spaces with the buffer’s default text attribute.
let mut space = CHAR_INFO_Char::default();
unsafe { *space.AsciiChar_mut() = b' ' as i8 };
let space = CHAR_INFO_0 {
AsciiChar: b' ' as i8,
};

let fill = CHAR_INFO {
Char: space,
Expand Down Expand Up @@ -1006,7 +999,7 @@ mod win {
Ok(())
}

const ENABLE_COOKED_MODE: DWORD =
const ENABLE_COOKED_MODE: CONSOLE_MODE =
ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;

pub(crate) fn cooked() -> Result<(), Error> {
Expand All @@ -1031,28 +1024,35 @@ mod win {
// proper way, requires manifesting
#[inline]
fn um_verify_version() -> bool {
let condition_mask: ULONGLONG = unsafe {
let condition_mask: u64 = unsafe {
VerSetConditionMask(
VerSetConditionMask(
VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL),
VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL as u8),
VER_MINORVERSION,
VER_GREATER_EQUAL,
VER_GREATER_EQUAL as u8,
),
VER_SERVICEPACKMAJOR,
VER_GREATER_EQUAL,
VER_GREATER_EQUAL as u8,
)
};

let mut osvi = OSVERSIONINFOEXW {
dwMinorVersion: ABRACADABRA_THRESHOLD.1 as _,
dwMajorVersion: ABRACADABRA_THRESHOLD.0 as _,
wServicePackMajor: 0,
..OSVERSIONINFOEXW::default()
dwOSVersionInfoSize: size_of::<OSVERSIONINFOEXW>() as u32,
dwBuildNumber: 0,
dwPlatformId: 0,
szCSDVersion: [0; 128],
wServicePackMinor: 0,
wSuiteMask: 0,
wProductType: 0,
wReserved: 0,
};

let ret = unsafe {
VerifyVersionInfoW(
&mut osvi as POSVERSIONINFOEXW,
&mut osvi,
VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR,
condition_mask,
)
Expand Down Expand Up @@ -1249,14 +1249,14 @@ impl<'a, T: AsRef<&'a [u8]>> From<T> for ResetScrollback<'a> {
}
}

impl<'a> AsRef<[u8]> for ResetScrollback<'a> {
impl AsRef<[u8]> for ResetScrollback<'_> {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.0
}
}

impl<'a> ResetScrollback<'a> {
impl ResetScrollback<'_> {
#[inline]
fn expand(&self) -> Expansion<Self> {
#[allow(dead_code)]
Expand Down

0 comments on commit f4073c6

Please sign in to comment.