Top Related Projects
A Text User Interface library for the Rust programming language
Quick Overview
Termion is a pure Rust library that provides a set of utilities for interacting with the terminal, including support for cursor positioning, color manipulation, and event handling. It is designed to be a cross-platform solution, working on Windows, macOS, and Linux.
Pros
- Cross-platform compatibility: Termion is designed to work across multiple operating systems, making it a versatile choice for terminal-based applications.
- Comprehensive feature set: The library provides a wide range of functionality, including cursor manipulation, color support, and event handling, allowing developers to build powerful terminal-based applications.
- Rust-based: As a Rust library, Termion benefits from the language's safety, performance, and concurrency features, making it a reliable choice for building terminal-based applications.
- Active development: The project is actively maintained, with regular updates and improvements, ensuring that it keeps up with the latest developments in the Rust ecosystem.
Cons
- Learning curve: Developers new to Rust may need to invest time in learning the language and the Termion library, which could be a barrier to entry for some projects.
- Dependency on Rust: Since Termion is a Rust-based library, it may not be the best choice for projects that are not written in Rust or do not have the resources to adopt the Rust language.
- Limited documentation: While the project has good documentation, some users may find it lacking in certain areas, which could make it more challenging to get started with the library.
- Performance overhead: Depending on the complexity of the terminal-based application, the use of Termion may introduce some performance overhead, which could be a concern for certain use cases.
Code Examples
Here are a few code examples demonstrating the usage of Termion:
- Cursor positioning:
use termion::cursor;
fn main() {
print!("{}", cursor::Goto(5, 10)); // Move the cursor to column 5, row 10
println!("Hello, world!");
}
- Color manipulation:
use termion::color;
fn main() {
println!("{}This is red text.{}", color::Fg(color::Red), color::Fg(color::Reset));
println!("{}This is green text.{}", color::Fg(color::Green), color::Fg(color::Reset));
}
- Event handling:
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use std::io::{stdout, Write};
fn main() {
let mut stdout = stdout().into_raw_mode().unwrap();
write!(stdout, "{}", termion::clear::All).unwrap();
for key in stdin().keys() {
match key.unwrap() {
Key::Char('q') => break,
key => println!("You pressed {:?}", key),
}
}
}
- Terminal size detection:
use termion::terminal_size;
fn main() {
let (width, height) = terminal_size().unwrap();
println!("Terminal size: {} x {}", width, height);
}
Getting Started
To get started with Termion, you can add it as a dependency in your Cargo.toml file:
[dependencies]
termion = "1.5.6"
Then, you can start using the library in your Rust code. Here's a simple example that demonstrates how to clear the terminal and print some text:
use termion::clear;
use termion::cursor;
fn main() {
print!("{}{}", clear::All, cursor::Goto(1, 1));
println!("Hello, Termion!");
}
To run this example, save the code in a file (e.g., main.rs
) and compile it with cargo build
. Then, run the executable with cargo run
.
Competitor Comparisons
A Text User Interface library for the Rust programming language
Pros of Cursive
- Cursive provides a more comprehensive set of widgets and UI components, allowing for the creation of more complex and feature-rich terminal-based applications.
- The library has a well-documented API and a growing community, making it easier to find support and resources.
- Cursive's event handling and input processing are more robust, providing a more reliable and responsive user experience.
Cons of Cursive
- Cursive has a larger dependency footprint, which may be a concern for projects with strict resource constraints.
- The learning curve for Cursive may be steeper than Termion, as it has a more extensive API and feature set.
- Cursive's performance may be slightly lower than Termion's, especially for simple terminal-based applications.
Code Comparison
Termion:
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use std::io::{Write, stdout};
fn main() {
let mut stdout = stdout().into_raw_mode().unwrap();
write!(stdout, "{}", termion::cursor::Goto(10, 10)).unwrap();
stdout.flush().unwrap();
}
Cursive:
use cursive::Cursive;
use cursive::views::{Button, Dialog, TextView};
fn main() {
let mut siv = Cursive::default();
siv.add_layer(
Dialog::around(TextView::new("Hello, World!"))
.title("Cursive Example")
.button("Quit", |s| s.quit()),
);
siv.run();
}
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Documentation | Examples | Changelog | Tutorial |
---|
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 quite convenient, due to its complete coverage of essential TTY features, providing one consistent API. Termion is rather low-level containing only abstraction aligned with what actually happens behind the scenes. For something more high-level, refer to inquirer-rs, which uses Termion as backend.
Termion generates escapes and API calls for the user. This makes it a whole lot cleaner to use escapes.
Supports Redox, Mac OS X, BSD, and Linux (or, in general, ANSI terminals).
A note on stability
This crate is stable.
Cargo.toml
[dependencies]
termion = "*"
3.0.0 to 4.0.0 guide
A change is only necessary if you were matching on all variants of the MouseEvent
enum without a wildcard.
In this case, you need to either handle the two new variants, MouseLeft
and MouseRight
, or add a wildcard.
2.0.0 to 3.0.0 guide
Changes are only required if you were using IntoRawMode
on generic terminals W: Write
. Now, terminal
is also required to implement AsFd
trait. So replacing generic bounds with W: Write + AsFd
should be sufficient.
1.0.0 to 2.0.0 guide
1.0.0 | 2.0.0 |
---|---|
AlternativeScreen::from(x) | x.into_alternative_screen() |
0.1.0 to 1.0.0 guide
This sample table gives an idea of how to go about converting to the new major version of Termion.
0.1.0 | 1.0.0 |
---|---|
use termion::IntoRawMode | use termion::raw::IntoRawMode |
use termion::TermRead | use termion::input::TermRead |
stdout.color(color::Red); | write!(stdout, "{}", color::Fg(color::Red)); |
stdout.color_bg(color::Red); | write!(stdout, "{}", color::Bg(color::Red)); |
stdout.goto(x, y); | write!(stdout, "{}", cursor::Goto(x, y)); |
color::rgb(r, g, b); | color::Rgb(r, g, b) (truecolor) |
x.with_mouse() | MouseTerminal::from(x) |
Features
- Raw mode.
- TrueColor.
- 256-color mode.
- Cursor movement.
- Text formatting.
- Console size.
- TTY-only stream.
- Control sequences.
- Termios control.
- Password input.
- Redox support.
- Safe
isatty
wrapper. - Panic-free error handling.
- Special keys events (modifiers, special keys, etc.).
- Allocation-free.
- Asynchronous key events.
- Mouse input.
- Carefully tested.
- Detailed documentation on every item.
and much more.
Examples
Style and colors.
extern crate termion;
use termion::{color, style};
use std::io;
fn main() {
println!("{}Red", color::Fg(color::Red));
println!("{}Blue", color::Fg(color::Blue));
println!("{}Blue'n'Bold{}", style::Bold, style::Reset);
println!("{}Just plain italic", style::Italic);
}
Moving the cursor
extern crate termion;
fn main() {
print!("{}{}Stuff", termion::clear::All, termion::cursor::Goto(1, 1));
}
Mouse
extern crate termion;
use termion::event::{Key, Event, MouseEvent};
use termion::input::{TermRead, MouseTerminal};
use termion::raw::IntoRawMode;
use std::io::{Write, stdout, stdin};
fn main() {
let stdin = stdin();
let mut stdout = MouseTerminal::from(stdout().into_raw_mode().unwrap());
write!(stdout, "{}{}q to exit. Click, click, click!", termion::clear::All, termion::cursor::Goto(1, 1)).unwrap();
stdout.flush().unwrap();
for c in stdin.events() {
let evt = c.unwrap();
match evt {
Event::Key(Key::Char('q')) => break,
Event::Mouse(me) => {
match me {
MouseEvent::Press(_, x, y) => {
write!(stdout, "{}x", termion::cursor::Goto(x, y)).unwrap();
},
_ => (),
}
}
_ => {}
}
stdout.flush().unwrap();
}
}
Read a password
extern crate termion;
use termion::input::TermRead;
use std::io::{Write, stdout, stdin};
fn main() {
let stdout = stdout();
let mut stdout = stdout.lock();
let stdin = stdin();
let mut stdin = stdin.lock();
stdout.write_all(b"password: ").unwrap();
stdout.flush().unwrap();
let pass = stdin.read_passwd(&mut stdout);
if let Ok(Some(pass)) = pass {
stdout.write_all(pass.as_bytes()).unwrap();
stdout.write_all(b"\n").unwrap();
} else {
stdout.write_all(b"Error\n").unwrap();
}
}
Usage
See examples/
, and the documentation, which can be rendered using cargo doc
.
For a more complete example, see a minesweeper implementation, that I made for Redox using termion.
License
MIT/X11.
Top Related Projects
A Text User Interface library for the Rust programming language
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot