]>
git.proxmox.com Git - rustc.git/blob - vendor/anstream/src/wincon.rs
1 use crate::adapter
::WinconBytes
;
5 /// Only pass printable data to the inner `Write`
6 #[cfg(feature = "wincon")] // here mostly for documentation purposes
8 pub struct WinconStream
<S
>
12 console
: anstyle_wincon
::Console
<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
>,
19 impl<S
> WinconStream
<S
>
23 /// Only pass printable data to the inner `Write`
25 pub fn new(console
: anstyle_wincon
::Console
<S
>) -> Self {
28 state
: Box
::default(),
32 /// Get the wrapped [`RawStream`]
34 pub fn into_inner(self) -> anstyle_wincon
::Console
<S
> {
39 #[cfg(feature = "auto")]
40 pub fn is_terminal(&self) -> bool
{
41 // HACK: We can't get the console's stream to check but if there is a console, it likely is
47 #[cfg(feature = "auto")]
48 impl<S
> is_terminal
::IsTerminal
for WinconStream
<S
>
53 fn is_terminal(&self) -> bool
{
58 impl<S
> std
::io
::Write
for WinconStream
<S
>
62 fn write(&mut self, buf
: &[u8]) -> std
::io
::Result
<usize> {
63 for (style
, printable
) in self.state
.extract_next(buf
) {
64 let fg
= style
.get_fg_color().and_then(cap_wincon_color
);
65 let bg
= style
.get_bg_color().and_then(cap_wincon_color
);
66 let written
= self.console
.write(fg
, bg
, printable
.as_bytes())?
;
67 let possible
= printable
.len();
68 if possible
!= written
{
69 // HACK: Unsupported atm
76 fn flush(&mut self) -> std
::io
::Result
<()> {
81 impl<S
> Lockable
for WinconStream
<S
>
83 S
: RawStream
+ Lockable
,
84 <S
as Lockable
>::Locked
: RawStream
,
86 type Locked
= WinconStream
<<S
as Lockable
>::Locked
>;
89 fn lock(self) -> Self::Locked
{
91 console
: self.console
.lock(),
97 fn cap_wincon_color(color
: anstyle
::Color
) -> Option
<anstyle
::AnsiColor
> {
99 anstyle
::Color
::Ansi(c
) => Some(c
),
100 anstyle
::Color
::Ansi256(c
) => c
.into_ansi(),
101 anstyle
::Color
::Rgb(_
) => None
,
108 use proptest
::prelude
::*;
109 use std
::io
::Write
as _
;
113 #[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
114 fn write_all_no_escapes(s
in "\\PC*") {
115 let buffer
= crate::Buffer
::new();
116 let mut stream
= WinconStream
::new(anstyle_wincon
::Console
::new(buffer
).unwrap());
117 stream
.write_all(s
.as_bytes()).unwrap();
118 let buffer
= stream
.into_inner().into_inner();
119 let actual
= std
::str::from_utf8(buffer
.as_ref()).unwrap();
120 assert_eq
!(s
, actual
);
124 #[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
125 fn write_byte_no_escapes(s
in "\\PC*") {
126 let buffer
= crate::Buffer
::new();
127 let mut stream
= WinconStream
::new(anstyle_wincon
::Console
::new(buffer
).unwrap());
128 for byte
in s
.as_bytes() {
129 stream
.write_all(&[*byte
]).unwrap();
131 let buffer
= stream
.into_inner().into_inner();
132 let actual
= std
::str::from_utf8(buffer
.as_ref()).unwrap();
133 assert_eq
!(s
, actual
);
137 #[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
138 fn write_all_random(s
in any
::<Vec
<u8>>()) {
139 let buffer
= crate::Buffer
::new();
140 let mut stream
= WinconStream
::new(anstyle_wincon
::Console
::new(buffer
).unwrap());
141 stream
.write_all(s
.as_slice()).unwrap();
145 #[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
146 fn write_byte_random(s
in any
::<Vec
<u8>>()) {
147 let buffer
= crate::Buffer
::new();
148 let mut stream
= WinconStream
::new(anstyle_wincon
::Console
::new(buffer
).unwrap());
149 for byte
in s
.as_slice() {
150 stream
.write_all(&[*byte
]).unwrap();