1use crate::IsTerminal;
2
3/// In-memory [`RawStream`][crate::RawStream]
4#[derive(Clone, Default, Debug, PartialEq, Eq)]
5pub struct Buffer(Vec<u8>);
6
7impl Buffer {
8 #[inline]
9 pub fn new() -> Self {
10 Default::default()
11 }
12
13 #[inline]
14 pub fn with_capacity(capacity: usize) -> Self {
15 Self(Vec::with_capacity(capacity))
16 }
17
18 #[inline]
19 pub fn as_bytes(&self) -> &[u8] {
20 &self.0
21 }
22}
23
24impl AsRef<[u8]> for Buffer {
25 #[inline]
26 fn as_ref(&self) -> &[u8] {
27 self.as_bytes()
28 }
29}
30
31impl IsTerminal for Buffer {
32 #[inline]
33 fn is_terminal(&self) -> bool {
34 false
35 }
36}
37
38impl std::io::Write for Buffer {
39 #[inline]
40 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
41 self.0.extend(iter:buf);
42 Ok(buf.len())
43 }
44
45 #[inline]
46 fn flush(&mut self) -> std::io::Result<()> {
47 Ok(())
48 }
49}
50
51#[cfg(all(windows, feature = "wincon"))]
52impl anstyle_wincon::WinconStream for Buffer {
53 fn set_colors(
54 &mut self,
55 fg: Option<anstyle::AnsiColor>,
56 bg: Option<anstyle::AnsiColor>,
57 ) -> std::io::Result<()> {
58 use std::io::Write as _;
59
60 if let Some(fg) = fg {
61 write!(self, "{}", fg.render_fg())?;
62 }
63 if let Some(bg) = bg {
64 write!(self, "{}", bg.render_bg())?;
65 }
66 if fg.is_none() && bg.is_none() {
67 write!(self, "{}", anstyle::Reset.render())?;
68 }
69 Ok(())
70 }
71
72 fn get_colors(
73 &self,
74 ) -> std::io::Result<(Option<anstyle::AnsiColor>, Option<anstyle::AnsiColor>)> {
75 Ok((None, None))
76 }
77}
78