Skip to main content

Crate termint

Crate termint 

Source
Expand description

§termint

Crates.io Version docs.rs Crates.io Total Downloads

termint is high-performance, structural TUI (Terminal User Interface) library and framework for Rust.

§Table of Contents

§How to get it

This crate is available on crates.io.

§With cargo

cargo add termint

§In Cargo.toml

[dependencies]
termint = "0.7.0"

§Features

  • backend-crossterm: Enables Crossterm as the event-reading backend.
  • backend-termal: Enables Termal as the event-reading backend.
  • serde: Enables serialization and deserialization of some structs.
  • all: Enables all features.

backend-crossterm is enabled by default. If both backend-crossterm and backend-termal are enabled, Termint defaults to Crossterm, unless specified otherwise via Term::<Backend>::init().

§Examples

§Framework Mode

Note: To use Framework mode, you must enable either the backend-crossterm or backend-termal feature.

Termint provides the Application trait to manage the UI lifecycle. You can then use Term::run, which runs the main loop, handles the input events and efficient rendering all using the given Application implementation.

use termint::prelude::*;

struct MyApp;

impl Application for MyApp {
    fn view(&self, _frame: &Frame) -> Element {
        let mut main = Block::vertical().title("Termint App");
        main.push("Hello from the Application trait!".fg(Color::Cyan), 0..);
        main.into()
    }

    fn event(&mut self, event: Event) -> Action {
        match event {
            Event::Key(k) if k.code == KeyCode::Char('q') => Action::QUIT,
            _ => Action::NONE,
        }
    }
}

fn main() -> Result<(), Error> {
    Term::default().setup()?.run(&mut MyApp)
}

§Manual Mode

If you prefer to manage you own loop, you can use the manual mode, where Term manages only terminal state and rendering. Everything else, such as the main loop, event handling and so on, you have to implement yourself.

use termint::prelude::*;

fn main() -> Result<(), Error> {
    let mut term = Term::default().setup()?;
    loop {
        // 1. Custom event handling
        // 2. Logic updates

        let mut main = Block::vertical().title("Termint App");
        main.push("Hello from the Manual mode!".fg(Color::Red), 0..);

        // 3. Render on demand
        term.render(main)?;
    }
}

§Colored printing

Colored printing is really easy, you can do it by using any Text widget. Here is an example of using Span widget:

use termint::prelude::*;

println!("{}", "Cyan text".fg(Color::Cyan));
println!("{}", "Cyan text on white".fg(Color::Cyan).bg(Color::White));
println!("{}", "Bold red text".fg(Color::Red).modifier(Modifier::BOLD));
println!("{}", "Text with RGB value".fg(Color::Rgb(0, 249, 210)));

image

You can also use re-exported termal crate to print colored text:

use termint::termal::printcln;

printcln!("{'yellow italic}Yellow Italic text{'reset}");
printcln!("{'y i}{}{'_}", "Yellow Italic text");
printcln!("{'#dd0 i}{}{'_}", "Custom Yellow Italic text");

§TUI examples

image

image

image

§Usage

Code blocks above are just examples of the usage. To see more about functions, Widgets and more, please visit the documentation.

You can also check the examples directory of this repository for more examples of how to use this crate for creating TUIs.

§Projects

Here is a list of some projects using termint:

Re-exports§

pub use termal;

Modules§

buffer
enums
Contains enums for foreground, background and more
geometry
Contains structs for geometry, such as Coords
macros
Contains useful macros
prelude
style
term
Contains Term struct Core terminal abstraction and application lifecycle.
text
A collection of text related types.
widgets
Contains widgets (Layout, Block, Span) A collection of types that implement the [Widget] trait.

Macros§

borders
Macro to combine [Border] sides
help
Makes creating help easier
modifiers
Creates vector with given given Modifiers
paragraph
Creates new paragraph in more simple way

Enums§

Error