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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use crate::adapter::WinconBytes;
use crate::Lockable;
use crate::RawStream;
#[cfg(feature = "wincon")] pub struct WinconStream<S>
where
S: RawStream,
{
console: anstyle_wincon::Console<S>,
state: Box<WinconBytes>,
}
impl<S> WinconStream<S>
where
S: RawStream,
{
#[inline]
pub fn new(console: anstyle_wincon::Console<S>) -> Self {
Self {
console,
state: Box::default(),
}
}
#[inline]
pub fn into_inner(self) -> anstyle_wincon::Console<S> {
self.console
}
}
impl<S> std::io::Write for WinconStream<S>
where
S: RawStream,
{
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
for (style, printable) in self.state.extract_next(buf) {
let fg = style.get_fg_color().and_then(cap_wincon_color);
let bg = style.get_bg_color().and_then(cap_wincon_color);
let written = self.console.write(fg, bg, printable.as_bytes())?;
let possible = printable.len();
if possible != written {
break;
}
}
Ok(buf.len())
}
#[inline]
fn flush(&mut self) -> std::io::Result<()> {
self.console.flush()
}
}
impl<S> Lockable for WinconStream<S>
where
S: RawStream + Lockable,
<S as Lockable>::Locked: RawStream,
{
type Locked = WinconStream<<S as Lockable>::Locked>;
#[inline]
fn lock(self) -> Self::Locked {
Self::Locked {
console: self.console.lock(),
state: self.state,
}
}
}
fn cap_wincon_color(color: anstyle::Color) -> Option<anstyle::AnsiColor> {
match color {
anstyle::Color::Ansi(c) => Some(c),
anstyle::Color::Ansi256(c) => c.into_ansi(),
anstyle::Color::Rgb(_) => None,
}
}
#[cfg(test)]
mod test {
use super::*;
use proptest::prelude::*;
use std::io::Write as _;
proptest! {
#[test]
#[cfg_attr(miri, ignore)] fn write_all_no_escapes(s in "\\PC*") {
let buffer = crate::Buffer::new();
let mut stream = WinconStream::new(anstyle_wincon::Console::new(buffer));
stream.write_all(s.as_bytes()).unwrap();
let buffer = stream.into_inner().into_inner();
let actual = std::str::from_utf8(buffer.as_ref()).unwrap();
assert_eq!(s, actual);
}
#[test]
#[cfg_attr(miri, ignore)] fn write_byte_no_escapes(s in "\\PC*") {
let buffer = crate::Buffer::new();
let mut stream = WinconStream::new(anstyle_wincon::Console::new(buffer));
for byte in s.as_bytes() {
stream.write_all(&[*byte]).unwrap();
}
let buffer = stream.into_inner().into_inner();
let actual = std::str::from_utf8(buffer.as_ref()).unwrap();
assert_eq!(s, actual);
}
#[test]
#[cfg_attr(miri, ignore)] fn write_all_random(s in any::<Vec<u8>>()) {
let buffer = crate::Buffer::new();
let mut stream = WinconStream::new(anstyle_wincon::Console::new(buffer));
stream.write_all(s.as_slice()).unwrap();
}
#[test]
#[cfg_attr(miri, ignore)] fn write_byte_random(s in any::<Vec<u8>>()) {
let buffer = crate::Buffer::new();
let mut stream = WinconStream::new(anstyle_wincon::Console::new(buffer));
for byte in s.as_slice() {
stream.write_all(&[*byte]).unwrap();
}
}
}
}