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
impl Buffer
sourcepub fn new(should_color: bool) -> Self
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.
sourcepub fn push_inline<T: Inline>(&mut self, thing: &T) -> &mut Self
pub fn push_inline<T: Inline>(&mut self, thing: &T) -> &mut Self
Append thing
onto the buffer using the Inline
trait
sourcepub fn push<M, T: Pushable<M>>(&mut self, thing: T) -> &mut Self
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 parameterM
(i.e.trait Pushable<M>
) that is not used except to make distinct implementations possible for the same typeimpl<T: Display> Pushable<marker::AsDisplay> for T
impl<T: Inline> Pushable<marker::AsInline> for T
push
is implemented for anyT: Pushable<M>
with anyM
- 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
impl EmitEscapes for Buffer
source§fn escapes_recognized(&self) -> bool
fn escapes_recognized(&self) -> bool
true
, escape codes will be emitted. Read moresource§fn set_color(&mut self, color: ColorCode) -> IoResult<()>
fn set_color(&mut self, color: ColorCode) -> IoResult<()>
\x1B[3???m
)source§impl Write for Buffer
impl Write for Buffer
source§fn write(&mut self, buf: &[u8]) -> IoResult<usize>
fn write(&mut self, buf: &[u8]) -> IoResult<usize>
source§fn flush(&mut self) -> IoResult<()>
fn flush(&mut self) -> IoResult<()>
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)Auto Trait Implementations§
impl RefUnwindSafe for Buffer
impl Send for Buffer
impl Sync for Buffer
impl Unpin for Buffer
impl UnwindSafe for Buffer
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Paint for Twhere
T: EmitEscapes,
impl<T> Paint for Twhere
T: EmitEscapes,
source§fn push_plain<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self
fn push_plain<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self
thing
onto the buffer using the Display
trait (plain uncolored text)source§fn push_bold<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self
fn push_bold<T: Display + ?Sized>(&mut self, thing: &T) -> &mut Self
thing
onto the buffer using the Display
trait (plain uncolored text but in bold)source§fn push_with_color_bold<T: Display + ?Sized>(
&mut self,
color: Color,
thing: &T
) -> &mut Self
fn push_with_color_bold<T: Display + ?Sized>( &mut self, color: Color, thing: &T ) -> &mut Self
push_with_color
but boldsource§fn push_with_any_color<T: Display + ?Sized>(
&mut self,
color: ColorCode,
thing: &T
) -> &mut Self
fn push_with_any_color<T: Display + ?Sized>( &mut self, color: ColorCode, thing: &T ) -> &mut Self
source§fn push_with_any_color_bold<T: Display + ?Sized>(
&mut self,
color: ColorCode,
thing: &T
) -> &mut Self
fn push_with_any_color_bold<T: Display + ?Sized>( &mut self, color: ColorCode, thing: &T ) -> &mut Self
push_with_any_color
but bold