pub trait Stylize: Sized {
    type Styled: AsRef<ContentStyle> + AsMut<ContentStyle>;

Show 64 methods // Required method fn stylize(self) -> Self::Styled; // Provided methods fn with(self, color: Color) -> Self::Styled { ... } fn on(self, color: Color) -> Self::Styled { ... } fn underline(self, color: Color) -> Self::Styled { ... } fn attribute(self, attr: Attribute) -> Self::Styled { ... } fn reset(self) -> Self::Styled { ... } fn bold(self) -> Self::Styled { ... } fn underlined(self) -> Self::Styled { ... } fn reverse(self) -> Self::Styled { ... } fn dim(self) -> Self::Styled { ... } fn italic(self) -> Self::Styled { ... } fn negative(self) -> Self::Styled { ... } fn slow_blink(self) -> Self::Styled { ... } fn rapid_blink(self) -> Self::Styled { ... } fn hidden(self) -> Self::Styled { ... } fn crossed_out(self) -> Self::Styled { ... } fn black(self) -> Self::Styled { ... } fn on_black(self) -> Self::Styled { ... } fn underline_black(self) -> Self::Styled { ... } fn dark_grey(self) -> Self::Styled { ... } fn on_dark_grey(self) -> Self::Styled { ... } fn underline_dark_grey(self) -> Self::Styled { ... } fn red(self) -> Self::Styled { ... } fn on_red(self) -> Self::Styled { ... } fn underline_red(self) -> Self::Styled { ... } fn dark_red(self) -> Self::Styled { ... } fn on_dark_red(self) -> Self::Styled { ... } fn underline_dark_red(self) -> Self::Styled { ... } fn green(self) -> Self::Styled { ... } fn on_green(self) -> Self::Styled { ... } fn underline_green(self) -> Self::Styled { ... } fn dark_green(self) -> Self::Styled { ... } fn on_dark_green(self) -> Self::Styled { ... } fn underline_dark_green(self) -> Self::Styled { ... } fn yellow(self) -> Self::Styled { ... } fn on_yellow(self) -> Self::Styled { ... } fn underline_yellow(self) -> Self::Styled { ... } fn dark_yellow(self) -> Self::Styled { ... } fn on_dark_yellow(self) -> Self::Styled { ... } fn underline_dark_yellow(self) -> Self::Styled { ... } fn blue(self) -> Self::Styled { ... } fn on_blue(self) -> Self::Styled { ... } fn underline_blue(self) -> Self::Styled { ... } fn dark_blue(self) -> Self::Styled { ... } fn on_dark_blue(self) -> Self::Styled { ... } fn underline_dark_blue(self) -> Self::Styled { ... } fn magenta(self) -> Self::Styled { ... } fn on_magenta(self) -> Self::Styled { ... } fn underline_magenta(self) -> Self::Styled { ... } fn dark_magenta(self) -> Self::Styled { ... } fn on_dark_magenta(self) -> Self::Styled { ... } fn underline_dark_magenta(self) -> Self::Styled { ... } fn cyan(self) -> Self::Styled { ... } fn on_cyan(self) -> Self::Styled { ... } fn underline_cyan(self) -> Self::Styled { ... } fn dark_cyan(self) -> Self::Styled { ... } fn on_dark_cyan(self) -> Self::Styled { ... } fn underline_dark_cyan(self) -> Self::Styled { ... } fn white(self) -> Self::Styled { ... } fn on_white(self) -> Self::Styled { ... } fn underline_white(self) -> Self::Styled { ... } fn grey(self) -> Self::Styled { ... } fn on_grey(self) -> Self::Styled { ... } fn underline_grey(self) -> Self::Styled { ... }
}
Expand description

Provides a set of methods to set attributes and colors.

§Examples

use crossterm::style::Stylize;

println!("{}", "Bold text".bold());
println!("{}", "Underlined text".underlined());
println!("{}", "Negative text".negative());
println!("{}", "Red on blue".red().on_blue());

Required Associated Types§

source

type Styled: AsRef<ContentStyle> + AsMut<ContentStyle>

This type with styles applied.

Required Methods§

source

fn stylize(self) -> Self::Styled

Styles this type.

Provided Methods§

source

fn with(self, color: Color) -> Self::Styled

Sets the foreground color.

source

fn on(self, color: Color) -> Self::Styled

Sets the background color.

source

fn underline(self, color: Color) -> Self::Styled

Sets the underline color.

source

fn attribute(self, attr: Attribute) -> Self::Styled

Styles the content with the attribute.

source

fn reset(self) -> Self::Styled

Applies the Reset attribute to the text.

source

fn bold(self) -> Self::Styled

Applies the Bold attribute to the text.

source

fn underlined(self) -> Self::Styled

