Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lif committed Feb 14, 2024
1 parent a79a37c commit ecf4d09
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ impl<O: AsyncWriteExt + Unpin + Send + AsRawFdHandle> Console<O> {
)?);
Ok(Self::new_inner(stdin, stdout, escape, raw_guard))
}

/// Write the given bytes to stdout.
#[cfg(target_family = "windows")]
pub async fn write_stdout(&mut self, bytes: &[u8]) -> Result<(), Error> {
use winapi::shared::minwindef::LPDWORD;
use winapi::um::winnt::{HANDLE, VOID};
let mut _lp_num_of_chars_written = 0u32;
winapi::um::consoleapi::WriteConsoleA(
self.stdout.as_raw_handle() as HANDLE,
bytes.as_ptr() as *const VOID,
bytes.len() as u32,
(&mut _lp_num_of_chars_written) as LPDWORD,
std::ptr::null() as *mut VOID,
);
Ok(())
}
}

impl Console<tokio::io::Stdout> {
Expand Down Expand Up @@ -133,25 +149,10 @@ impl<O: AsyncWriteExt + Unpin + Send> Console<O> {
}

/// Write the given bytes to stdout.
#[cfg(not(target_family = "windows"))]
pub async fn write_stdout(&mut self, bytes: &[u8]) -> Result<(), Error> {
#[cfg(target_family = "windows")]
{
use winapi::shared::minwindef::LPDWORD;
use winapi::um::winnt::{HANDLE, VOID};
let mut _lp_num_of_chars_written = 0u32;
winapi::um::consoleapi::WriteConsoleA(
self.stdout.as_raw_handle() as HANDLE,
bytes.as_ptr() as *const VOID,
bytes.len(),
(&mut _lp_num_of_chars_written).as_ptr() as LPDWORD,
std::ptr::null() as *const VOID,
);
}
#[cfg(not(target_family = "windows"))]
{
self.stdout.write_all(bytes).await?;
self.stdout.flush().await?;
}
self.stdout.write_all(bytes).await?;
self.stdout.flush().await?;
Ok(())
}

Expand Down

0 comments on commit ecf4d09

Please sign in to comment.