]> git.proxmox.com Git - rustc.git/blob - src/libterm/lib.rs
New upstream version 1.16.0+dfsg1
[rustc.git] / src / libterm / lib.rs
1 // Copyright 2013-2015 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 //! # Examples
20 //!
21 //! ```no_run
22 //! # #![feature(rustc_private)]
23 //! extern crate term;
24 //! use std::io::prelude::*;
25 //!
26 //! fn main() {
27 //! let mut t = term::stdout().unwrap();
28 //!
29 //! t.fg(term::color::GREEN).unwrap();
30 //! write!(t, "hello, ").unwrap();
31 //!
32 //! t.fg(term::color::RED).unwrap();
33 //! writeln!(t, "world!").unwrap();
34 //!
35 //! assert!(t.reset().unwrap());
36 //! }
37 //! ```
38 //!
39 //! [ansi]: https://en.wikipedia.org/wiki/ANSI_escape_code
40 //! [win]: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682010%28v=vs.85%29.aspx
41 //! [ti]: https://en.wikipedia.org/wiki/Terminfo
42
43 #![crate_name = "term"]
44 #![unstable(feature = "rustc_private",
45 reason = "use the crates.io `term` library instead",
46 issue = "27812")]
47 #![crate_type = "rlib"]
48 #![crate_type = "dylib"]
49 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
50 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
51 html_root_url = "https://doc.rust-lang.org/nightly/",
52 html_playground_url = "https://play.rust-lang.org/",
53 test(attr(deny(warnings))))]
54 #![deny(missing_docs)]
55 #![deny(warnings)]
56
57 #![feature(box_syntax)]
58 #![feature(staged_api)]
59 #![cfg_attr(windows, feature(libc))]
60 // Handle rustfmt skips
61 #![feature(custom_attribute)]
62 #![allow(unused_attributes)]
63
64 use std::io::prelude::*;
65
66 pub use terminfo::TerminfoTerminal;
67 #[cfg(windows)]
68 pub use win::WinConsole;
69
70 use std::io::{self, Stdout, Stderr};
71
72 pub mod terminfo;
73
74 #[cfg(windows)]
75 mod win;
76
77 /// Alias for stdout terminals.
78 pub type StdoutTerminal = Terminal<Output = Stdout> + Send;
79 /// Alias for stderr terminals.
80 pub type StderrTerminal = Terminal<Output = Stderr> + Send;
81
82 #[cfg(not(windows))]
83 /// Return a Terminal wrapping stdout, or None if a terminal couldn't be
84 /// opened.
85 pub fn stdout() -> Option<Box<StdoutTerminal>> {
86 TerminfoTerminal::new(io::stdout()).map(|t| Box::new(t) as Box<StdoutTerminal>)
87 }
88
89 #[cfg(windows)]
90 /// Return a Terminal wrapping stdout, or None if a terminal couldn't be
91 /// opened.
92 pub fn stdout() -> Option<Box<StdoutTerminal>> {
93 TerminfoTerminal::new(io::stdout())
94 .map(|t| Box::new(t) as Box<StdoutTerminal>)
95 .or_else(|| WinConsole::new(io::stdout()).ok().map(|t| Box::new(t) as Box<StdoutTerminal>))
96 }
97
98 #[cfg(not(windows))]
99 /// Return a Terminal wrapping stderr, or None if a terminal couldn't be
100 /// opened.
101 pub 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.
108 pub fn stderr() -> Option<Box<StderrTerminal>> {
109 TerminfoTerminal::new(io::stderr())
110 .map(|t| Box::new(t) as Box<StderrTerminal>)
111 .or_else(|| WinConsole::new(io::stderr()).ok().map(|t| Box::new(t) as Box<StderrTerminal>))
112 }
113
114
115 /// Terminal color definitions
116 #[allow(missing_docs)]
117 pub mod color {
118 /// Number for a terminal color
119 pub type Color = u16;
120
121 pub const BLACK: Color = 0;
122 pub const RED: Color = 1;
123 pub const GREEN: Color = 2;
124 pub const YELLOW: Color = 3;
125 pub const BLUE: Color = 4;
126 pub const MAGENTA: Color = 5;
127 pub const CYAN: Color = 6;
128 pub const WHITE: Color = 7;
129
130 pub const BRIGHT_BLACK: Color = 8;
131 pub const BRIGHT_RED: Color = 9;
132 pub const BRIGHT_GREEN: Color = 10;
133 pub const BRIGHT_YELLOW: Color = 11;
134 pub const BRIGHT_BLUE: Color = 12;
135 pub const BRIGHT_MAGENTA: Color = 13;
136 pub const BRIGHT_CYAN: Color = 14;
137 pub const BRIGHT_WHITE: Color = 15;
138 }
139
140 /// Terminal attributes for use with term.attr().
141 ///
142 /// Most attributes can only be turned on and must be turned off with term.reset().
143 /// The ones that can be turned off explicitly take a boolean value.
144 /// Color is also represented as an attribute for convenience.
145 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
146 pub enum Attr {
147 /// Bold (or possibly bright) mode
148 Bold,
149 /// Dim mode, also called faint or half-bright. Often not supported
150 Dim,
151 /// Italics mode. Often not supported
152 Italic(bool),
153 /// Underline mode
154 Underline(bool),
155 /// Blink mode
156 Blink,
157 /// Standout mode. Often implemented as Reverse, sometimes coupled with Bold
158 Standout(bool),
159 /// Reverse mode, inverts the foreground and background colors
160 Reverse,
161 /// Secure mode, also called invis mode. Hides the printed text
162 Secure,
163 /// Convenience attribute to set the foreground color
164 ForegroundColor(color::Color),
165 /// Convenience attribute to set the background color
166 BackgroundColor(color::Color),
167 }
168
169 /// A terminal with similar capabilities to an ANSI Terminal
170 /// (foreground/background colors etc).
171 pub trait Terminal: Write {
172 /// The terminal's output writer type.
173 type Output: Write;
174
175 /// Sets the foreground color to the given color.
176 ///
177 /// If the color is a bright color, but the terminal only supports 8 colors,
178 /// the corresponding normal color will be used instead.
179 ///
180 /// Returns `Ok(true)` if the color was set, `Ok(false)` otherwise, and `Err(e)`
181 /// if there was an I/O error.
182 fn fg(&mut self, color: color::Color) -> io::Result<bool>;
183
184 /// Sets the background color to the given color.
185 ///
186 /// If the color is a bright color, but the terminal only supports 8 colors,
187 /// the corresponding normal color will be used instead.
188 ///
189 /// Returns `Ok(true)` if the color was set, `Ok(false)` otherwise, and `Err(e)`
190 /// if there was an I/O error.
191 fn bg(&mut self, color: color::Color) -> io::Result<bool>;
192
193 /// Sets the given terminal attribute, if supported. Returns `Ok(true)`
194 /// if the attribute was supported, `Ok(false)` otherwise, and `Err(e)` if
195 /// there was an I/O error.
196 fn attr(&mut self, attr: Attr) -> io::Result<bool>;
197
198 /// Returns whether the given terminal attribute is supported.
199 fn supports_attr(&self, attr: Attr) -> bool;
200
201 /// Resets all terminal attributes and colors to their defaults.
202 ///
203 /// Returns `Ok(true)` if the terminal was reset, `Ok(false)` otherwise, and `Err(e)` if there
204 /// was an I/O error.
205 ///
206 /// *Note: This does not flush.*
207 ///
208 /// That means the reset command may get buffered so, if you aren't planning on doing anything
209 /// else that might flush stdout's buffer (e.g. writing a line of text), you should flush after
210 /// calling reset.
211 fn reset(&mut self) -> io::Result<bool>;
212
213 /// Gets an immutable reference to the stream inside
214 fn get_ref(&self) -> &Self::Output;
215
216 /// Gets a mutable reference to the stream inside
217 fn get_mut(&mut self) -> &mut Self::Output;
218
219 /// Returns the contained stream, destroying the `Terminal`
220 fn into_inner(self) -> Self::Output where Self: Sized;
221 }