Applies the Underlined attribute to the text.

source

fn reverse(self) -> Self::Styled

Applies the Reverse attribute to the text.

source

fn dim(self) -> Self::Styled

Applies the Dim attribute to the text.

source

fn italic(self) -> Self::Styled

Applies the Italic attribute to the text.

source

fn negative(self) -> Self::Styled

Applies the Reverse attribute to the text.

Applies the SlowBlink attribute to the text.

Applies the RapidBlink attribute to the text.

source

fn hidden(self) -> Self::Styled

Applies the Hidden attribute to the text.

source

fn crossed_out(self) -> Self::Styled

Applies the CrossedOut attribute to the text.

source

fn black(self) -> Self::Styled

Sets the foreground color to Black.

source

fn on_black(self) -> Self::Styled

Sets the background color to Black.

source

fn underline_black(self) -> Self::Styled

Sets the underline color to Black.

source

fn dark_grey(self) -> Self::Styled

Sets the foreground color to DarkGrey.

source

fn on_dark_grey(self) -> Self::Styled

Sets the background color to DarkGrey.

source

fn underline_dark_grey(self) -> Self::Styled

Sets the underline color to DarkGrey.

source

fn red(self) -> Self::Styled

Sets the foreground color to Red.

Examples found in repository?
examples/event_visualizer.rs (line 24)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
fn main() -> Result<(), Box<dyn Error>> {
    let mut event = Event::FocusGained;
    let mut window = Window::init()?;

    if !window.supports().keyboard() {
        window.restore()?;
        eprintln!("This game does not support this terminal.\nIf Curious, look up terminals that support the kitty keyboard protocol");
        return Err("Terminal Unsupported".into());
    }

    loop {
        window.update(Duration::ZERO)?;

        if let Some(new_event) = window.event() {
            event = new_event.clone();
        }

        render!(
            window, [
                vec2(0, 20) => "To Quit, Press Ctrl + C".red(),
                vec2(0, 0) => format!("{:#?}", event).replace('\t', "   "),
            ]
        );

        if window.key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)) {
            break;
        }
    }

    Ok(())
}
More examples
Hide additional examples
examples/simple.rs (line 20)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() -> io::Result<()> {
    // Will init the window for you, handling all required procedures.
    let mut window = Window::init()?;

    // Ask the system to handle panics for us.
    handle_panics();

    loop {
        // Ask the window to draw, handle events, and fix sizing issues.
        // Duration is the time for which to poll events before re-rendering.
        window.update(Duration::from_millis(200))?;

        // Render elements to the window
        render!(window, [
            vec2(0, 0) => "Hello World!",
            vec2(0, 1) => "Press `Enter` to exit!".red(),
        ]);

        // Check if the Enter Key was pressed, and exit the app if it was.
        if window.code(KeyCode::Enter) {
            break;
        }
    }

    // Restore the window, enabling the window to function normally again
    // If nothing will be run after this, once the window is dropped, this will be run implicitly.
    window.restore()
}
examples/inline.rs (line 30)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
fn progress_bar() -> io::Result<()> {
    let mut window = Window::init_inline(2)?;

    let timer = SystemTime::now();
    let duration = Duration::from_secs(3);

    // The Inline Render Loop
    loop {
        // Render's the Window and captures events
        window.update(Duration::ZERO)?;

        let amount_done = SystemTime::now().duration_since(timer).unwrap();

        let percent = amount_done.as_secs_f64() / duration.as_secs_f64();

        if percent >= 1.0 {
            break;
        }

        let x = (window.size().x as f64 * percent).round() as u16;

        // Create the progress bar text
        let text_green = "|".repeat(x as usize).green();
        let text_red = "|".repeat((window.size().x - x) as usize).red();

        // Render the Progress Bar
        render!(window, [
            vec2(0, 1) => text_green,
            vec2(x, 1) => text_red,
            vec2(0, 0) => "Progress"
        ]);

        // End the loop if key is pressed early
        if window.key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)) {
            break;
        }
    }

    window.restore()
}
examples/space_invaders.rs (line 230)
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
pub fn app(window: &mut Window) -> io::Result<String> {
    let mut score = 0;
    let mut player = Player::new(window, 'W'.green());

    let mut projectiles = vec![];

    let mut enemies = vec![];

    let mut delta = Duration::ZERO;

    let mut spawner = Duration::from_millis(800);

    let mut move_timer = Duration::from_millis(200);

    let mut shoot_timer = Duration::from_millis(500);

    let info_text = Buffer::sized_element("Press C-q to quit");

    // Main Game Loop
    loop {
        let start = SystemTime::now();
        // update the window, without blocking the screen
        window.update(Duration::from_secs_f64(1.0 / 60.0))?;

        if window.code(KeyCode::Char(' ')) {
            projectiles.push(Projectile::new(
                vec2(player.loc.x - 1, player.loc.y - 1),
                -0.3,
                "|||".green(),
            ))
        }

        // Render and update projectiles
        projectiles.retain(|x| x.alive(window));

        projectiles.iter_mut().for_each(|x| {
            x.update();
            x.draw(window);
        });

        // Render and update the player.
        player.update(window);
        player.draw(window);

        match spawner.checked_sub(delta) {
            Some(s) => spawner = s,
            None => {
                enemies.push(Enemy::new(vec2(0, 3), 'M'.red(), 10));
                spawner = Duration::from_secs(2);
            }
        }

        match move_timer.checked_sub(delta) {
            Some(m) => move_timer = m,
            None => {
                if enemies.iter_mut().any(|x| x.enemy_move(window)) {
                    return Ok(format!("Game Over\nScore was: {}", score));
                }
                move_timer = Duration::from_millis(200);
            }
        }

        if player.hit(&projectiles) {
            return Ok(format!("Game Over\nScore was: {}", score));
        }

        match shoot_timer.checked_sub(delta) {
            Some(s) => shoot_timer = s,
            None => {
                enemies.iter_mut().for_each(|x| {
                    projectiles.push(Projectile::new(vec2(x.loc.x, x.loc.y + 1), 0.3, "|".red()))
                });
                shoot_timer = Duration::from_millis(500);
            }
        }

        enemies.retain_mut(|x| {
            if x.hit(&projectiles) {
                score += x.score;
                false
            } else {
                true
            }
        });

        enemies.iter_mut().for_each(|x| x.draw(window));

        render!(
            window, [
                vec2(0, 0) => format!("Score: {}", score),
                vec2(window.size().x - info_text.size().x, 0) => info_text
            ]
        );

        if window.key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)) {
            break;
        }

        delta = SystemTime::now().duration_since(start).unwrap();
    }

    Ok("Game Exited".to_string())
}
source

