Debian Codegraph @

debian/rust-vendor/console/src/lib.rs

raw
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")]
86 extern crate alloc;
87
88 #[cfg(feature = "alloc")]
89 pub use crate::kb::Key;
90 #[cfg(feature = "std")]
91 pub use crate::term::{
92 is_dumb, user_attended, user_attended_stderr, Term, TermFamily, TermFeatures, TermTarget,
93 };
94 #[cfg(feature = "std")]
95 pub 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, set_true_colors_enabled,
98 set_true_colors_enabled_stderr, style, true_colors_enabled, true_colors_enabled_stderr,
99 truncate_str, Alignment, Attribute, Color, Emoji, Style, StyledObject,
100 };
101
102 #[cfg(all(feature = "ansi-parsing", feature = "alloc"))]
103 pub use crate::ansi::strip_ansi_codes;
104 #[cfg(feature = "ansi-parsing")]
105 pub use crate::ansi::{AnsiCodeIterator, WithoutAnsi};
106
107 #[cfg(feature = "std")]
108 mod common_term;
109 #[cfg(feature = "alloc")]
110 mod kb;
111 #[cfg(feature = "std")]
112 mod term;
113 #[cfg(all(unix, not(target_arch = "wasm32"), feature = "std"))]
114 mod unix_term;
115 #[cfg(feature = "std")]
116 mod utils;
117 #[cfg(all(feature = "std", target_arch = "wasm32"))]
118 mod wasm_term;
119 #[cfg(all(feature = "std", windows))]
120 mod windows_term;
121
122 #[cfg(feature = "ansi-parsing")]
123 mod ansi;