moving output writing to one spot
parent
1eb955b474
commit
cba1004db8
@ -0,0 +1,52 @@
|
||||
use anyhow::{Context, Result};
|
||||
use crate::error::Error;
|
||||
use windows::Win32::{Foundation::{CloseHandle, HANDLE}, System::Console};
|
||||
// use std::io;
|
||||
|
||||
|
||||
pub fn stdout_handle() -> Result<HANDLE> {
|
||||
unsafe {
|
||||
let handle = Console::GetStdHandle(Console::STD_OUTPUT_HANDLE)
|
||||
.context("unable to get stdin handle")?;
|
||||
Ok(handle)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Writer {
|
||||
output: HANDLE,
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
pub fn new() -> Result<Self> {
|
||||
Ok(Self{
|
||||
output: stdout_handle()?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn write(&mut self, s: &str) -> Result<()> {
|
||||
unsafe {
|
||||
Error::check(Console::WriteConsoleA(self.output, s.as_bytes(), None, None))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn close(&mut self) -> Result<()> {
|
||||
unsafe {
|
||||
CloseHandle(self.output);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// impl std::io::Write for Writer {
|
||||
// fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
// unsafe {
|
||||
// Error::check(Console::WriteConsoleA(self.output, buf, None, None))?;
|
||||
// }
|
||||
// Ok(0)
|
||||
// }
|
||||
//
|
||||
// fn flush(&mut self) -> io::Result<()> {
|
||||
// Ok(())
|
||||
// }
|
||||
// }
|
Loading…
Reference in New Issue