1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#[cfg(target_family = "wasm")]
mod wasm {
    use std::io::Write;

    /// Displays a message on the STDOUT
    pub fn print_tty(prompt: impl ToString) -> std::io::Result<()> {
        let mut stdout = std::io::stdout();
        write!(stdout, "{}", prompt.to_string().as_str())?;
        stdout.flush()?;
        Ok(())
    }
}

#[cfg(target_family = "unix")]
mod unix {
    use std::io::Write;

    /// Displays a message on the TTY
    pub fn print_tty(prompt: impl ToString) -> std::io::Result<()> {
        let mut stream = std::fs::OpenOptions::new().write(true).open("/dev/tty")?;
        stream
            .write_all(prompt.to_string().as_str().as_bytes())
            .and_then(|_| stream.flush())
    }
}

#[cfg(target_family = "windows")]
mod windows {
    use std::io::Write;
    use std::os::windows::io::FromRawHandle;
    use winapi::um::fileapi::{CreateFileA, OPEN_EXISTING};
    use winapi::um::handleapi::INVALID_HANDLE_VALUE;
    use winapi::um::winnt::{FILE_SHARE_READ, FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE};

    /// Displays a message on the TTY
    pub fn print_tty(prompt: impl ToString) -> std::io::Result<()> {
        let handle = unsafe {
            CreateFileA(
                b"CONOUT$\x00".as_ptr() as *const i8,
                GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                std::ptr::null_mut(),
                OPEN_EXISTING,
                0,
                std::ptr::null_mut(),
            )
        };
        if handle == INVALID_HANDLE_VALUE {
            return Err(std::io::Error::last_os_error());
        }

        let mut stream = unsafe { std::fs::File::from_raw_handle(handle) };

        stream
            .write_all(prompt.to_string().as_str().as_bytes())
            .and_then(|_| stream.flush())
    }
}

/// Prints a message to a writer
pub fn print_writer(stream: &mut impl Write, prompt: impl ToString) -> std::io::Result<()> {
    stream
        .write_all(prompt.to_string().as_str().as_bytes())
        .and_then(|_| stream.flush())
}

use std::io::Write;
#[cfg(target_family = "unix")]
pub use unix::print_tty;
#[cfg(target_family = "wasm")]
pub use wasm::print_tty;
#[cfg(target_family = "windows")]
pub use windows::print_tty;