[go: up one dir, main page]

console/
lib.rs

1//! console is a library for Rust that provides access to various terminal
2//! features so you can build nicer looking command line interfaces.  It
3//! comes with various tools and utilities for working with Terminals and
4//! formatting text.
5//!
6//! Best paired with other libraries in the family:
7//!
8//! * [dialoguer](https://docs.rs/dialoguer)
9//! * [indicatif](https://docs.rs/indicatif)
10//!
11//! # Terminal Access
12//!
13//! The terminal is abstracted through the `console::Term` type.  It can
14//! either directly provide access to the connected terminal or by buffering
15//! up commands.  A buffered terminal will however not be completely buffered
16//! on windows where cursor movements are currently directly passed through.
17//!
18//! Example usage:
19//!
20//! ```
21//! # fn test() -> Result<(), Box<dyn std::error::Error>> {
22//! use std::thread;
23//! use std::time::Duration;
24//!
25//! use console::Term;
26//!
27//! let term = Term::stdout();
28//! term.write_line("Hello World!")?;
29//! thread::sleep(Duration::from_millis(2000));
30//! term.clear_line()?;
31//! # Ok(()) } test().unwrap();
32//! ```
33//!
34//! # Colors and Styles
35//!
36//! `console` automatically detects when to use colors based on the tty flag.  It also
37//! provides higher level wrappers for styling text and other things that can be
38//! displayed with the `style` function and utility types.
39//!
40//! Example usage:
41//!
42//! ```
43//! use console::style;
44//!
45//! println!("This is {} neat", style("quite").cyan());
46//! ```
47//!
48//! You can also store styles and apply them to text later:
49//!
50//! ```
51//! use console::Style;
52//!
53//! let cyan = Style::new().cyan();
54//! println!("This is {} neat", cyan.apply_to("quite"));
55//! ```
56//!
57//! # Working with ANSI Codes
58//!
59//! The crate provides the function `strip_ansi_codes` to remove ANSI codes
60//! from a string as well as `measure_text_width` to calculate the width of a
61//! string as it would be displayed by the terminal.  Both of those together
62//! are useful for more complex formatting.
63//!
64//! # Unicode Width Support
65//!
66//! By default this crate depends on the `unicode-width` crate to calculate
67//! the width of terminal characters.  If you do not need this you can disable
68//! the `unicode-width` feature which will cut down on dependencies.
69//!
70//! # Features
71//!
72//! By default all features are enabled.  The following features exist:
73//!
74//! * `unicode-width`: adds support for unicode width calculations
75//! * `ansi-parsing`: adds support for parsing ansi codes (this adds support
76//!   for stripping and taking ansi escape codes into account for length
77//!   calculations).
78
79#![warn(
80    unreachable_pub,
81    clippy::std_instead_of_core,
82    clippy::std_instead_of_alloc
83)]
84#![cfg_attr(not(feature = "std"), no_std)]
85#[cfg(feature = "alloc")]
86extern crate alloc;
87
88#[cfg(feature = "alloc")]
89pub use crate::kb::Key;
90#[cfg(feature = "std")]
91pub use crate::term::{
92    user_attended, user_attended_stderr, Term, TermFamily, TermFeatures, TermTarget,
93};
94#[cfg(feature = "std")]
95pub use crate::utils::{
96    colors_enabled, colors_enabled_stderr, measure_text_width, pad_str, pad_str_with,
97    set_colors_enabled, set_colors_enabled_stderr, style, truncate_str, Alignment, Attribute,
98    Color, Emoji, Style, StyledObject,
99};
100
101#[cfg(all(feature = "ansi-parsing", feature = "alloc"))]
102pub use crate::ansi::strip_ansi_codes;
103#[cfg(feature = "ansi-parsing")]
104pub use crate::ansi::{AnsiCodeIterator, WithoutAnsi};
105
106#[cfg(feature = "std")]
107mod common_term;
108#[cfg(feature = "alloc")]
109mod kb;
110#[cfg(feature = "std")]
111mod term;
112#[cfg(all(unix, not(target_arch = "wasm32"), feature = "std"))]
113mod unix_term;
114#[cfg(feature = "std")]
115mod utils;
116#[cfg(all(feature = "std", target_arch = "wasm32"))]
117mod wasm_term;
118#[cfg(all(feature = "std", windows))]
119mod windows_term;
120
121#[cfg(feature = "ansi-parsing")]
122mod ansi;