[][src]Struct term_string::TermString

pub struct TermString { /* fields omitted */ }

A string type with term styling info attached to it.

Internally, TermString contains multiple strings, each one of them has a TermStyle attached to it.

Methods

impl TermString
[src]

Basic methods for constructing and modifying TermStrings,

Create a TermString variable from a TermStyle and a string value.

Examples

let bold = TermStyle::bold();
let ts = TermString::new(bold, "some bold text");
ts.println();

Return the length of the un-styled string contained in TermString.

Examples

let bold = TermStyle::bold();
let underline = TermStyle::underline(true);
let mut ts = TermString::new(bold, "some bold text ");
ts += TermString::new(underline, "and some underlined text.");
assert_eq!(ts.len(), 40);

Return true if the un-styled string contained in TermString is empty.

Examples

let bold = TermStyle::bold();
let mut ts = TermString::new(bold, "");
assert!(ts.is_empty());
ts += "this is bold."

Return the un-styled string contained in TermString.

Examples

let bold = TermStyle::bold();
let underline = TermStyle::underline(true);
let mut ts = TermString::new(bold, "some bold text ");
ts += TermString::new(underline, "and some underlined text.");
let s = "some bold text and some underlined text.";
assert_eq!(ts.as_string(), s);

Append a string value to a TermString, inheriting the previous style.

Examples

let bold = TermStyle::bold();
let mut ts = TermString::new(bold, "some bold text ");
ts.append_str("and other bold text");
ts.println();

Note that the line:

ts.append_str("and other bold text");

is equivalent to:

ts += "and other bold text";

Append a TermString to a TermString.

Examples

let bold = TermStyle::bold();
let underline = TermStyle::underline(true);
let mut ts = TermString::new(bold, "some bold text ");
let ts2 = TermString::new(underline, "and some underlined text.");
ts.append_term_str(ts2);
ts.println();

Note that the line:

ts.append_term_str(ts2);

is equivalent to:

ts += ts2;

Also note that the method's argument type is Into<Self>, and From<S> for TermString where S: Borrow<str> is implemented.

So, this works:

let bold = TermStyle::bold();
let mut ts = TermString::new(bold, "some bold text ");
ts.append_term_str("and some un-styled text.");
ts.println();

Note that the method argument in the example above is converted into a TermString with a Default style first before appending. Contrast this with the behavior of append_str(), where the appended value inherits the previous style.

So, the line:

ts.append_term_str("and some un-styled text.");

is equivalent to:

ts += TermString::from("and some un-styled text.");

which in turn is equivalent to:

ts += TermString::new(TermStyle::default(), "and some un-styled text.");

This is effectively an alias to append_term_str()

The chaining equivalent of append_str().

The chaining equivalent of append_term_str().

The chaining equivalent of append().

impl TermString
[src]

Method for modifying the style of all internal elements of a TermString.

A corresponding method from TermStyle is used on each internal element of the TermString.

Remember that TermStyle is a Copy type.

Set the styles of all internal elements of the TermString to this style.

Examples

use term_string::{TermString, TermStyle, color};

let fg_bg = TermStyle::bg(color::RED) + TermStyle::fg(color::WHITE);
let underline = TermStyle::underline(true);

let mut ts = TermString::new(fg_bg, "fg bg");
ts += TermString::new(underline, " underline");

ts.set_style(TermStyle::bold());

// This will print "fg bg underline" in bold and without
// foreground or background colors or underline.
ts.println();

The chaining equivalent of set_style().

Reset the styles of all internal elements of the TermString.

Examples

use term_string::{TermString, TermStyle, color};

let fg_bg = TermStyle::bg(color::RED) + TermStyle::fg(color::WHITE);
let underline = TermStyle::underline(true);

let mut ts = TermString::new(fg_bg, "fg bg");
ts += TermString::new(underline, " underline");

ts.reset_style();

// This will print "fg bg underline" without any styling
ts.println();

The chaining equivalent of reset_style().

Calls TermStyle::or_style() on each internal element of the TermString.

Examples

use term_string::{TermString, TermStyle, color};

let fg_bg = TermStyle::bg(color::RED) + TermStyle::fg(color::WHITE);
let underline = TermStyle::underline(true);

let mut ts = TermString::new(fg_bg, "fg bg");
ts += TermString::new(underline, " underline");

ts.or_style(TermStyle::bg(color::BLUE));

