ANSI Terminal

ansi_term cat-command-line-interface

This program depicts the use of ansi_term⮳ crate and how it is used for controlling colors and formatting, such as blue bold text or yellow underlined text, on ANSI terminals.

There are two main data structures in ansi_term⮳: ansi_term::ANSIString⮳ and Style⮳. A Style holds stylistic information: colors, whether the text should be bold, or blinking, or whatever. There are also Color variants that represent simple foreground color styles. An ansi_term::ANSIString⮳ is a string paired with a ansi_term::Style⮳.

Note: British English uses Color instead of Color.

Print Colored Text to the Terminal

use ansi_term::Colour;

fn main() {
    println!(
        "This is {} in color, {} in color and {} in color",
        Colour::Red.paint("red"),
        Colour::Blue.paint("blue"),
        Colour::Green.paint("green")
    );
}

Print Bold Text to the Terminal

cat-command-line-interface

For anything more complex than plain foreground color changes, the code needs to construct ansi_term::Style⮳ struct. ansi_term::Style::new⮳ creates the struct, and properties chained.

use ansi_term::Style;

fn main() {
    println!(
        "{} and this is not",
        Style::new().bold().paint("This is Bold")
    );
}

Print Bold and Colored Text to the Terminal

cat-command-line-interface

ansi_term::Color⮳ implements many similar functions as ansi_term::Style⮳ and can chain methods.

use ansi_term::Colour;
use ansi_term::Style;

fn main() {
    println!(
        "{}, {} and {}",
        Colour::Yellow.paint("This is colored"),
        Style::new().bold().paint("this is bold"),
        Colour::Yellow.bold().paint("this is bold and colored")
    );
}

Manipulate the Cursor, Style the Output, Handle Input Events

crossterm crossterm-crates.io crossterm-github crossterm-lib.rs

crossterm⮳ is a pure-Rust, low-level terminal rendering and event handling library used to write cross-platform text-based interfaces. It supports all UNIX and Windows terminals down to Windows 7. Features include the following:

  • Full control over writing and flushing output buffer.
  • Is tty function.
  • Cursor manipulation.
  • Styled output.
  • Terminal handling.
  • Events (key inputs, mouse...).
use std::io::Write;
use std::io::stdout;

use crossterm::ExecutableCommand;
use crossterm::cursor;
use crossterm::event;
use crossterm::event::DisableMouseCapture;
use crossterm::event::EnableMouseCapture;
use crossterm::event::Event;
use crossterm::event::KeyCode;
use crossterm::execute;
use crossterm::queue;
use crossterm::style;
use crossterm::style::Color;
use crossterm::style::Print;
use crossterm::style::SetBackgroundColor;
use crossterm::style::SetForegroundColor;
use crossterm::terminal;
use crossterm::terminal::ClearType;

fn main() -> anyhow::Result<()> {
    let mut stdout = stdout();

    // Enter raw mode and enable mouse capture
    terminal::enable_raw_mode()?;
    // Execute the command immediately
    execute!(stdout, EnableMouseCapture)?;

    // Clear the screen and move cursor to the top left
    // You can queue commands instead of executing them directly
    // when you call `Write::flush` these commands will be executed.
    queue!(
        stdout,
        terminal::Clear(ClearType::All),
        cursor::MoveTo(0, 0)
    )?;
    // Many other commands...
    stdout.flush()?;

    // Display instructions
    println!("Press 'q' to exit.");
    execute!(
        stdout,
        SetForegroundColor(Color::Blue),
        SetBackgroundColor(Color::White),
        Print("Hello, Crossterm!\n")
    )?;
    // We can use the `execute` function rather than the macro.
    // The `execute` function returns itself,
    // therefore you can queue another command.
    stdout.execute(style::ResetColor)?;

    loop {
        // Listen for key events
        if let Event::Key(key_event) = event::read()? {
            match key_event.code {
                KeyCode::Char('q') => {
                    break;
                }
                KeyCode::Up => {
                    println!("Up arrow key pressed.");
                }
                KeyCode::Down => {
                    println!("Down arrow key pressed.");
                }
                KeyCode::Left => {
                    println!("Left arrow key pressed.");
                }
                KeyCode::Right => {
                    println!("Right arrow key pressed.");
                }
                _ => {}
            }
        }
    }

    // Leave raw mode and disable mouse capture
    terminal::disable_raw_mode()?;
    execute!(stdout, DisableMouseCapture)?;

    Ok(())
}

