[][src]Module crossterm::style

A module to apply attributes and colors on your text.

Style

The style module provides a functionality to apply attributes and colors on your text.

This documentation does not contain a lot of examples. The reason is that it's fairly obvious how to use this crate. Although, we do provide examples repository to demonstrate the capabilities.

Platform-specific Notes

Not all features are supported on all terminals/platforms. You should always consult platform-specific notes of the following types:

Examples

A few examples of how to use the style module.

Colors

How to change the terminal text color.

Command API:

Using the Command API to color text.

use std::io::{stdout, Write};

use crossterm::{execute, Result};
use crossterm::style::{Print, SetForegroundColor, SetBackgroundColor, ResetColor, Color, Attribute};

fn main() -> Result<()> {
    execute!(
        stdout(),
        // Blue foreground
        SetForegroundColor(Color::Blue),
        // Red background
        SetBackgroundColor(Color::Red),
        // Print text
        Print("Blue text on Red.".to_string()),
        // Reset to default colors
        ResetColor
    )
}

Functions:

Using functions from Colorize on a String or &'static str to color it.

use crossterm::style::Colorize;

println!("{}", "Red foreground color & blue background.".red().on_blue());

Attributes

How to appy terminal attributes to text.

Command API:

Using the Command API to set attributes.

use std::io::{stdout, Write};

use crossterm::{execute, Result, style::Print};
use crossterm::style::{SetAttribute, Attribute};

fn main() -> Result<()> {
    execute!(
        stdout(),
        // Set to bold
        SetAttribute(Attribute::Bold),
        Print("Bold text here.".to_string()),
        // Reset all attributes
        SetAttribute(Attribute::Reset)
    )
}

Functions:

Using Styler functions on a String or &'static str to set attributes to it.

use crossterm::style::Styler;

println!("{}", "Bold".bold());
println!("{}", "Underlined".underlined());
println!("{}", "Negative".negative());

Displayable:

Attribute implements Display and therefore it can be formatted like:

use crossterm::style::Attribute;

println!(
    "{} Underlined {} No Underline",
    Attribute::Underlined,
    Attribute::NoUnderline
);

Structs

Attributes

a bitset for all possible attributes

Colors

Represents, optionally, a foreground and/or a background color.

ContentStyle

The style that can be put on content.

Print

A command that prints the given displayable type.

PrintStyledContent

A command that prints styled content.

ResetColor

A command that resets the colors back to default.

SetAttribute

A command that sets an attribute.

SetAttributes

A command that sets several attributes.

SetBackgroundColor

A command that sets the the background color.

SetColors

A command that optionally sets the foreground and/or background color.

SetForegroundColor

A command that sets the the foreground color.

StyledContent

The style with the content to be styled.

Enums

Attribute

Represents an attribute.

Color

Represents a color.

Colored

Represents a foreground or background color.

Traits

Colorize

Provides a set of methods to set the colors.

Styler

Provides a set of methods to set the text attributes.

Functions

available_color_count

Returns available color count.

style

Creates a StyledContent.