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

The scope of the unsafe block can be appropriately reduced #126

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
58 changes: 29 additions & 29 deletions src/windows_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,21 @@ pub fn is_a_color_terminal(out: &Term) -> bool {
}

fn enable_ansi_on(out: &Term) -> bool {
unsafe {

let handle = as_handle(out);

let mut dw_mode = 0;
if GetConsoleMode(handle, &mut dw_mode) == 0 {
if unsafe { GetConsoleMode(handle, &mut dw_mode) == 0 }{
return false;
}

dw_mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if SetConsoleMode(handle, dw_mode) == 0 {
if unsafe { SetConsoleMode(handle, dw_mode) == 0 }{
return false;
}

true
}

}

unsafe fn console_on_any(fds: &[DWORD]) -> bool {
Expand Down Expand Up @@ -203,13 +203,13 @@ pub fn clear_line(out: &Term) -> io::Result<()> {
return common_term::clear_line(out);
}
if let Some((hand, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
let width = csbi.srWindow.Right - csbi.srWindow.Left;
let pos = COORD {
X: 0,
Y: csbi.dwCursorPosition.Y,
};
let mut written = 0;
unsafe {
let width = csbi.srWindow.Right - csbi.srWindow.Left;
let pos = COORD {
X: 0,
Y: csbi.dwCursorPosition.Y,
};
let mut written = 0;
FillConsoleOutputCharacterA(hand, b' ' as CHAR, width as DWORD, pos, &mut written);
FillConsoleOutputAttribute(hand, csbi.wAttributes, width as DWORD, pos, &mut written);
SetConsoleCursorPosition(hand, pos);
Expand All @@ -223,13 +223,13 @@ pub fn clear_chars(out: &Term, n: usize) -> io::Result<()> {
return common_term::clear_chars(out, n);
}
if let Some((hand, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
let width = cmp::min(csbi.dwCursorPosition.X, n as i16);
let pos = COORD {
X: csbi.dwCursorPosition.X - width,
Y: csbi.dwCursorPosition.Y,
};
let mut written = 0;
unsafe {
let width = cmp::min(csbi.dwCursorPosition.X, n as i16);
let pos = COORD {
X: csbi.dwCursorPosition.X - width,
Y: csbi.dwCursorPosition.Y,
};
let mut written = 0;
FillConsoleOutputCharacterA(hand, b' ' as CHAR, width as DWORD, pos, &mut written);
FillConsoleOutputAttribute(hand, csbi.wAttributes, width as DWORD, pos, &mut written);
SetConsoleCursorPosition(hand, pos);
Expand All @@ -243,10 +243,10 @@ pub fn clear_screen(out: &Term) -> io::Result<()> {
return common_term::clear_screen(out);
}
if let Some((hand, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
let cells = csbi.dwSize.X as DWORD * csbi.dwSize.Y as DWORD; // as DWORD, or else this causes stack overflows.
let pos = COORD { X: 0, Y: 0 };
let mut written = 0;
unsafe {
let cells = csbi.dwSize.X as DWORD * csbi.dwSize.Y as DWORD; // as DWORD, or else this causes stack overflows.
let pos = COORD { X: 0, Y: 0 };
let mut written = 0;
FillConsoleOutputCharacterA(hand, b' ' as CHAR, cells, pos, &mut written); // cells as DWORD no longer needed.
FillConsoleOutputAttribute(hand, csbi.wAttributes, cells, pos, &mut written);
SetConsoleCursorPosition(hand, pos);
Expand All @@ -260,15 +260,15 @@ pub fn clear_to_end_of_screen(out: &Term) -> io::Result<()> {
return common_term::clear_to_end_of_screen(out);
}
if let Some((hand, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
let bottom = csbi.srWindow.Right as DWORD * csbi.srWindow.Bottom as DWORD;
let cells =
bottom - (csbi.dwCursorPosition.X as DWORD * csbi.dwCursorPosition.Y as DWORD); // as DWORD, or else this causes stack overflows.
let pos = COORD {
X: 0,
Y: csbi.dwCursorPosition.Y,
};
let mut written = 0;
unsafe {
let bottom = csbi.srWindow.Right as DWORD * csbi.srWindow.Bottom as DWORD;
let cells =
bottom - (csbi.dwCursorPosition.X as DWORD * csbi.dwCursorPosition.Y as DWORD); // as DWORD, or else this causes stack overflows.
let pos = COORD {
X: 0,
Y: csbi.dwCursorPosition.Y,
};
let mut written = 0;
FillConsoleOutputCharacterA(hand, b' ' as CHAR, cells, pos, &mut written); // cells as DWORD no longer needed.
FillConsoleOutputAttribute(hand, csbi.wAttributes, cells, pos, &mut written);
SetConsoleCursorPosition(hand, pos);
Expand All @@ -282,8 +282,8 @@ pub fn show_cursor(out: &Term) -> io::Result<()> {
return common_term::show_cursor(out);
}
if let Some((hand, mut cci)) = get_console_cursor_info(as_handle(out)) {
cci.bVisible = 1;
unsafe {
cci.bVisible = 1;
SetConsoleCursorInfo(hand, &cci);
}
}
Expand All @@ -295,8 +295,8 @@ pub fn hide_cursor(out: &Term) -> io::Result<()> {
return common_term::hide_cursor(out);
}
if let Some((hand, mut cci)) = get_console_cursor_info(as_handle(out)) {
cci.bVisible = 0;
unsafe {
cci.bVisible = 0;
SetConsoleCursorInfo(hand, &cci);
}
}
Expand Down