prettytty::util

Enum ByteFormat

Source
pub enum ByteFormat<'a> {
    Concise(&'a [u8]),
    Nicely(&'a [u8]),
    Hexdump(&'a [u8]),
}
Expand description

Display a byte string in a more humane manner.

The intended use for this enumeration is wrapping byte strings before handing them off to one of Rust’s formatting macros. However, the low-level ByteFormat::render method, especially when combined with a Rewriter instance, enables other use cases, too.

§Example

assert_eq!(
    format!("{}", ByteFormat::Concise(b"\x1b[1m\x90@\xfe\x07")),
    "␛[1m.@.␇"
);
assert_eq!(
    format!("{}", ByteFormat::Nicely(b"\x1b[1m\x90@\xfe\x07")),
    "‹ESC›[1m‹DCS›@「FE」‹BEL›"
);
assert_eq!(
    format!("{}", ByteFormat::Hexdump(b"\x1b[1m\x90@\xfe\x07")),
    "0000:  1b5b 316d 9040 fe07  ␛[1m.@.␇"
);

Variants§

§

Concise(&'a [u8])

The concise format uses one character per byte. It displays C0 control codes with Unicode control pictures (which may be hard to read) and replaces bytes larger than 0x7F with a period .

§

Nicely(&'a [u8])

The elaborate format uses more than one character per byte where necessary. It displays C0 control codes as well as select C1 control codes as mnemonics between guillemets, e.g., ‹ESC› for 0x1B. It displays bytes larger than 0x7F as hexadecimal numbers between corner brackets, e.g., 「A0」 for 0xA0.

§

Hexdump(&'a [u8])

The hexdump format combines hexadecimal and concise formatting. Unlike the other formats, it is line-oriented, displaying up to 16 bytes per line.

Implementations§

Source§

impl ByteFormat<'_>

Source

pub fn render<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, Error>

Render the bytes with the given writer.

This method largely is an implementation detail. It differs from the display trait by accepting arbitrary writers and by returning the number of characters (not bytes) written. It is public to support applications that require either of these features.

Since the hexdump format is line-oriented, it emits newlines for all but the last line. The number of characters written only covers that last line.

Trait Implementations§

Source§

impl<'a> Debug for ByteFormat<'a>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for ByteFormat<'_>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for ByteFormat<'a>

§

impl<'a> RefUnwindSafe for ByteFormat<'a>

§

impl<'a> Send for ByteFormat<'a>

§

impl<'a> Sync for ByteFormat<'a>

§

impl<'a> Unpin for ByteFormat<'a>

§

impl<'a> UnwindSafe for ByteFormat<'a>

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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>,

Source§

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.