Struct AutoStream

pub struct AutoStream<S: RawStream> { /* private fields */ }

std::io::Write that adapts ANSI escape codes to the underlying Writes capabilities

This includes

You can customize auto-detection by calling into anstyle_query to get a ColorChoice and then calling [AutoStream::new(stream, choice)].

Implementations

impl AutoStream<Stderr>

fn lock(self) -> AutoStream<StderrLock<'static>>

Get exclusive access to the AutoStream

Why?

  • Faster performance when writing in a loop
  • Avoid other threads interleaving output with the current thread

impl AutoStream<Stdout>

fn lock(self) -> AutoStream<StdoutLock<'static>>

Get exclusive access to the AutoStream

Why?

  • Faster performance when writing in a loop
  • Avoid other threads interleaving output with the current thread

impl<S> AutoStream<S> where S: RawStream,

fn new(raw: S, choice: ColorChoice) -> Self

Runtime control over styling behavior

Example

# #[cfg(feature = "auto")] {
# use std::io::IsTerminal as _;
// Like `AutoStream::choice` but without `NO_COLOR`, `CLICOLOR_FORCE`, `CI`
fn choice(raw: &dyn anstream::stream::RawStream) -> anstream::ColorChoice {
    let choice = anstream::ColorChoice::global();
    if choice == anstream::ColorChoice::Auto {
        if raw.is_terminal() && anstyle_query::term_supports_color() {
            anstream::ColorChoice::Always
        } else {
            anstream::ColorChoice::Never
        }
    } else {
        choice
    }
}

let stream = std::io::stdout();
let choice = choice(&stream);
let auto = anstream::AutoStream::new(stream, choice);
# }
fn auto(raw: S) -> Self

Auto-adapt for the stream's capabilities

fn choice(raw: &S) -> ColorChoice

Report the desired choice for the given stream

fn always_ansi(raw: S) -> Self

Force ANSI escape codes to be passed through as-is, no matter what the inner Write supports.

fn always(raw: S) -> Self

Force color, no matter what the inner Write supports.

fn never(raw: S) -> Self

Only pass printable data to the inner Write.

fn into_inner(self) -> S

Get the wrapped RawStream

fn as_inner(&self) -> &S

Get the wrapped RawStream

fn is_terminal(&self) -> bool

Returns true if the descriptor/handle refers to a terminal/tty.

fn current_choice(&self) -> ColorChoice

Prefer AutoStream::choice

This doesn't report what is requested but what is currently active.

Trait Implementations

impl<S> Write for AutoStream<S> where S: RawStream + AsLockedWrite,

fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize>
fn flush(&mut self) -> Result<()>
fn write_all(&mut self, buf: &[u8]) -> Result<()>
fn write_fmt(&mut self, args: Arguments<'_>) -> Result<()>

impl<S: Debug + RawStream> Debug for AutoStream<S>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Auto Trait Implementations

impl<S> Freeze for AutoStream<S> where StreamInner<S>: Freeze,

impl<S> RefUnwindSafe for AutoStream<S> where StreamInner<S>: RefUnwindSafe,

impl<S> Send for AutoStream<S> where StreamInner<S>: Send,

impl<S> Sync for AutoStream<S> where StreamInner<S>: Sync,

impl<S> Unpin for AutoStream<S> where StreamInner<S>: Unpin,

impl<S> UnsafeUnpin for AutoStream<S> where StreamInner<S>: UnsafeUnpin,

impl<S> UnwindSafe for AutoStream<S> where StreamInner<S>: UnwindSafe,

Blanket Implementations

impl<T> Any for AutoStream<S> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for AutoStream<S> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for AutoStream<S> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for AutoStream<S>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for AutoStream<S> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for AutoStream<S> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for AutoStream<S> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>