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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//! The `TerminalWriter` struct.

use crate::config::{detect_write_config, WriteConfig};
use crate::{Terminal, TerminalColorSupport, WriteTerminal};
use io_extras::grip::AsGrip;
#[cfg(windows)]
use io_extras::os::windows::{
    AsHandleOrSocket, AsRawHandleOrSocket, BorrowedHandleOrSocket, RawHandleOrSocket,
};
use std::fmt;
use std::io::{self, IoSlice, Write};
#[cfg(not(windows))]
use {
    io_extras::os::rustix::{AsRawFd, RawFd},
    io_lifetimes::{AsFd, BorrowedFd},
};

/// A wrapper around a `Write` which adds minimal terminal support.
#[derive(Debug)]
pub struct TerminalWriter<Inner: Write> {
    inner: Inner,
    write_config: Option<WriteConfig>,
}

impl<Inner: Write + AsGrip> TerminalWriter<Inner> {
    /// Wrap a `TerminalWriter` around the given stream, autodetecting
    /// terminal properties using its `AsGrip` implementation.
    pub fn with_handle(inner: Inner) -> Self {
        let write_config = detect_write_config(&inner);
        Self {
            inner,
            write_config,
        }
    }

    /// Wrap a `TerminalWriter` around the given stream, using the given
    /// terminal properties.
    pub fn from(
        inner: Inner,
        is_terminal: bool,
        color_support: TerminalColorSupport,
        color_preference: bool,
    ) -> Self {
        Self {
            inner,
            write_config: if is_terminal {
                Some(WriteConfig {
                    color_support,
                    color_preference,
                })
            } else {
                None
            },
        }
    }
}

impl<Inner: Write> TerminalWriter<Inner> {
    /// Wrap a `TerminalWriter` around the given stream, using
    /// conservative terminal properties.
    pub fn generic(inner: Inner) -> Self {
        Self {
            inner,
            write_config: None,
        }
    }

    /// Consume `self` and return the inner stream.
    #[inline]
    pub fn into_inner(self) -> Inner {
        self.inner
    }
}

#[cfg(not(windows))]
impl<Inner: Write + AsRawFd> AsRawFd for TerminalWriter<Inner> {
    #[inline]
    fn as_raw_fd(&self) -> RawFd {
        self.inner.as_raw_fd()
    }
}

#[cfg(not(windows))]
impl<Inner: Write + AsFd> AsFd for TerminalWriter<Inner> {
    #[inline]
    fn as_fd(&self) -> BorrowedFd {
        self.inner.as_fd()
    }
}

#[cfg(windows)]
impl<Inner: Write + AsRawHandleOrSocket> AsRawHandleOrSocket for TerminalWriter<Inner> {
    #[inline]
    fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
        self.inner.as_raw_handle_or_socket()
    }
}

#[cfg(windows)]
impl<Inner: Write + AsHandleOrSocket> AsHandleOrSocket for TerminalWriter<Inner> {
    #[inline]
    fn as_handle_or_socket(&self) -> BorrowedHandleOrSocket<'_> {
        self.inner.as_handle_or_socket()
    }
}

impl<Inner: Write> Terminal for TerminalWriter<Inner> {}

impl<Inner: Write> WriteTerminal for TerminalWriter<Inner> {
    fn color_support(&self) -> TerminalColorSupport {
        self.write_config
            .as_ref()
            .map_or_else(Default::default, |c| c.color_support)
    }

    fn color_preference(&self) -> bool {
        self.write_config
            .as_ref()
            .map_or(false, |c| c.color_preference)
    }

    fn is_output_terminal(&self) -> bool {
        self.write_config.is_some()
    }
}

impl<Inner: Write> Write for TerminalWriter<Inner> {
    #[inline]
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.inner.write(buf)
    }

    #[inline]
    fn flush(&mut self) -> io::Result<()> {
        self.inner.flush()
    }

    #[inline]
    fn write_vectored(&mut self, bufs: &[IoSlice]) -> io::Result<usize> {
        self.inner.write_vectored(bufs)
    }

    #[cfg(can_vector)]
    #[inline]
    fn is_write_vectored(&self) -> bool {
        self.inner.is_write_vectored()
    }

    #[inline]
    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
        self.inner.write_all(buf)
    }

    #[cfg(write_all_vectored)]
    #[inline]
    fn write_all_vectored(&mut self, bufs: &mut [IoSlice]) -> io::Result<()> {
        self.inner.write_all_vectored(bufs)
    }

    #[inline]
    fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
        self.inner.write_fmt(fmt)
    }
}