// This will print "fg bg" with red background and white foreground,
// then " underline" with underline and blue background.
ts.println();

The chaining equivalent of or_style().

Calls TermStyle::add_style() on each internal element of the TermString.

Examples

use term_string::{TermString, TermStyle, color};

let fg_bg = TermStyle::bg(color::RED) + TermStyle::fg(color::WHITE);
let underline = TermStyle::underline(true);

let mut ts = TermString::new(fg_bg, "fg bg");
ts += TermString::new(underline, " underline");

ts.add_style(TermStyle::bg(color::BLUE));

// This will print "fg bg" with blue background and white foreground,
// then " underline" with underline and blue background.
ts.println();

The chaining equivalent of add_style().

impl TermString
[src]

IO write/print methods

Note

TermWrite is bound to Write + Send on Windows, and only Write on other platforms.

Write TermString to out without styling.

Examples

let bold = TermStyle::bold();
let ts = TermString::new(bold, "some bold text");

// This will write "some bold text" to stdout without
// any formatting, so not really bold.
ts.write_plain(std::io::stdout());

Write TermString to out with styling.

Note

  • out doesn't have to be an actual tty.
  • If on Windows, and getting terminfo fails, the styling info will be set on the current console, regardless of what out is.

Check out print(), println(), eprint(), and eprintln() below, where out is checked before styled output is written to it.

Examples

let bold = TermStyle::bold();
let ts = TermString::new(bold, "some bold text");

// This will write "some bold text" to stdout with formatting,
// even if stdout is not a tty
ts.write_styled(std::io::stdout());

Write TermString to stdout without styling.

Examples

let bold = TermStyle::bold();
let ts = TermString::new(bold, "some bold text");

// This will write "some bold text" to `stdout` without
// any formatting, so not really bold.
ts.print_plain();

Print TermString to stdout with styling.

Note

stdout doesn't have to be an actual tty.

Check out print() below, where stdout is checked before styled output is printed.

Examples

let bold = TermStyle::bold();
let ts = TermString::new(bold, "some bold text");

// This will write "some bold text" to stdout
// as bold text.
ts.print_styled();

Print TermString to stdout with styling if stdout is a tty, and without if it's not.

Examples

let bold = TermStyle::bold();
let ts = TermString::new(bold, "some bold text");

// This will write "some bold text" to stdout as bold
// text if stdout is a tty, and without any formatting if not.
ts.print();

The same as print_plain(), but with a newline printed at the end.

The same as print_styled(), but with a newline printed at the end.

The same as print(), but with a newline printed at the end.

Write TermString to stderr without styling.

Examples

let bold = TermStyle::bold();
let ts = TermString::new(bold, "some bold text");

// This will write "some bold text" to `stderr` without
// any formatting, so not really bold.
ts.eprint_plain();

Print TermString to stderr with styling.

Note

stderr doesn't have to be an actual tty.

Check out eprint() below, where stderr is checked before styled output is printed.

Examples

let bold = TermStyle::bold();
let ts = TermString::new(bold, "some bold text");

// This will write "some bold text" to stderr
// as bold text.
ts.eprint_styled();

Print TermString to stderr with styling if stderr is a tty, and without if it's not.

Examples

let bold = TermStyle::bold();
let ts = TermString::new(bold, "some bold text");

// This will write "some bold text" to stderr as bold
// text if stderr is a tty, and without any formatting if not.
ts.eprint();

The same as eprint_plain(), but with a newline printed at the end.

The same as eprint_styled(), but with a newline printed at the end.

The same as eprint(), but with a newline printed at the end.

Trait Implementations

impl Clone for TermString
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Default for TermString
[src]

Returns the "default value" for a type. Read more

impl Debug for TermString
[src]

Formats the value using the given formatter. Read more

impl<S> From<S> for TermString where
    S: Borrow<str>, 
[src]

Performs the conversion.

impl<S> Add<S> for TermString where
    S: Borrow<str>, 
[src]

The resulting type after applying the + operator.

Performs the + operation.

impl<S> AddAssign<S> for TermString where
    S: Borrow<str>, 
[src]

Performs the += operation.

impl Add for TermString
[src]

The resulting type after applying the + operator.

Performs the + operation.

impl AddAssign for TermString
[src]

Performs the += operation.

Auto Trait Implementations

impl Send for TermString

impl Sync for TermString