termcolor

termcolor termcolor-crates.io termcolor-github termcolor-lib.rs

termcolor is a simple cross platform library for writing colored text to a terminal. It offers a straightforward way to add colored output to your terminal applications in Rust, working consistently across different operating systems. It supports various color choices, text styling, and provides options for controlling where the colored output is directed, such as standard output or standard error.

anstyle

anstyle-website anstyle anstyle-crates.io anstyle-github anstyle-lib.rs cat-command-line-interface

anstyle provides composable and spec-compliant ANSI escape code manipulation for styling terminal output.

anstream

anstream-website anstream anstream-crates.io anstream-github anstream-lib.rs cat-command-line-interface

anstream is a cross-platform library for writing colored text to a terminal. It offers a streaming API for composing and writing styled ANSI output to terminals. It facilitates efficient construction of complex styled strings via chaining, minimizing allocations and optimizing write operations. anstream supports configurable output destinations (stdout, stderr, or custom writers) and guarantees correct ANSI escape code handling for cross-platform compatibility.

nu-ansi-term

nu-ansi-term nu-ansi-term-crates.io nu-ansi-term-github nu-ansi-term-lib.rs

nu-ansi-term is a library for ANSI terminal colors and styles (e.g. bold, underline). nu-ansi-term provides ANSI terminal coloring and styling capabilities, particularly focused on supporting the styling needs of the NuShell project. When used independently, it offers a convenient and familiar API for those already working within the Nu ecosystem, enabling styled terminal output with support for common formatting options.

ansiterm

ansiterm ansiterm-crates.io ansiterm-github ansiterm-lib.rs

ansiterm is a library for ANSI terminal colors and styles (bold, underline). It provides ANSI escape code manipulation for terminal styling, offering a more direct and lower-level approach compared to some higher-level crates.

console

console console-crates.io console-github console-lib.rs

console is a terminal and console abstraction for Rust. The console crate provides a comprehensive set of tools for building interactive console applications. It offers features such as styled text output with ANSI escape code support, progress bar rendering, user input handling (including password prompting and line editing), and terminal manipulation. console aims to simplify the development of rich command-line interfaces by abstracting away platform-specific terminal complexities and providing a consistent API for common console interactions.

owo-colors

owo-colors owo-colors-crates.io owo-colors-github owo-colors-lib.rscat-command-line-interface

owo-colors is a zero-allocation terminal colors that will make people go 'owo'. It provides a simple and fast way to add color to terminal output. It leverages ANSI escape codes for styling and focuses on a concise API for common use cases, prioritizing speed and ease of use over more complex styling features. It's designed to be lightweight and efficient, minimizing overhead for applications where basic terminal coloring is sufficient.

stylish

stylish stylish-crates.io stylish-github stylish-lib.rs

stylish is another crate implementing colorized text.

yansi

yansi yansi-crates.io yansi-github yansi-lib.rs cat-command-line-interface

yansi is a simple ANSI terminal color painting library. It provides an ergonomic and composable API for styling terminal output with ANSI escape codes. It emphasizes ease of use through its builder-like interface, allowing developers to chain styling methods and construct complex formatted strings.

termion

termion termion-crates.io termion-github termion-lib.rs

termion is a pure Rust, bindless library for low-level handling, manipulating and reading information about terminals. This provides a full-featured alternative to Termbox.

Termion aims to be simple and yet expressive. It is bindless, meaning that it is not a front-end to some other library (e.g., ncurses or termbox), but a standalone library directly talking to the TTY.

Termion is a pure Rust library that provides a cross-platform interface for controlling the terminal. It gives access to advanced terminal features like cursor manipulation, color control, and raw mode, enabling developers to create interactive command-line applications. Being pure Rust, it avoids external dependencies and offers predictable performance.

colored

colored colored-crates.io colored-github colored-lib.rs

The most simple way to add colors in your terminal.

Related Topics

  • User Interaction.