1 // Copyright 2013-2014 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.
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.
11 //! Terminal formatting library.
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 implementations,
15 //! the `TerminfoTerminal`, which uses control characters from a
16 //! [terminfo][ti] database, and `WinConsole`, which uses the [Win32 Console
22 //! extern crate term;
25 //! let mut t = term::stdout().unwrap();
27 //! t.fg(term::color::GREEN).unwrap();
28 //! (write!(t, "hello, ")).unwrap();
30 //! t.fg(term::color::RED).unwrap();
31 //! (writeln!(t, "world!")).unwrap();
33 //! t.reset().unwrap();
37 //! [ansi]: https://en.wikipedia.org/wiki/ANSI_escape_code
38 //! [win]: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682010%28v=vs.85%29.aspx
39 //! [ti]: https://en.wikipedia.org/wiki/Terminfo
41 #![crate_id = "term#0.11.0-pre"]
42 #![comment = "Simple ANSI color library"]
43 #![license = "MIT/ASL2"]
44 #![crate_type = "rlib"]
45 #![crate_type = "dylib"]
46 #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
47 html_favicon_url = "http://www.rust-lang.org/favicon.ico",
48 html_root_url = "http://doc.rust-lang.org/",
49 html_playground_url = "http://play.rust-lang.org/")]
51 #![feature(macro_rules, phase)]
55 #[cfg(stage0)] #[phase(syntax, link)] extern crate log;
56 #[cfg(not(stage0))] #[phase(plugin, link)] extern crate log;
58 pub use terminfo::TerminfoTerminal;
60 pub use win::WinConsole;
62 use std::io::IoResult;
70 /// Return a Terminal wrapping stdout, or None if a terminal couldn't be
72 pub fn stdout() -> Option<Box<Terminal<Box<Writer:Send>>:Send>> {
73 let ti: Option<TerminfoTerminal<Box<Writer:Send>>>
74 = Terminal::new(box std::io::stdout() as Box<Writer:Send>);
75 ti.map(|t| box t as Box<Terminal<Box<Writer:Send>:Send>:Send>)
79 /// Return a Terminal wrapping stdout, or None if a terminal couldn't be
81 pub fn stdout() -> Option<Box<Terminal<Box<Writer:Send>:Send>:Send>> {
82 let ti: Option<TerminfoTerminal<Box<Writer:Send>>>
83 = Terminal::new(box std::io::stdout() as Box<Writer:Send>);
86 Some(t) => Some(box t as Box<Terminal<Box<Writer:Send>:Send>:Send>),
88 let wc: Option<WinConsole<Box<Writer:Send>>>
89 = Terminal::new(box std::io::stdout() as Box<Writer:Send>);
90 wc.map(|w| box w as Box<Terminal<Box<Writer:Send>:Send>:Send>)
96 /// Return a Terminal wrapping stderr, or None if a terminal couldn't be
98 pub fn stderr() -> Option<Box<Terminal<Box<Writer:Send>:Send>:Send>:Send> {
99 let ti: Option<TerminfoTerminal<Box<Writer:Send>>>
100 = Terminal::new(box std::io::stderr() as Box<Writer:Send>);
101 ti.map(|t| box t as Box<Terminal<Box<Writer:Send>:Send>:Send>)
105 /// Return a Terminal wrapping stderr, or None if a terminal couldn't be
107 pub fn stderr() -> Option<Box<Terminal<Box<Writer:Send>:Send>:Send>> {
108 let ti: Option<TerminfoTerminal<Box<Writer:Send>>>
109 = Terminal::new(box std::io::stderr() as Box<Writer:Send>);
112 Some(t) => Some(box t as Box<Terminal<Box<Writer:Send>:Send>:Send>),
114 let wc: Option<WinConsole<Box<Writer:Send>>>
115 = Terminal::new(box std::io::stderr() as Box<Writer:Send>);
116 wc.map(|w| box w as Box<Terminal<Box<Writer:Send>:Send>:Send>)
122 /// Terminal color definitions
124 /// Number for a terminal color
125 pub type Color = u16;
127 pub static BLACK: Color = 0u16;
128 pub static RED: Color = 1u16;
129 pub static GREEN: Color = 2u16;
130 pub static YELLOW: Color = 3u16;
131 pub static BLUE: Color = 4u16;
132 pub static MAGENTA: Color = 5u16;
133 pub static CYAN: Color = 6u16;
134 pub static WHITE: Color = 7u16;
136 pub static BRIGHT_BLACK: Color = 8u16;
137 pub static BRIGHT_RED: Color = 9u16;
138 pub static BRIGHT_GREEN: Color = 10u16;
139 pub static BRIGHT_YELLOW: Color = 11u16;
140 pub static BRIGHT_BLUE: Color = 12u16;
141 pub static BRIGHT_MAGENTA: Color = 13u16;
142 pub static BRIGHT_CYAN: Color = 14u16;
143 pub static BRIGHT_WHITE: Color = 15u16;
146 /// Terminal attributes
148 /// Terminal attributes for use with term.attr().
150 /// Most attributes can only be turned on and must be turned off with term.reset().
151 /// The ones that can be turned off explicitly take a boolean value.
152 /// Color is also represented as an attribute for convenience.
154 /// Bold (or possibly bright) mode
156 /// Dim mode, also called faint or half-bright. Often not supported
158 /// Italics mode. Often not supported
164 /// Standout mode. Often implemented as Reverse, sometimes coupled with Bold
166 /// Reverse mode, inverts the foreground and background colors
168 /// Secure mode, also called invis mode. Hides the printed text
170 /// Convenience attribute to set the foreground color
171 ForegroundColor(super::color::Color),
172 /// Convenience attribute to set the background color
173 BackgroundColor(super::color::Color)
177 /// A terminal with similar capabilities to an ANSI Terminal
178 /// (foreground/background colors etc).
179 pub trait Terminal<T: Writer>: Writer {
180 /// Returns `None` whenever the terminal cannot be created for some
182 fn new(out: T) -> Option<Self>;
184 /// Sets the foreground color to the given color.
186 /// If the color is a bright color, but the terminal only supports 8 colors,
187 /// the corresponding normal color will be used instead.
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 fg(&mut self, color: color::Color) -> IoResult<bool>;
193 /// Sets the background color to the given color.
195 /// If the color is a bright color, but the terminal only supports 8 colors,
196 /// the corresponding normal color will be used instead.
198 /// Returns `Ok(true)` if the color was set, `Ok(false)` otherwise, and `Err(e)`
199 /// if there was an I/O error.
200 fn bg(&mut self, color: color::Color) -> IoResult<bool>;
202 /// Sets the given terminal attribute, if supported. Returns `Ok(true)`
203 /// if the attribute was supported, `Ok(false)` otherwise, and `Err(e)` if
204 /// there was an I/O error.
205 fn attr(&mut self, attr: attr::Attr) -> IoResult<bool>;
207 /// Returns whether the given terminal attribute is supported.
208 fn supports_attr(&self, attr: attr::Attr) -> bool;
210 /// Resets all terminal attributes and color to the default.
212 fn reset(&mut self) -> IoResult<()>;
214 /// Returns the contained stream, destroying the `Terminal`
215 fn unwrap(self) -> T;
217 /// Gets an immutable reference to the stream inside
218 fn get_ref<'a>(&'a self) -> &'a T;
220 /// Gets a mutable reference to the stream inside
221 fn get_mut<'a>(&'a mut self) -> &'a mut T;