| 1 |
use crate::adapter::WinconBytes; |
| 2 |
use crate::stream::AsLockedWrite; |
| 3 |
use crate::stream::IsTerminal; |
| 4 |
|
| 5 |
/// Only pass printable data to the inner `Write` |
| 6 |
#[cfg(feature = "wincon")] // here mostly for documentation purposes |
| 7 |
#[derive(Debug)] |
| 8 |
pub struct WinconStream<S> |
| 9 |
where |
| 10 |
S: anstyle_wincon::WinconStream, |
| 11 |
{ |
| 12 |
raw: S, |
| 13 |
// `WinconBytes` is especially large compared to other variants of `AutoStream`, so boxing it |
| 14 |
// here so `AutoStream` doesn't have to discard one allocation and create another one when |
| 15 |
// calling `AutoStream::lock` |
| 16 |
state: Box<WinconBytes>, |
| 17 |
} |
| 18 |
|
| 19 |
impl<S> WinconStream<S> |
| 20 |
where |
| 21 |
S: anstyle_wincon::WinconStream, |
| 22 |
{ |
| 23 |
/// Only pass printable data to the inner `Write` |
| 24 |
#[inline] |
| 25 |
pub fn new(raw: S) -> Self { |
| 26 |
Self { |
| 27 |
raw, |
| 28 |
state: Default::default(), |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/// Get the wrapped [`anstyle_wincon::WinconStream`] |
| 33 |
#[inline] |
| 34 |
pub fn into_inner(self) -> S { |
| 35 |
self.raw |
| 36 |
} |
| 37 |
|
| 38 |
/// Get the wrapped [`std::io::Write`] |
| 39 |
#[inline] |
| 40 |
pub fn as_inner(&self) -> &S { |
| 41 |
&self.raw |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
impl<S> WinconStream<S> |
| 46 |
where |
| 47 |
S: anstyle_wincon::WinconStream, |
| 48 |
S: IsTerminal, |
| 49 |
{ |
| 50 |
/// Returns `true` if the descriptor/handle refers to a terminal/tty. |
| 51 |
#[inline] |
| 52 |
pub fn is_terminal(&self) -> bool { |
| 53 |
self.raw.is_terminal() |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
impl WinconStream<std::io::Stdout> { |
| 58 |
/// Get exclusive access to the `WinconStream` |
| 59 |
/// |
| 60 |
/// Why? |
| 61 |
/// - Faster performance when writing in a loop |
| 62 |
/// - Avoid other threads interleaving output with the current thread |
| 63 |
#[inline] |
| 64 |
pub fn lock(self) -> WinconStream<std::io::StdoutLock<'static>> { |
| 65 |
WinconStream { |
| 66 |
raw: self.raw.lock(), |
| 67 |
state: self.state, |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
impl WinconStream<std::io::Stderr> { |
| 73 |
/// Get exclusive access to the `WinconStream` |
| 74 |
/// |
| 75 |
/// Why? |
| 76 |
/// - Faster performance when writing in a loop |
| 77 |
/// - Avoid other threads interleaving output with the current thread |
| 78 |
#[inline] |
| 79 |
pub fn lock(self) -> WinconStream<std::io::StderrLock<'static>> { |
| 80 |
WinconStream { |
| 81 |
raw: self.raw.lock(), |
| 82 |
state: self.state, |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
impl<S> std::io::Write for WinconStream<S> |
| 88 |
where |
| 89 |
S: anstyle_wincon::WinconStream, |
| 90 |
S: AsLockedWrite, |
| 91 |
{ |
| 92 |
// Must forward all calls to ensure locking happens appropriately |
| 93 |
#[inline] |
| 94 |
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { |
| 95 |
write(&mut self.raw.as_locked_write(), &mut self.state, buf) |
| 96 |
} |
| 97 |
#[inline] |
| 98 |
fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> { |
| 99 |
let buf = bufs |
| 100 |
.iter() |
| 101 |
.find(|b| !b.is_empty()) |
| 102 |
.map(|b| &**b) |
| 103 |
.unwrap_or(&[][..]); |
| 104 |
self.write(buf) |
| 105 |
} |
| 106 |
// is_write_vectored: nightly only |
| 107 |
#[inline] |
| 108 |
fn flush(&mut self) -> std::io::Result<()> { |
| 109 |
self.raw.as_locked_write().flush() |
| 110 |
} |
| 111 |
#[inline] |
| 112 |
fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> { |
| 113 |
write_all(&mut self.raw.as_locked_write(), &mut self.state, buf) |
| 114 |
} |
| 115 |
// write_all_vectored: nightly only |
| 116 |
#[inline] |
| 117 |
fn write_fmt(&mut self, args: std::fmt::Arguments<'_>) -> std::io::Result<()> { |
| 118 |
write_fmt(&mut self.raw.as_locked_write(), &mut self.state, args) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
fn write( |
| 123 |
raw: &mut dyn anstyle_wincon::WinconStream, |
| 124 |
state: &mut WinconBytes, |
| 125 |
buf: &[u8], |
| 126 |
) -> std::io::Result<usize> { |
| 127 |
for (style, printable) in state.extract_next(buf) { |
| 128 |
let fg = style.get_fg_color().and_then(cap_wincon_color); |
| 129 |
let bg = style.get_bg_color().and_then(cap_wincon_color); |
| 130 |
let written = raw.write_colored(fg, bg, printable.as_bytes())?; |
| 131 |
let possible = printable.len(); |
| 132 |
if possible != written { |
| 133 |
// HACK: Unsupported atm |
| 134 |
break; |
| 135 |
} |
| 136 |
} |
| 137 |
Ok(buf.len()) |
| 138 |
} |
| 139 |
|
| 140 |
fn write_all( |
| 141 |
raw: &mut dyn anstyle_wincon::WinconStream, |
| 142 |
state: &mut WinconBytes, |
| 143 |
buf: &[u8], |
| 144 |
) -> std::io::Result<()> { |
| 145 |
for (style, printable) in state.extract_next(buf) { |
| 146 |
let mut buf = printable.as_bytes(); |
| 147 |
let fg = style.get_fg_color().and_then(cap_wincon_color); |
| 148 |
let bg = style.get_bg_color().and_then(cap_wincon_color); |
| 149 |
while !buf.is_empty() { |
| 150 |
match raw.write_colored(fg, bg, buf) { |
| 151 |
Ok(0) => { |
| 152 |
return Err(std::io::Error::new( |
| 153 |
std::io::ErrorKind::WriteZero, |
| 154 |
"failed to write whole buffer", |
| 155 |
)); |
| 156 |
} |
| 157 |
Ok(n) => buf = &buf[n..], |
| 158 |
Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => {} |
| 159 |
Err(e) => return Err(e), |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
Ok(()) |
| 164 |
} |
| 165 |
|
| 166 |
fn write_fmt( |
| 167 |
raw: &mut dyn anstyle_wincon::WinconStream, |
| 168 |
state: &mut WinconBytes, |
| 169 |
args: std::fmt::Arguments<'_>, |
| 170 |
) -> std::io::Result<()> { |
| 171 |
let write_all = |buf: &[u8]| write_all(raw, state, buf); |
| 172 |
crate::fmt::Adapter::new(write_all).write_fmt(args) |
| 173 |
} |
| 174 |
|
| 175 |
fn cap_wincon_color(color: anstyle::Color) -> Option<anstyle::AnsiColor> { |
| 176 |
match color { |
| 177 |
anstyle::Color::Ansi(c) => Some(c), |
| 178 |
anstyle::Color::Ansi256(c) => c.into_ansi(), |
| 179 |
anstyle::Color::Rgb(_) => None, |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
#[cfg(test)] |
| 184 |
mod test { |
| 185 |
use super::*; |
| 186 |
use proptest::prelude::*; |
| 187 |
use std::io::Write as _; |
| 188 |
|
| 189 |
proptest! { |
| 190 |
#[test] |
| 191 |
#[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253 |
| 192 |
fn write_all_no_escapes(s in "\\PC*") { |
| 193 |
let buffer = Vec::new(); |
| 194 |
let mut stream = WinconStream::new(buffer); |
| 195 |
stream.write_all(s.as_bytes()).unwrap(); |
| 196 |
let buffer = stream.into_inner(); |
| 197 |
let actual = std::str::from_utf8(buffer.as_ref()).unwrap(); |
| 198 |
assert_eq!(s, actual); |
| 199 |
} |
| 200 |
|
| 201 |
#[test] |
| 202 |
#[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253 |
| 203 |
fn write_byte_no_escapes(s in "\\PC*") { |
| 204 |
let buffer = Vec::new(); |
| 205 |
let mut stream = WinconStream::new(buffer); |
| 206 |
for byte in s.as_bytes() { |
| 207 |
stream.write_all(&[*byte]).unwrap(); |
| 208 |
} |
| 209 |
let buffer = stream.into_inner(); |
| 210 |
let actual = std::str::from_utf8(buffer.as_ref()).unwrap(); |
| 211 |
assert_eq!(s, actual); |
| 212 |
} |
| 213 |
|
| 214 |
#[test] |
| 215 |
#[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253 |
| 216 |
fn write_all_random(s in any::<Vec<u8>>()) { |
| 217 |
let buffer = Vec::new(); |
| 218 |
let mut stream = WinconStream::new(buffer); |
| 219 |
stream.write_all(s.as_slice()).unwrap(); |
| 220 |
} |
| 221 |
|
| 222 |
#[test] |
| 223 |
#[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253 |
| 224 |
fn write_byte_random(s in any::<Vec<u8>>()) { |
| 225 |
let buffer = Vec::new(); |
| 226 |
let mut stream = WinconStream::new(buffer); |
| 227 |
for byte in s.as_slice() { |
| 228 |
stream.write_all(&[*byte]).unwrap(); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
} |