term/lib.rs
1// Copyright 2013-2019 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! Terminal formatting library.
12//!
13//! This crate provides the `Terminal` trait, which abstracts over an [ANSI
14//! Terminal][ansi] to provide color printing, among other things. There are two
15//! implementations, the `TerminfoTerminal`, which uses control characters from
16//! a [terminfo][ti] database, and `WinConsole`, which uses the [Win32 Console
17//! API][win].
18//!
19//! # Usage
20//!
21//! This crate is [on crates.io](https://crates.io/crates/term) and can be
22//! used by adding `term` to the dependencies in your project's `Cargo.toml`.
23//!
24//! ```toml
25//! [dependencies]
26//!
27//! term = "*"
28//! ```
29//!
30//! # Examples
31//!
32//! ```no_run
33//! use std::io::prelude::*;
34//!
35//! let mut t = term::stdout().unwrap();
36//!
37//! t.fg(term::color::GREEN).unwrap();
38//! write!(t, "hello, ").unwrap();
39//!
40//! t.fg(term::color::RED).unwrap();
41//! writeln!(t, "world!").unwrap();
42//!
43//! t.reset().unwrap();
44//! ```
45//!
46//! [ansi]: https://en.wikipedia.org/wiki/ANSI_escape_code
47//! [win]: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682010%28v=vs.85%29.aspx
48//! [ti]: https://en.wikipedia.org/wiki/Terminfo
49
50#![doc(
51 html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
52 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
53 html_root_url = "https://docs.rs/term/latest",
54 test(attr(deny(warnings)))
55)]
56#![deny(missing_docs)]
57#![deny(rust_2018_idioms)]
58#![cfg_attr(test, deny(warnings))]
59
60use std::io::prelude::*;
61
62pub use crate::terminfo::TerminfoTerminal;
63#[cfg(windows)]
64pub use win::{WinConsole, WinConsoleInfo};
65
66use std::io::{self, Stderr, Stdout};
67
68pub mod terminfo;
69
70#[cfg(windows)]
71mod win;
72
73/// Alias for stdout terminals.
74pub type StdoutTerminal = dyn Terminal<Output = Stdout> + Send;
75/// Alias for stderr terminals.
76pub type StderrTerminal = dyn Terminal<Output = Stderr> + Send;
77
78#[cfg(not(windows))]
79/// Return a Terminal wrapping stdout, or None if a terminal couldn't be
80/// opened.
81pub fn stdout() -> Option<Box<StdoutTerminal>> {
82 TerminfoTerminal::new(io::stdout()).map(|t| Box::new(t) as Box<StdoutTerminal>)
83}
84
85#[cfg(windows)]
86/// Return a Terminal wrapping stdout, or None if a terminal couldn't be
87/// opened.
88pub fn stdout() -> Option<Box<StdoutTerminal>> {
89 TerminfoTerminal::new(io::stdout())
90 .map(|t| Box::new(t) as Box<StdoutTerminal>)
91 .or_else(|| {
92 WinConsole::new(io::stdout())
93 .ok()
94 .map(|t| Box::new(t) as Box<StdoutTerminal>)
95 })
96}
97
98#[cfg(not(windows))]
99/// Return a Terminal wrapping stderr, or None if a terminal couldn't be
100/// opened.
101pub fn stderr() -> Option<Box<StderrTerminal>> {
102 TerminfoTerminal::new(io::stderr()).map(|t| Box::new(t) as Box<StderrTerminal>)
103}
104
105#[cfg(windows)]
106/// Return a Terminal wrapping stderr, or None if a terminal couldn't be
107/// opened.
108pub fn stderr() -> Option<Box<StderrTerminal>> {
109 TerminfoTerminal::new(io::stderr())
110 .map(|t| Box::new(t) as Box<StderrTerminal>)
111 .or_else(|| {
112 WinConsole::new(io::stderr())
113 .ok()
114 .map(|t| Box::new(t) as Box<StderrTerminal>)
115 })
116}
117
118/// Terminal color definitions
119#[allow(missing_docs)]
120pub mod color {
121 /// Number for a terminal color
122 pub type Color = u32;
123
124 pub const BLACK: Color = 0;
125 pub const RED: Color = 1;
126 pub const GREEN: Color = 2;
127 pub const YELLOW: Color = 3;
128 pub const BLUE: Color = 4;
129 pub const MAGENTA: Color = 5;
130 pub const CYAN: Color = 6;
131 pub const WHITE: Color = 7;
132
133 pub const BRIGHT_BLACK: Color = 8;
134 pub const BRIGHT_RED: Color = 9;
135 pub const BRIGHT_GREEN: Color = 10;
136 pub const BRIGHT_YELLOW: Color = 11;
137 pub const BRIGHT_BLUE: Color = 12;
138 pub const BRIGHT_MAGENTA: Color = 13;
139 pub const BRIGHT_CYAN: Color = 14;
140 pub const BRIGHT_WHITE: Color = 15;
141}
142
143/// Terminal attributes for use with term.attr().
144///
145/// Most attributes can only be turned on and must be turned off with term.reset().
146/// The ones that can be turned off explicitly take a boolean value.
147/// Color is also represented as an attribute for convenience.
148#[derive(Debug, PartialEq, Hash, Eq, Copy, Clone)]
149pub enum Attr {
150 /// Bold (or possibly bright) mode
151 Bold,
152 /// Dim mode, also called faint or half-bright. Often not supported
153 Dim,
154 /// Italics mode. Often not supported
155 Italic(bool),
156 /// Underline mode
157 Underline(bool),
158 /// Blink mode
159 Blink,
160 /// Standout mode. Often implemented as Reverse, sometimes coupled with Bold
161 Standout(bool),
162 /// Reverse mode, inverts the foreground and background colors
163 Reverse,
164 /// Secure mode, also called invis mode. Hides the printed text
165 Secure,
166 /// Convenience attribute to set the foreground color
167 ForegroundColor(color::Color),
168 /// Convenience attribute to set the background color
169 BackgroundColor(color::Color),
170}
171
172/// An error arising from interacting with the terminal.
173#[derive(Debug)]
174#[non_exhaustive]
175pub enum Error {
176 /// Indicates an error from any underlying IO
177 Io(io::Error),
178 /// Indicates an error during terminfo parsing
179 TerminfoParsing(terminfo::Error),
180 /// Indicates an error expanding a parameterized string from the terminfo database
181 ParameterizedExpansion(terminfo::parm::Error),
182 /// Indicates that the terminal does not support the requested operation.
183 NotSupported,
184 /// Indicates that the `TERM` environment variable was unset, and thus we were unable to detect
185 /// which terminal we should be using.
186 TermUnset,
187 /// Indicates that we were unable to find a terminfo entry for the requested terminal.
188 TerminfoEntryNotFound,
189 /// Indicates that the cursor could not be moved to the requested position.
190 CursorDestinationInvalid,
191 /// Indicates that the terminal does not support displaying the requested color.
192 ///
193 /// This is like `NotSupported`, but more specific.
194 ColorOutOfRange,
195}
196
197// manually implemented because std::io::Error does not implement Eq/PartialEq
198impl std::cmp::PartialEq for Error {
199 fn eq(&self, other: &Error) -> bool {
200 use crate::Error::*;
201 match self {
202 Io(_) => false,
203 TerminfoParsing(a) => matches!(other, TerminfoParsing(b) if a == b),
204 ParameterizedExpansion(a) => matches!(other, ParameterizedExpansion(b) if a == b),
205 NotSupported => matches!(other, NotSupported),
206 TermUnset => matches!(other, TermUnset),
207 TerminfoEntryNotFound => matches!(other, TerminfoEntryNotFound),
208 CursorDestinationInvalid => matches!(other, CursorDestinationInvalid),
209 ColorOutOfRange => matches!(other, ColorOutOfRange),
210 }
211 }
212}
213
214/// The canonical `Result` type using this crate's Error type.
215pub type Result<T> = std::result::Result<T, Error>;
216
217impl std::fmt::Display for Error {
218 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
219 use crate::Error::*;
220 match self {
221 Io(io) => io.fmt(f),
222 TerminfoParsing(e) => e.fmt(f),
223 ParameterizedExpansion(e) => e.fmt(f),
224 NotSupported => f.write_str("operation not supported by the terminal"),
225 TermUnset => {
226 f.write_str("TERM environment variable unset, unable to detect a terminal")
227 }
228 TerminfoEntryNotFound => {
229 f.write_str("could not find a terminfo entry for this terminal")
230 }
231 CursorDestinationInvalid => f.write_str("could not move cursor to requested position"),
232 ColorOutOfRange => f.write_str("color not supported by the terminal"),
233 }
234 }
235}
236
237impl std::error::Error for Error {
238 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
239 match self {
240 Error::Io(io) => Some(io),
241 Error::TerminfoParsing(e) => Some(e),
242 Error::ParameterizedExpansion(e) => Some(e),
243 _ => None,
244 }
245 }
246}
247
248impl From<Error> for io::Error {
249 fn from(err: Error) -> io::Error {
250 let kind = match &err {
251 Error::Io(e) => e.kind(),
252 _ => io::ErrorKind::Other,
253 };
254 io::Error::new(kind, err)
255 }
256}
257
258impl std::convert::From<io::Error> for Error {
259 fn from(val: io::Error) -> Self {
260 Error::Io(val)
261 }
262}
263
264impl std::convert::From<terminfo::Error> for Error {
265 fn from(val: terminfo::Error) -> Self {
266 Error::TerminfoParsing(val)
267 }
268}
269
270impl std::convert::From<terminfo::parm::Error> for Error {
271 fn from(val: terminfo::parm::Error) -> Self {
272 Error::ParameterizedExpansion(val)
273 }
274}
275
276/// A terminal with similar capabilities to an ANSI Terminal
277/// (foreground/background colors etc).
278pub trait Terminal: Write {
279 /// The terminal's output writer type.
280 type Output: Write;
281
282 /// Sets the foreground color to the given color.
283 ///
284 /// If the color is a bright color, but the terminal only supports 8 colors,
285 /// the corresponding normal color will be used instead.
286 ///
287 /// Returns `Ok(())` if the color change code was sent to the terminal, or `Err(e)` if there
288 /// was an error.
289 fn fg(&mut self, color: color::Color) -> Result<()>;
290
291 /// Sets the background color to the given color.
292 ///
293 /// If the color is a bright color, but the terminal only supports 8 colors,
294 /// the corresponding normal color will be used instead.
295 ///
296 /// Returns `Ok(())` if the color change code was sent to the terminal, or `Err(e)` if there
297 /// was an error.
298 fn bg(&mut self, color: color::Color) -> Result<()>;
299
300 /// Sets the given terminal attribute, if supported. Returns `Ok(())` if the attribute is
301 /// supported and was sent to the terminal, or `Err(e)` if there was an error or the attribute
302 /// wasn't supported.
303 fn attr(&mut self, attr: Attr) -> Result<()>;
304
305 /// Returns whether the given terminal attribute is supported.
306 fn supports_attr(&self, attr: Attr) -> bool;
307
308 /// Resets all terminal attributes and colors to their defaults.
309 ///
310 /// Returns `Ok(())` if the reset code was printed, or `Err(e)` if there was an error.
311 ///
312 /// *Note: This does not flush.*
313 ///
314 /// That means the reset command may get buffered so, if you aren't planning on doing anything
315 /// else that might flush stdout's buffer (e.g. writing a line of text), you should flush after
316 /// calling reset.
317 fn reset(&mut self) -> Result<()>;
318
319 /// Returns true if reset is supported.
320 fn supports_reset(&self) -> bool;
321
322 /// Returns true if color is fully supported.
323 ///
324 /// If this function returns `true`, `bg`, `fg`, and `reset` will never
325 /// return `Err(Error::NotSupported)`.
326 fn supports_color(&self) -> bool;
327
328 /// Moves the cursor up one line.
329 ///
330 /// Returns `Ok(())` if the cursor movement code was printed, or `Err(e)` if there was an
331 /// error.
332 fn cursor_up(&mut self) -> Result<()>;
333
334 /// Deletes the text from the cursor location to the end of the line.
335 ///
336 /// Returns `Ok(())` if the deletion code was printed, or `Err(e)` if there was an error.
337 fn delete_line(&mut self) -> Result<()>;
338
339 /// Moves the cursor to the left edge of the current line.
340 ///
341 /// Returns `Ok(true)` if the deletion code was printed, or `Err(e)` if there was an error.
342 fn carriage_return(&mut self) -> Result<()>;
343
344 /// Gets an immutable reference to the stream inside
345 fn get_ref(&self) -> &Self::Output;
346
347 /// Gets a mutable reference to the stream inside
348 fn get_mut(&mut self) -> &mut Self::Output;
349
350 /// Returns the contained stream, destroying the `Terminal`
351 fn into_inner(self) -> Self::Output
352 where
353 Self: Sized;
354}