Correct for Windows

This commit is contained in:
pedrocarlo 2025-04-20 18:11:45 -03:00
parent b550fbb3e4
commit 7aaffff45f
3 changed files with 64 additions and 2 deletions

1
Cargo.lock generated
View file

@ -1713,6 +1713,7 @@ dependencies = [
"tracing",
"tracing-appender",
"tracing-subscriber",
"windows-sys 0.59.0",
]
[[package]]

View file

@ -33,7 +33,6 @@ limbo_core = { path = "../core", default-features = true, features = [
"completion",
] }
miette = { version = "7.4.0", features = ["fancy"] }
nix = "0.29.0"
nu-ansi-term = "0.50.1"
rustyline = { version = "15.0.0", default-features = true, features = [
"derive",
@ -44,6 +43,14 @@ tracing = "0.1.41"
tracing-appender = "0.2.3"
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
[target.'cfg(target_family = "unix")'.dependencies]
nix = "0.29.0"
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.59.0", features = [
"Win32_Foundation",
"Win32_System_Console",
] }
[features]
default = ["io_uring"]

View file

@ -5,6 +5,7 @@ mod helper;
mod input;
mod opcodes_dictionary;
#[cfg(unix)]
use nix::unistd::isatty;
use rustyline::{error::ReadlineError, Config, Editor};
use std::{
@ -74,5 +75,58 @@ fn main() -> anyhow::Result<()> {
/// Return whether or not STDIN is a TTY
fn is_a_tty() -> bool {
isatty(libc::STDIN_FILENO).unwrap_or(false)
#[cfg(unix)]
{
isatty(libc::STDIN_FILENO).unwrap_or(false)
}
#[cfg(windows)]
{
let handle = windows::get_std_handle(windows::console::STD_INPUT_HANDLE);
match handle {
Ok(handle) => {
// If this function doesn't fail then fd is a TTY
windows::get_console_mode(handle).is_ok()
}
Err(_) => false,
}
}
}
// Code acquired from Rustyline
#[cfg(windows)]
mod windows {
use std::io;
use windows_sys::Win32::Foundation::{self as foundation, BOOL, FALSE, HANDLE};
pub use windows_sys::Win32::System::Console as console;
pub fn get_console_mode(handle: HANDLE) -> rustyline::Result<console::CONSOLE_MODE> {
let mut original_mode = 0;
check(unsafe { console::GetConsoleMode(handle, &mut original_mode) })?;
Ok(original_mode)
}
pub fn get_std_handle(fd: console::STD_HANDLE) -> rustyline::Result<HANDLE> {
let handle = unsafe { console::GetStdHandle(fd) };
check_handle(handle)
}
fn check_handle(handle: HANDLE) -> rustyline::Result<HANDLE> {
if handle == foundation::INVALID_HANDLE_VALUE {
Err(io::Error::last_os_error())?;
} else if handle.is_null() {
Err(io::Error::new(
io::ErrorKind::Other,
"no stdio handle available for this process",
))?;
}
Ok(handle)
}
fn check(rc: BOOL) -> io::Result<()> {
if rc == FALSE {
Err(io::Error::last_os_error())
} else {
Ok(())
}
}
}