|
| 1 | +use super::{AsyncInputStream, AsyncOutputStream, AsyncRead, AsyncWrite, Result}; |
| 2 | +use std::cell::LazyCell; |
| 3 | +use wasip2::cli::terminal_input::TerminalInput; |
| 4 | +use wasip2::cli::terminal_output::TerminalOutput; |
| 5 | + |
| 6 | +/// Use the program's stdin as an `AsyncInputStream`. |
| 7 | +#[derive(Debug)] |
| 8 | +pub struct Stdin { |
| 9 | + stream: AsyncInputStream, |
| 10 | + terminput: LazyCell<Option<TerminalInput>>, |
| 11 | +} |
| 12 | + |
| 13 | +/// Get the program's stdin for use as an `AsyncInputStream`. |
| 14 | +pub fn stdin() -> Stdin { |
| 15 | + let stream = AsyncInputStream::new(wasip2::cli::stdin::get_stdin()); |
| 16 | + Stdin { |
| 17 | + stream, |
| 18 | + terminput: LazyCell::new(wasip2::cli::terminal_stdin::get_terminal_stdin), |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl Stdin { |
| 23 | + /// Check if stdin is a terminal. |
| 24 | + pub fn is_terminal(&self) -> bool { |
| 25 | + LazyCell::force(&self.terminput).is_some() |
| 26 | + } |
| 27 | + |
| 28 | + /// Get the `AsyncInputStream` used to implement `Stdin` |
| 29 | + pub fn into_inner(self) -> AsyncInputStream { |
| 30 | + self.stream |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl AsyncRead for Stdin { |
| 35 | + #[inline] |
| 36 | + async fn read(&mut self, buf: &mut [u8]) -> Result<usize> { |
| 37 | + self.stream.read(buf).await |
| 38 | + } |
| 39 | + |
| 40 | + #[inline] |
| 41 | + async fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> { |
| 42 | + self.stream.read_to_end(buf).await |
| 43 | + } |
| 44 | + |
| 45 | + #[inline] |
| 46 | + fn as_async_input_stream(&self) -> Option<&AsyncInputStream> { |
| 47 | + Some(&self.stream) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/// Use the program's stdout as an `AsyncOutputStream`. |
| 52 | +#[derive(Debug)] |
| 53 | +pub struct Stdout { |
| 54 | + stream: AsyncOutputStream, |
| 55 | + termoutput: LazyCell<Option<TerminalOutput>>, |
| 56 | +} |
| 57 | + |
| 58 | +/// Get the program's stdout for use as an `AsyncOutputStream`. |
| 59 | +pub fn stdout() -> Stdout { |
| 60 | + let stream = AsyncOutputStream::new(wasip2::cli::stdout::get_stdout()); |
| 61 | + Stdout { |
| 62 | + stream, |
| 63 | + termoutput: LazyCell::new(wasip2::cli::terminal_stdout::get_terminal_stdout), |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl Stdout { |
| 68 | + /// Check if stdout is a terminal. |
| 69 | + pub fn is_terminal(&self) -> bool { |
| 70 | + LazyCell::force(&self.termoutput).is_some() |
| 71 | + } |
| 72 | + |
| 73 | + /// Get the `AsyncOutputStream` used to implement `Stdout` |
| 74 | + pub fn into_inner(self) -> AsyncOutputStream { |
| 75 | + self.stream |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl AsyncWrite for Stdout { |
| 80 | + #[inline] |
| 81 | + async fn write(&mut self, buf: &[u8]) -> Result<usize> { |
| 82 | + self.stream.write(buf).await |
| 83 | + } |
| 84 | + |
| 85 | + #[inline] |
| 86 | + async fn flush(&mut self) -> Result<()> { |
| 87 | + self.stream.flush().await |
| 88 | + } |
| 89 | + |
| 90 | + #[inline] |
| 91 | + async fn write_all(&mut self, buf: &[u8]) -> Result<()> { |
| 92 | + self.stream.write_all(buf).await |
| 93 | + } |
| 94 | + |
| 95 | + #[inline] |
| 96 | + fn as_async_output_stream(&self) -> Option<&AsyncOutputStream> { |
| 97 | + self.stream.as_async_output_stream() |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/// Use the program's stdout as an `AsyncOutputStream`. |
| 102 | +#[derive(Debug)] |
| 103 | +pub struct Stderr { |
| 104 | + stream: AsyncOutputStream, |
| 105 | + termoutput: LazyCell<Option<TerminalOutput>>, |
| 106 | +} |
| 107 | + |
| 108 | +/// Get the program's stdout for use as an `AsyncOutputStream`. |
| 109 | +pub fn stderr() -> Stderr { |
| 110 | + let stream = AsyncOutputStream::new(wasip2::cli::stderr::get_stderr()); |
| 111 | + Stderr { |
| 112 | + stream, |
| 113 | + termoutput: LazyCell::new(wasip2::cli::terminal_stderr::get_terminal_stderr), |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl Stderr { |
| 118 | + /// Check if stderr is a terminal. |
| 119 | + pub fn is_terminal(&self) -> bool { |
| 120 | + LazyCell::force(&self.termoutput).is_some() |
| 121 | + } |
| 122 | + |
| 123 | + /// Get the `AsyncOutputStream` used to implement `Stderr` |
| 124 | + pub fn into_inner(self) -> AsyncOutputStream { |
| 125 | + self.stream |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +impl AsyncWrite for Stderr { |
| 130 | + #[inline] |
| 131 | + async fn write(&mut self, buf: &[u8]) -> Result<usize> { |
| 132 | + self.stream.write(buf).await |
| 133 | + } |
| 134 | + |
| 135 | + #[inline] |
| 136 | + async fn flush(&mut self) -> Result<()> { |
| 137 | + self.stream.flush().await |
| 138 | + } |
| 139 | + |
| 140 | + #[inline] |
| 141 | + async fn write_all(&mut self, buf: &[u8]) -> Result<()> { |
| 142 | + self.stream.write_all(buf).await |
| 143 | + } |
| 144 | + |
| 145 | + #[inline] |
| 146 | + fn as_async_output_stream(&self) -> Option<&AsyncOutputStream> { |
| 147 | + self.stream.as_async_output_stream() |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +#[cfg(test)] |
| 152 | +mod test { |
| 153 | + use crate::io::AsyncWrite; |
| 154 | + use crate::runtime::block_on; |
| 155 | + #[test] |
| 156 | + // No internal predicate. Run test with --nocapture and inspect output manually. |
| 157 | + fn stdout_println_hello_world() { |
| 158 | + block_on(async { |
| 159 | + let mut stdout = super::stdout(); |
| 160 | + let term = if stdout.is_terminal() { "is" } else { "is not" }; |
| 161 | + stdout |
| 162 | + .write_all(format!("hello, world! stdout {term} a terminal\n",).as_bytes()) |
| 163 | + .await |
| 164 | + .unwrap(); |
| 165 | + }) |
| 166 | + } |
| 167 | + #[test] |
| 168 | + // No internal predicate. Run test with --nocapture and inspect output manually. |
| 169 | + fn stderr_println_hello_world() { |
| 170 | + block_on(async { |
| 171 | + let mut stdout = super::stdout(); |
| 172 | + let term = if stdout.is_terminal() { "is" } else { "is not" }; |
| 173 | + stdout |
| 174 | + .write_all(format!("hello, world! stderr {term} a terminal\n",).as_bytes()) |
| 175 | + .await |
| 176 | + .unwrap(); |
| 177 | + }) |
| 178 | + } |
| 179 | +} |
0 commit comments