fn on_red(self) -> Self::Styled

Sets the background color to Red.

source

fn underline_red(self) -> Self::Styled

Sets the underline color to Red.

source

fn dark_red(self) -> Self::Styled

Sets the foreground color to DarkRed.

source

fn on_dark_red(self) -> Self::Styled

Sets the background color to DarkRed.

source

fn underline_dark_red(self) -> Self::Styled

Sets the underline color to DarkRed.

source

fn green(self) -> Self::Styled

Sets the foreground color to Green.

Examples found in repository?
examples/inline.rs (line 29)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
fn progress_bar() -> io::Result<()> {
    let mut window = Window::init_inline(2)?;

    let timer = SystemTime::now();
    let duration = Duration::from_secs(3);

    // The Inline Render Loop
    loop {
        // Render's the Window and captures events
        window.update(Duration::ZERO)?;

        let amount_done = SystemTime::now().duration_since(timer).unwrap();

        let percent = amount_done.as_secs_f64() / duration.as_secs_f64();

        if percent >= 1.0 {
            break;
        }

        let x = (window.size().x as f64 * percent).round() as u16;

        // Create the progress bar text
        let text_green = "|".repeat(x as usize).green();
        let text_red = "|".repeat((window.size().x - x) as usize).red();

        // Render the Progress Bar
        render!(window, [
            vec2(0, 1) => text_green,
            vec2(x, 1) => text_red,
            vec2(0, 0) => "Progress"
        ]);

        // End the loop if key is pressed early
        if window.key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)) {
            break;
        }
    }

    window.restore()
}
More examples
Hide additional examples
examples/space_invaders.rs (line 185)
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
pub fn app(window: &mut Window) -> io::Result<String> {
    let mut score = 0;
    let mut player = Player::new(window, 'W'.green());

    let mut projectiles = vec![];

    let mut enemies = vec![];

    let mut delta = Duration::ZERO;

    let mut spawner = Duration::from_millis(800);

    let mut move_timer = Duration::from_millis(200);

    let mut shoot_timer = Duration::from_millis(500);

    let info_text = Buffer::sized_element("Press C-q to quit");

    // Main Game Loop
    loop {
        let start = SystemTime::now();
        // update the window, without blocking the screen
        window.update(Duration::from_secs_f64(1.0 / 60.0))?;

        if window.code(KeyCode::Char(' ')) {
            projectiles.push(Projectile::new(
                vec2(player.loc.x - 1, player.loc.y - 1),
                -0.3,
                "|||".green(),
            ))
        }

        // Render and update projectiles
        projectiles.retain(|x| x.alive(window));

        projectiles.iter_mut().for_each(|x| {
            x.update();
            x.draw(window);
        });

        // Render and update the player.
        player.update(window);
        player.draw(window);

        match spawner.checked_sub(delta) {
            Some(s) => spawner = s,
            None => {
                enemies.push(Enemy::new(vec2(0, 3), 'M'.red(), 10));
                spawner = Duration::from_secs(2);
            }
        }

        match move_timer.checked_sub(delta) {
            Some(m) => move_timer = m,
            None => {
                if enemies.iter_mut().any(|x| x.enemy_move(window)) {
                    return Ok(format!("Game Over\nScore was: {}", score));
                }
                move_timer = Duration::from_millis(200);
            }
        }

        if player.hit(&projectiles) {
            return Ok(format!("Game Over\nScore was: {}", score));
        }

        match shoot_timer.checked_sub(delta) {
            Some(s) => shoot_timer = s,
            None => {
                enemies.iter_mut().for_each(|x| {
                    projectiles.push(Projectile::new(vec2(x.loc.x, x.loc.y + 1), 0.3, "|".red()))
                });
                shoot_timer = Duration::from_millis(500);
            }
        }

        enemies.retain_mut(|x| {
            if x.hit(&projectiles) {
                score += x.score;
                false
            } else {
                true
            }
        });

        enemies.iter_mut().for_each(|x| x.draw(window));

        render!(
            window, [
                vec2(0, 0) => format!("Score: {}", score),
                vec2(window.size().x - info_text.size().x, 0) => info_text
            ]
        );

        if window.key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)) {
            break;
        }

        delta = SystemTime::now().duration_since(start).unwrap();
    }

    Ok("Game Exited".to_string())
}
source

