stylish_stringlike

This crate provides a string-like API for styled text objects, and widgets for displaying those styled text objects specifically oriented towards terminal output.

usage

Add this to you Cargo.toml:

toml [dependencies] stylish-stringlike = "0.2.0"

Example

``` rust use std::borrow::Cow; use stylishstringlike::text::{Joinable, Paintable, Pushable, Replaceable, Sliceable, Span, Spans, Tag}; use stylishstringlike::widget::{HBox, TextWidget, TruncationStyle};

let italic = Tag::new("", ""); let bold = Tag::new("", ""); let underline = Tag::new("", "");

let foo: Span = Span::new(Cow::Borrowed(&italic), Cow::Borrowed("foo")); let bar: Span = Span::new(Cow::Borrowed(&bold), Cow::Borrowed("bar"));

// Spans of different styles can be joined together. let foobar = foo.join(&bar); assert_eq!(format!("{}", foobar), "foobar");

// Perform literal string replacement with the replace method. let foobaz = foobar.replace("bar", "baz"); assert_eq!(format!("{}", foobaz), "foobaz");

let mut buz: Spans = Default::default(); buz.push(&Span::new(Cow::Borrowed(&underline), Cow::Borrowed("buz")));

// Replace text with styled text objects instead of string literals. let foobuz = foobar.replace("bar", &buz); assert_eq!(format!("{}", foobuz), "foobuz");

// Use the slice method to slice on bytes. let foob = foobar.slice(..4).unwrap(); assert_eq!(format!("{}", foob), "foob");

// Use the HBox widget to truncate multiple spans of text to fit in a desired width. fn makespans(style: &Tag, text: &str) -> Spans { let mut spans: Spans = Default::default(); let span: Span = Span::new(Cow::Borrowed(style), Cow::Borrowed(text)); spans = spans.join(&span); spans } let truncation = TruncationStyle::Inner(Some(Span::new( Cow::Borrowed(&underline), Cow::Borrowed("…"), ))); let firstspans = makespans(&italic, "abcdefg"); let secondspans = makespans(&bold, "12345678"); let firstsegment = TextWidget::new(&firstspans, &truncation); let secondsegment = TextWidget::new(&second_spans, &truncation);

let mut hbox: HBox> = Default::default(); hbox.push(&firstsegment); hbox.push(&secondsegment); assert_eq!( format!("{}", hbox.truncate(10)), "abfg1278" );

```