Skip to content
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

Switch from the winapi crate to windows-sys #115

Merged
merged 1 commit into from
Jul 21, 2024
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
109 changes: 80 additions & 29 deletions Cargo.lock

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

12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ appveyor = { repository = "Stebalien/term" }
home = "0.5.5"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["consoleapi", "wincon", "handleapi", "fileapi"] }
rustversion = "1"


[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.52.0"
features = [
"Win32_Storage",
"Win32_Foundation",
"Win32_System_Console",
"Win32_Storage_FileSystem",
"Win32_Security",
]

[features]
default=[]
33 changes: 17 additions & 16 deletions src/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@ use crate::Attr;
use crate::Error;
use crate::Result;
use crate::Terminal;

use std::io;
use std::io::prelude::*;
use std::ops::Deref;
use std::ptr;

use winapi::shared::minwindef::{DWORD, WORD};
use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode};
use winapi::um::fileapi::{CreateFileA, OPEN_EXISTING};
use winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE};
use winapi::um::wincon::FillConsoleOutputAttribute;
use winapi::um::wincon::{
FillConsoleOutputCharacterW, GetConsoleScreenBufferInfo, CONSOLE_SCREEN_BUFFER_INFO, COORD,
use windows_sys::core::PCSTR;
use windows_sys::Win32::Foundation::{
CloseHandle, GENERIC_READ, GENERIC_WRITE, HANDLE, INVALID_HANDLE_VALUE,
};
use windows_sys::Win32::Storage::FileSystem::{CreateFileA, FILE_SHARE_WRITE, OPEN_EXISTING};
use windows_sys::Win32::System::Console::{
FillConsoleOutputAttribute, FillConsoleOutputCharacterW, GetConsoleMode,
GetConsoleScreenBufferInfo, SetConsoleCursorPosition, SetConsoleMode, SetConsoleTextAttribute,
BACKGROUND_INTENSITY, CONSOLE_CHARACTER_ATTRIBUTES, CONSOLE_MODE, CONSOLE_SCREEN_BUFFER_INFO,
COORD, ENABLE_VIRTUAL_TERMINAL_PROCESSING,
};
use winapi::um::wincon::{SetConsoleCursorPosition, SetConsoleTextAttribute};
use winapi::um::wincon::{BACKGROUND_INTENSITY, ENABLE_VIRTUAL_TERMINAL_PROCESSING};
use winapi::um::winnt::{FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE, HANDLE};

/// Console info which can be used by a Terminal implementation
/// which uses the Win32 Console API.
Expand Down Expand Up @@ -122,13 +123,13 @@ fn conout() -> io::Result<HandleWrapper> {
let name = b"CONOUT$\0";
let handle = unsafe {
CreateFileA(
name.as_ptr() as *const i8,
name.as_ptr() as PCSTR,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE,
ptr::null_mut(),
OPEN_EXISTING,
0,
ptr::null_mut(),
0,
)
};
if handle == INVALID_HANDLE_VALUE {
Expand All @@ -138,8 +139,8 @@ fn conout() -> io::Result<HandleWrapper> {
}
}

unsafe fn set_flag(handle: HANDLE, flag: DWORD) -> io::Result<()> {
let mut curr_mode: DWORD = 0;
unsafe fn set_flag(handle: HANDLE, flag: CONSOLE_MODE) -> io::Result<()> {
let mut curr_mode: CONSOLE_MODE = 0;
if GetConsoleMode(handle, &mut curr_mode) == 0 {
return Err(io::Error::last_os_error());
}
Expand Down Expand Up @@ -231,7 +232,7 @@ impl<T: Write + Send> WinConsole<T> {
fg = bg;
}

let mut accum: WORD = 0;
let mut accum: CONSOLE_CHARACTER_ATTRIBUTES = 0;

accum |= color_to_bits(fg);
accum |= color_to_bits(bg) << 4;
Expand Down Expand Up @@ -382,7 +383,7 @@ impl<T: Write + Send> Terminal for WinConsole<T> {
let buffer_info = get_console_screen_buffer_info(*handle)?;
let pos = buffer_info.dwCursorPosition;
let size = buffer_info.dwSize;
let num = (size.X - pos.X) as DWORD;
let num = (size.X - pos.X) as u32;
let mut written = 0;
// 0x0020u16 is ' ' (space) in UTF-16 (same as ascii)
if FillConsoleOutputCharacterW(*handle, 0x0020, num, pos, &mut written) == 0 {
Expand Down