fn on_green(self) -> Self::Styled

Sets the background color to Green.

source

fn underline_green(self) -> Self::Styled

Sets the underline color to Green.

source

fn dark_green(self) -> Self::Styled

Sets the foreground color to DarkGreen.

source

fn on_dark_green(self) -> Self::Styled

Sets the background color to DarkGreen.

source

fn underline_dark_green(self) -> Self::Styled

Sets the underline color to DarkGreen.

source

fn yellow(self) -> Self::Styled

Sets the foreground color to Yellow.

source

fn on_yellow(self) -> Self::Styled

Sets the background color to Yellow.

source

fn underline_yellow(self) -> Self::Styled

Sets the underline color to Yellow.

source

fn dark_yellow(self) -> Self::Styled

Sets the foreground color to DarkYellow.

source

fn on_dark_yellow(self) -> Self::Styled

Sets the background color to DarkYellow.

source

fn underline_dark_yellow(self) -> Self::Styled

Sets the underline color to DarkYellow.

source

fn blue(self) -> Self::Styled

Sets the foreground color to Blue.

source

fn on_blue(self) -> Self::Styled

Sets the background color to Blue.

source

fn underline_blue(self) -> Self::Styled

Sets the underline color to Blue.

source

fn dark_blue(self) -> Self::Styled

Sets the foreground color to DarkBlue.

source

fn on_dark_blue(self) -> Self::Styled

Sets the background color to DarkBlue.

source

fn underline_dark_blue(self) -> Self::Styled

Sets the underline color to DarkBlue.

source

fn magenta(self) -> Self::Styled

Sets the foreground color to Magenta.

source

fn on_magenta(self) -> Self::Styled

Sets the background color to Magenta.

source

fn underline_magenta(self) -> Self::Styled

Sets the underline color to Magenta.

source

fn dark_magenta(self) -> Self::Styled

Sets the foreground color to DarkMagenta.

source

fn on_dark_magenta(self) -> Self::Styled

Sets the background color to DarkMagenta.

source

fn underline_dark_magenta(self) -> Self::Styled

Sets the underline color to DarkMagenta.

source

fn cyan(self) -> Self::Styled

Sets the foreground color to Cyan.

source

fn on_cyan(self) -> Self::Styled

Sets the background color to Cyan.

source

fn underline_cyan(self) -> Self::Styled

Sets the underline color to Cyan.

source

fn dark_cyan(self) -> Self::Styled

Sets the foreground color to DarkCyan.

source

fn on_dark_cyan(self) -> Self::Styled

Sets the background color to DarkCyan.

source

fn underline_dark_cyan(self) -> Self::Styled

Sets the underline color to DarkCyan.

source

fn white(self) -> Self::Styled

Sets the foreground color to White.

source

fn on_white(self) -> Self::Styled

Sets the background color to White.

source

fn underline_white(self) -> Self::Styled

Sets the underline color to White.

source

fn grey(self) -> Self::Styled

Sets the foreground color to Grey.

source

fn on_grey(self) -> Self::Styled

Sets the background color to Grey.

source

fn underline_grey(self) -> Self::Styled

Sets the underline color to Grey.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Stylize for &str

source§

impl Stylize for char

source§

impl Stylize for String

Implementors§