| 1 | use std::io::{Read, Write}; |
| 2 | use std::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle}; |
| 3 | |
| 4 | use winapi::um::consoleapi::{ |
| 5 | GetConsoleMode, |
| 6 | SetConsoleMode, |
| 7 | }; |
| 8 | use winapi::um::wincon::{ |
| 9 | ENABLE_LINE_INPUT, |
| 10 | ENABLE_ECHO_INPUT, |
| 11 | }; |
| 12 | |
| 13 | use winapi::shared::minwindef::{BOOL, DWORD}; |
| 14 | |
| 15 | pub struct Terminal { |
| 16 | input: std::io::Stdin, |
| 17 | output: std::io::Stderr, |
| 18 | } |
| 19 | |
| 20 | #[derive(Copy, Clone)] |
| 21 | pub struct TerminalMode { |
| 22 | input_mode: DWORD, |
| 23 | } |
| 24 | |
| 25 | impl Terminal { |
| 26 | pub fn open() -> std::io::Result<Self> { |
| 27 | let input = std::io::stdin(); |
| 28 | let output = std::io::stderr(); |
| 29 | if !is_terminal(input.as_handle()) { |
| 30 | return Err(std::io::Error::new(std::io::ErrorKind::Other, "stdin is not a terminal")); |
| 31 | } |
| 32 | if !is_terminal(output.as_handle()) { |
| 33 | return Err(std::io::Error::new(std::io::ErrorKind::Other, "stderr is not a terminal")); |
| 34 | } |
| 35 | Ok(Self { |
| 36 | input, |
| 37 | output, |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | pub fn get_terminal_mode(&self) -> std::io::Result<TerminalMode> { |
| 42 | unsafe { |
| 43 | let mut input_mode = 0; |
| 44 | check_ret(GetConsoleMode(self.input.as_raw_handle().cast(), &mut input_mode))?; |
| 45 | Ok(TerminalMode { |
| 46 | input_mode, |
| 47 | }) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub fn set_terminal_mode(&self, mode: &TerminalMode) -> std::io::Result<()> { |
| 52 | unsafe { |
| 53 | check_ret(SetConsoleMode( |
| 54 | self.input.as_raw_handle().cast(), |
| 55 | mode.input_mode, |
| 56 | ))?; |
| 57 | Ok(()) |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | impl TerminalMode { |
| 63 | pub fn enable_line_editing(&mut self) { |
| 64 | self.input_mode |= ENABLE_LINE_INPUT; |
| 65 | } |
| 66 | |
| 67 | pub fn disable_echo(&mut self) { |
| 68 | self.input_mode &= !ENABLE_ECHO_INPUT; |
| 69 | } |
| 70 | |
| 71 | pub fn enable_echo(&mut self) { |
| 72 | self.input_mode |= ENABLE_ECHO_INPUT; |
| 73 | } |
| 74 | |
| 75 | pub fn is_echo_enabled(&self) -> bool { |
| 76 | self.input_mode & ENABLE_ECHO_INPUT != 0 |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | fn is_terminal(handle: BorrowedHandle) -> bool { |
| 81 | unsafe { |
| 82 | let mut mode = 0; |
| 83 | GetConsoleMode(handle.as_raw_handle().cast(), &mut mode) != 0 |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fn check_ret(input: BOOL) -> std::io::Result<()> { |
| 88 | if input != 0 { |
| 89 | Ok(()) |
| 90 | } else { |
| 91 | Err(std::io::Error::last_os_error()) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | impl Read for Terminal { |
| 96 | fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { |
| 97 | self.input.read(buf) |
| 98 | } |
| 99 | |
| 100 | fn read_vectored(&mut self, bufs: &mut [std::io::IoSliceMut<'_>]) -> std::io::Result<usize> { |
| 101 | self.input.read_vectored(bufs) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | impl Write for Terminal { |
| 106 | fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { |
| 107 | self.output.write(buf) |
| 108 | } |
| 109 | |
| 110 | fn flush(&mut self) -> std::io::Result<()> { |
| 111 | self.output.flush() |
| 112 | } |
| 113 | |
| 114 | fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> { |
| 115 | self.output.write_vectored(bufs) |
| 116 | } |
| 117 | } |