Struct conciliator::core::Buffer

source ·
pub struct Buffer { /* private fields */ }
Expand description

Buffer for colored text

A buffer stores text and color escape codes for printing to an output stream. Actually writing the escape codes can be disabled at runtime (i.e. when the underlying stream is not attached to a TTY).
See Paint for convenient methods to append colored text.

This has also been taken and adapted from termcolor.

Implementations§

source§

impl Buffer

source

pub fn new(should_color: bool) -> Self

Create a new buffer for colored text. If should_color is false, all attempts at setting colors or styles via the Paint trait will be silently ignored.

source

pub fn push_inline<T: Inline>(&mut self, thing: &T) -> &mut Self

Append thing onto the buffer using the Inline trait

source

pub fn push<M, T: Pushable<M>>(&mut self, thing: T) -> &mut Self

Append thing onto the buffer using either Inline or Display, doesn’t work if both are implemented for T

Pushable is blanket-implemented for both Inline and Display types. This would normally not be possible, because both blanket implementations could apply to the same type, thereby causing a conflict (Rust does not have any specialization or way to express where T: NOT Trait). So instead, this uses a trick that relies on a “marker type” being inferred.

This workaround is described in detail on Pushable, but in short:

  • Pushable has a type parameter M (i.e. trait Pushable<M>) that is not used except to make distinct implementations possible for the same type
  • impl<T: Display> Pushable<marker::AsDisplay> for T
  • impl<T: Inline> Pushable<marker::AsInline> for T
  • push is implemented for any T: Pushable<M> with any M
  • when there is only one applicable implementation, the marker type M can be inferred and won’t need to be specified
  • Surprisingly, it just works!

If, however, there are multiple applicable implementations (i.e. for a type implements Inline and Display), the type cannot be inferred: type annotations needed. In this case, it is easier to just use the more specific function instead of bothering with the type annotations. (For the time being, the marker types are private, so type annotations are also impossible.)

 use conciliator::{Conciliator, Wrap};
 let con = conciliator::init();
 con.line(..)
     .push("Test") // &str,
     .push(123)    // i32 and
     .push(' ')    // char all implement Display
     .push(Wrap::Alpha(":^)")); // Wrap implements Inline

Trait Implementations§

source§

impl EmitEscapes for Buffer

source§

fn escapes_recognized(&self) -> bool

If this returns true, escape codes will be emitted. Read more
source§

fn set_color(&mut self, color: ColorCode) -> IoResult<()>

Write a “foreground color” escape code (\x1B[3???m)
source§

fn set_bold(&mut self) -> IoResult<()>

Write a “bold text” escape code (\x1B[1m)
source§

fn reset(&mut self) -> IoResult<()>

Write a “reset style and color” escape code (\x1B[0m)
source§

fn write_with_color<T: Display + ?Sized>(&mut self, color: ColorCode, thing: &T)

set_color, then write! and then reset, provided for convenience
source§

fn write_with_color_bold<T: Display + ?Sized>( &mut self, color: ColorCode, thing: &T )

set_bold & set_color, then write! and then reset, provided for convenience
source§

impl Write for Buffer

source§

fn write(&mut self, buf: &[u8]) -> IoResult<usize>

Write a buffer into this writer, returning how many bytes were written. Read more
source§

fn flush(&mut self) -> IoResult<()>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> Paint for T
where T: EmitEscapes,

source§

fn tag(&mut self, color: Color, tag: &str) -> &mut Self

Add a tag to this buffer: [ $tag ]
source§

fn push_plain<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

Append thing onto the buffer using the Display trait (plain uncolored text)
source§

fn push_bold<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

Append thing onto the buffer using the Display trait (plain uncolored text but in bold)
source§

fn push_alpha<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

source§

fn push_beta<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

source§

fn push_gamma<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

source§

fn push_delta<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

source§

fn push_zeta<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

source§

fn push_iota<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

source§

fn push_omega<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

source§

fn push_alpha_bold<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

source§

fn push_beta_bold<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

source§

fn push_gamma_bold<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

source§

fn push_delta_bold<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

source§

fn push_zeta_bold<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

source§

fn push_iota_bold<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

source§

fn push_omega_bold<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self

source§

fn push_with_color<T: Display + ?Sized>( &mut self, color: Color, thing: &T ) -> &mut Self

Append to the buffer using Display but use the provided Color.
source§

fn push_with_color_bold<T: Display + ?Sized>( &mut self, color: Color, thing: &T ) -> &mut Self

Same as push_with_color but bold
source§

fn push_with_any_color<T: Display + ?Sized>( &mut self, color: ColorCode, thing: &T ) -> &mut Self

Append to the buffer using Display but use any ColorCode.
source§

fn push_with_any_color_bold<T: Display + ?Sized>( &mut self, color: ColorCode, thing: &T ) -> &mut Self

Same as push_with_any_color but bold
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.