Top Related Projects
Build terminal user interfaces and dashboards using Rust
Cross platform terminal library rust
A Text User Interface library for the Rust programming language
Mirror of https://gitlab.redox-os.org/redox-os/termion
Rust utility library for nice command line prompts and similar things
Quick Overview
Console-rs is a Rust library that provides cross-platform utilities for working with terminals and console applications. It offers features such as colored output, cursor manipulation, and terminal size detection, making it easier to create rich command-line interfaces in Rust.
Pros
- Cross-platform compatibility (Windows, macOS, Linux)
- Easy-to-use API for terminal manipulation and styling
- Supports ANSI colors and styles
- Provides terminal size detection and cursor movement
Cons
- Limited support for complex terminal layouts
- May have performance overhead for very large output
- Requires runtime checks for terminal capabilities
- Some advanced features might not work in all terminal emulators
Code Examples
- Colored output:
use console::style;
println!("This is {}!", style("colored").red());
println!("This is {}!", style("bold").bold());
- Terminal size detection:
use console::Term;
let term = Term::stdout();
let (width, height) = term.size();
println!("Terminal size: {}x{}", width, height);
- Cursor manipulation:
use console::Term;
let term = Term::stdout();
term.clear_screen()?;
term.move_cursor_to(10, 5)?;
println!("This text appears at row 5, column 10");
- Progress bar:
use console::Term;
use std::{thread, time};
let term = Term::stdout();
for i in 0..=100 {
term.clear_line()?;
term.write_str(&format!("Progress: {}%", i))?;
thread::sleep(time::Duration::from_millis(50));
}
Getting Started
To use console-rs in your Rust project, add the following to your Cargo.toml
:
[dependencies]
console = "0.15.5"
Then, in your Rust code, you can import and use the library like this:
use console::{style, Term};
fn main() -> std::io::Result<()> {
let term = Term::stdout();
term.write_line(&format!("Hello, {}!", style("world").cyan()))?;
Ok(())
}
This example prints "Hello, world!" with the word "world" in cyan color.
Competitor Comparisons
Build terminal user interfaces and dashboards using Rust
Pros of tui-rs
- More comprehensive TUI framework with advanced widgets and layouts
- Better suited for complex, interactive terminal applications
- Supports custom rendering and event handling for greater flexibility
Cons of tui-rs
- Steeper learning curve due to its more complex API
- Requires more boilerplate code for simple tasks
- Less suitable for quick, one-off terminal output formatting
Code Comparison
tui-rs example:
let app = App::new("Counter App");
Terminal::new(CrosstermBackend::new(stdout()))?
.draw(|f| {
let size = f.size();
f.render_widget(app, size);
})?;
console example:
use console::style;
println!("{}", style("Hello").red().bold());
Summary
tui-rs is a more powerful framework for building complex terminal user interfaces, offering advanced widgets and custom rendering. However, it comes with a steeper learning curve and requires more code for simple tasks. console, on the other hand, is simpler and more straightforward for basic terminal output formatting but lacks the advanced features of tui-rs for building full-fledged TUI applications.
Cross platform terminal library rust
Pros of crossterm
- More comprehensive terminal manipulation capabilities, including cursor movement, raw mode, and event handling
- Cross-platform support for Windows, macOS, and Linux without external dependencies
- Active development with frequent updates and improvements
Cons of crossterm
- Steeper learning curve due to more complex API
- Potentially overkill for simple console output tasks
- Slightly larger codebase and dependency footprint
Code comparison
crossterm:
use crossterm::{
execute,
style::{Color, Print, ResetColor, SetForegroundColor},
};
fn main() -> crossterm::Result<()> {
execute!(
stdout(),
SetForegroundColor(Color::Red),
Print("Hello, World!"),
ResetColor
)
}
console:
use console::style;
fn main() {
println!("{}", style("Hello, World!").red());
}
Summary
crossterm offers more advanced terminal manipulation features and cross-platform support, making it suitable for complex console applications. console provides a simpler API for basic styling and output, which may be preferable for straightforward tasks. The choice between the two depends on the specific requirements of your project and the level of terminal control needed.
A Text User Interface library for the Rust programming language
Pros of Cursive
- More comprehensive TUI framework with widgets and layouts
- Event-driven architecture for interactive applications
- Supports multiple backends (ncurses, termion, crossterm)
Cons of Cursive
- Steeper learning curve due to more complex API
- Heavier dependency footprint
- May be overkill for simple console output tasks
Code Comparison
Cursive example:
siv.add_layer(
Dialog::around(
EditView::new()
.on_submit(|s, name| {
s.pop_layer();
s.add_layer(Dialog::text(format!("Hello {}!", name)));
})
.fixed_width(20),
)
.title("Enter your name"),
);
Console example:
let name = Text::new("John Doe").red().on_blue();
println!("{}", name);
Summary
Cursive is a full-featured TUI framework suitable for complex interactive applications, while Console focuses on simpler console output styling and formatting. Cursive offers more power and flexibility but comes with increased complexity, whereas Console provides a straightforward API for basic console manipulation tasks.
Mirror of https://gitlab.redox-os.org/redox-os/termion
Pros of termion
- More low-level control over terminal operations
- Supports a wider range of Unix-like systems, including Redox OS
- Provides raw mode functionality for direct input handling
Cons of termion
- Less user-friendly API, requiring more boilerplate code
- Limited Windows support compared to console
- Fewer high-level abstractions for common terminal operations
Code Comparison
termion example:
use termion::{color, style};
println!("{}{}Hello, World!{}", color::Fg(color::Red), style::Bold, style::Reset);
console example:
use console::style;
println!("{}", style("Hello, World!").red().bold());
Summary
termion offers more granular control over terminal operations and supports a broader range of Unix-like systems, including Redox OS. However, it has a steeper learning curve and requires more boilerplate code. console provides a more user-friendly API with higher-level abstractions, making it easier to use for common terminal operations, but it may offer less flexibility for advanced use cases. The choice between the two depends on the specific requirements of your project and the level of control you need over terminal functionality.
Rust utility library for nice command line prompts and similar things
Pros of dialoguer
- Focused on interactive command-line prompts and user input
- Provides a higher-level API for complex input scenarios
- Offers more sophisticated input validation and confirmation dialogs
Cons of dialoguer
- More specialized, less general-purpose than console
- Potentially steeper learning curve for simple use cases
- May have more dependencies and a larger footprint
Code comparison
console:
use console::Term;
let term = Term::stdout();
term.write_line("Hello, world!")?;
term.read_line()?;
dialoguer:
use dialoguer::{Input, Confirm};
let name = Input::<String>::new()
.with_prompt("Enter your name")
.interact_text()?;
let confirmed = Confirm::new().with_prompt("Are you sure?").interact()?;
Summary
While console provides a general-purpose terminal manipulation library, dialoguer focuses on creating interactive command-line interfaces. Console offers lower-level control over terminal output and input, whereas dialoguer simplifies the process of gathering user input through various prompt types. The choice between the two depends on the specific needs of your project, with console being more suitable for broad terminal control and dialoguer excelling in user interaction scenarios.
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
console
console is a library for Rust that provides access to various terminal features so you can build nicer looking command line interfaces. It comes with various tools and utilities for working with Terminals and formatting text.
Best paired with other libraries in the family:
Terminal Access
The terminal is abstracted through the console::Term
type. It can
either directly provide access to the connected terminal or by buffering
up commands. A buffered terminal will however not be completely buffered
on windows where cursor movements are currently directly passed through.
Example usage:
use std::thread;
use std::time::Duration;
use console::Term;
let term = Term::stdout();
term.write_line("Hello World!")?;
thread::sleep(Duration::from_millis(2000));
term.clear_line()?;
Colors and Styles
console
automatically detects when to use colors based on the tty flag. It also
provides higher level wrappers for styling text and other things that can be
displayed with the style
function and utility types.
Example usage:
use console::style;
println!("This is {} neat", style("quite").cyan());
You can also store styles and apply them to text later:
use console::Style;
let cyan = Style::new().cyan();
println!("This is {} neat", cyan.apply_to("quite"));
Working with ANSI Codes
The crate provides the function strip_ansi_codes
to remove ANSI codes
from a string as well as measure_text_width
to calculate the width of a
string as it would be displayed by the terminal. Both of those together
are useful for more complex formatting.
Unicode Width Support
By default this crate depends on the unicode-width
crate to calculate
the width of terminal characters. If you do not need this you can disable
the unicode-width
feature which will cut down on dependencies.
License: MIT
Top Related Projects
Build terminal user interfaces and dashboards using Rust
Cross platform terminal library rust
A Text User Interface library for the Rust programming language
Mirror of https://gitlab.redox-os.org/redox-os/termion
Rust utility library for nice command line prompts and similar things
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