Switch to 'yansi' for all terminal coloring.

Resolves #299.
This commit is contained in:
Sergio Benitez 2017-06-01 21:44:31 -07:00
parent 73fed03ef4
commit a6c4d053ad
13 changed files with 53 additions and 62 deletions

View File

@ -22,5 +22,5 @@ log = "^0.3"
compiletest_rs = "^0.2"
[build-dependencies]
ansi_term = "0.9"
yansi = "0.2"
version_check = "0.1"

View File

@ -1,10 +1,10 @@
//! This tiny build script ensures that rocket_codegen is not compiled with an
//! incompatible version of rust.
extern crate ansi_term;
extern crate yansi;
extern crate version_check;
use ansi_term::Color::{Red, Yellow, Blue, White};
use yansi::Color::{Red, Yellow, Blue, White};
use version_check::{is_nightly, is_min_version, is_min_date};
// Specifies the minimum nightly version needed to compile Rocket's codegen.
@ -37,7 +37,7 @@ fn main() {
(Some(is_nightly), Some((ok_version, version)), Some((ok_date, date))) => {
if !is_nightly {
printerr!("{} {}",
Red.bold().paint("Error:"),
Red.paint("Error:").bold(),
White.paint("Rocket requires a nightly version of Rust."));
print_version_err(&*version, &*date);
printerr!("{}{}{}",
@ -49,7 +49,7 @@ fn main() {
if !ok_version || !ok_date {
printerr!("{} {}",
Red.bold().paint("Error:"),
Red.paint("Error:").bold(),
White.paint("Rocket codegen requires a more recent version of rustc."));
printerr!("{}{}{}",
Blue.paint("Use `"),

View File

@ -18,7 +18,7 @@ categories = ["web-programming::http-server"]
tls = ["rustls", "hyper-rustls"]
[dependencies]
term-painter = "0.2"
yansi = "0.2"
log = "0.3"
url = "1"
toml = { version = "0.2", default-features = false }
@ -47,5 +47,5 @@ lazy_static = "0.2"
rocket_codegen = { version = "0.2.8", path = "../codegen" }
[build-dependencies]
ansi_term = "0.9"
yansi = "0.2"
version_check = "^0.1"

View File

@ -1,10 +1,10 @@
//! This tiny build script ensures that rocket is not compiled with an
//! incompatible version of rust.
extern crate ansi_term;
extern crate yansi;
extern crate version_check;
use ansi_term::Color::{Red, Yellow, Blue, White};
use yansi::Color::{Red, Yellow, Blue, White};
use version_check::{is_nightly, is_min_version};
// Specifies the minimum nightly version needed to compile Rocket.
@ -32,7 +32,7 @@ fn main() {
if let (Some(is_nightly), Some((ok_version, version))) = (ok_nightly, ok_version) {
if !is_nightly {
printerr!("{} {}",
Red.bold().paint("Error:"),
Red.paint("Error:").bold(),
White.paint("Rocket requires a nightly version of Rust."));
print_version_err(&*version);
printerr!("{}{}{}",
@ -44,7 +44,7 @@ fn main() {
if !ok_version {
printerr!("{} {}",
Red.bold().paint("Error:"),
Red.paint("Error:").bold(),
White.paint("Rocket requires a newer version of rustc."));
printerr!("{}{}{}",
Blue.paint("Use `"),

View File

@ -5,8 +5,7 @@ use error::Error;
use request::Request;
use std::fmt;
use term_painter::ToStyle;
use term_painter::Color::*;
use yansi::Color::*;
/// An error catching route.
///

View File

@ -5,8 +5,7 @@ use std::fmt;
use super::Environment;
use self::ConfigError::*;
use term_painter::Color::White;
use term_painter::ToStyle;
use yansi::Color::White;
/// The type of a configuration parsing error.
#[derive(Debug, PartialEq, Clone)]

View File

@ -82,8 +82,7 @@ impl Fairings {
}
pub fn pretty_print_counts(&self) {
use term_painter::ToStyle;
use term_painter::Color::{White, Magenta};
use yansi::Paint;
if self.all_fairings.is_empty() {
return
@ -92,12 +91,12 @@ impl Fairings {
fn info_if_nonempty(kind: &str, fairings: &[&Fairing]) {
let names: Vec<&str> = fairings.iter().map(|f| f.info().name).collect();
info_!("{} {}: {}",
White.paint(fairings.len()),
Paint::white(fairings.len()),
kind,
White.paint(names.join(", ")));
Paint::white(names.join(", ")));
}
info!("📡 {}:", Magenta.paint("Fairings"));
info!("📡 {}:", Paint::purple("Fairings"));
info_if_nonempty("launch", &self.launch);
info_if_nonempty("request", &self.request);
info_if_nonempty("response", &self.response);

View File

@ -101,7 +101,7 @@
#[macro_use] extern crate pear;
#[cfg(feature = "tls")] extern crate rustls;
#[cfg(feature = "tls")] extern crate hyper_rustls;
extern crate term_painter;
extern crate yansi;
extern crate hyper;
extern crate url;
extern crate toml;

View File

@ -4,8 +4,7 @@ use std::str::FromStr;
use std::fmt;
use log::{self, Log, LogLevel, LogRecord, LogMetadata};
use term_painter::Color::*;
use term_painter::ToStyle;
use yansi::Color::*;
struct RocketLogger(LoggingLevel);
@ -119,20 +118,20 @@ impl Log for RocketLogger {
use log::LogLevel::*;
match level {
Info => println!("{}", Blue.paint(record.args())),
Trace => println!("{}", Magenta.paint(record.args())),
Trace => println!("{}", Purple.paint(record.args())),
Error => {
println!("{} {}",
Red.bold().paint("Error:"),
Red.paint("Error:").bold(),
Red.paint(record.args()))
}
Warn => {
println!("{} {}",
Yellow.bold().paint("Warning:"),
Yellow.paint("Warning:").bold(),
Yellow.paint(record.args()))
}
Debug => {
let loc = record.location();
print!("\n{} ", Blue.bold().paint("-->"));
print!("\n{} ", Blue.paint("-->").bold());
println!("{}:{}", Blue.paint(loc.file()), Blue.paint(loc.line()));
println!("{}", record.args());
}

View File

@ -81,9 +81,7 @@
use std::fmt;
use term_painter::Color::*;
use term_painter::Color;
use term_painter::ToStyle;
use yansi::{Paint, Color};
use self::Outcome::*;
@ -422,9 +420,9 @@ impl<S, E, F> Outcome<S, E, F> {
#[inline]
fn formatting(&self) -> (Color, &'static str) {
match *self {
Success(..) => (Green, "Success"),
Failure(..) => (Red, "Failure"),
Forward(..) => (Yellow, "Forward"),
Success(..) => (Color::Green, "Success"),
Failure(..) => (Color::Red, "Failure"),
Forward(..) => (Color::Yellow, "Forward"),
}
}
}
@ -438,6 +436,6 @@ impl<S, E, F> fmt::Debug for Outcome<S, E, F> {
impl<S, E, F> fmt::Display for Outcome<S, E, F> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let (color, string) = self.formatting();
write!(f, "{}", color.paint(string))
write!(f, "{}", Paint::new(string).fg(color))
}
}

View File

@ -3,8 +3,7 @@ use std::net::SocketAddr;
use std::fmt;
use std::str;
use term_painter::Color::*;
use term_painter::ToStyle;
use yansi::Paint;
use state::{Container, Storage};
@ -580,10 +579,10 @@ impl<'r> fmt::Display for Request<'r> {
/// Pretty prints a Request. This is primarily used by Rocket's logging
/// infrastructure.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", Green.paint(&self.method), Blue.paint(&self.uri))?;
write!(f, "{} {}", Paint::green(&self.method), Paint::blue(&self.uri))?;
if let Some(content_type) = self.content_type() {
if self.method.supports_payload() {
write!(f, " {}", Yellow.paint(content_type))?;
write!(f, " {}", Paint::yellow(content_type))?;
}
}

View File

@ -5,8 +5,7 @@ use std::net::SocketAddr;
use std::io::{self, Write};
use std::mem;
use term_painter::Color::*;
use term_painter::ToStyle;
use yansi::Paint;
use state::Container;
#[cfg(feature = "tls")] use hyper_rustls::TlsServer;
@ -111,7 +110,7 @@ impl Rocket {
#[inline]
fn issue_response(&self, response: Response, hyp_res: hyper::FreshResponse) {
match self.write_response(response, hyp_res) {
Ok(_) => info_!("{}", Green.paint("Response succeeded.")),
Ok(_) => info_!("{}", Paint::green("Response succeeded.")),
Err(e) => error_!("Failed to write response: {:?}.", e)
}
}
@ -251,7 +250,7 @@ impl Rocket {
// There was no matching route.
if request.method() == Method::Head {
info_!("Autohandling {} request.", White.paint("HEAD"));
info_!("Autohandling {} request.", Paint::white("HEAD"));
request.set_method(Method::Get);
let mut response = self.dispatch(request, data);
response.strip_body();
@ -298,7 +297,7 @@ impl Rocket {
// Check if the request processing completed or if the request needs
// to be forwarded. If it does, continue the loop to try again.
info_!("{} {}", White.paint("Outcome:"), outcome);
info_!("{} {}", Paint::white("Outcome:"), outcome);
match outcome {
o@Outcome::Success(_) | o @Outcome::Failure(_) => return o,
Outcome::Forward(unused_data) => data = unused_data,
@ -315,7 +314,7 @@ impl Rocket {
// 500 catcher is executed. if there is no registered catcher for `status`,
// the default catcher is used.
fn handle_error<'r>(&self, status: Status, req: &'r Request) -> Response<'r> {
warn_!("Responding with {} catcher.", Red.paint(&status));
warn_!("Responding with {} catcher.", Paint::red(&status));
// Try to get the active catcher but fallback to user's 500 catcher.
let catcher = self.catchers.get(&status.code).unwrap_or_else(|| {
@ -396,28 +395,28 @@ impl Rocket {
}
info!("🔧 Configured for {}.", config.environment);
info_!("address: {}", White.paint(&config.address));
info_!("port: {}", White.paint(&config.port));
info_!("log: {}", White.paint(config.log_level));
info_!("workers: {}", White.paint(config.workers));
info_!("secret key: {}", White.paint(config.secret_key.kind()));
info_!("limits: {}", White.paint(&config.limits));
info_!("address: {}", Paint::white(&config.address));
info_!("port: {}", Paint::white(&config.port));
info_!("log: {}", Paint::white(config.log_level));
info_!("workers: {}", Paint::white(config.workers));
info_!("secret key: {}", Paint::white(config.secret_key.kind()));
info_!("limits: {}", Paint::white(&config.limits));
let tls_configured = config.tls.is_some();
if tls_configured && cfg!(feature = "tls") {
info_!("tls: {}", White.paint("enabled"));
info_!("tls: {}", Paint::white("enabled"));
} else {
if tls_configured {
error_!("tls: {}", White.paint("disabled"));
error_!("tls: {}", Paint::white("disabled"));
error_!("tls is configured, but the tls feature is disabled");
} else {
info_!("tls: {}", White.paint("disabled"));
info_!("tls: {}", Paint::white("disabled"));
}
}
for (name, value) in config.extras() {
info_!("{} {}: {}",
Yellow.paint("[extra]"), name, White.paint(LoggedValue(value)));
Paint::yellow("[extra]"), name, Paint::white(LoggedValue(value)));
}
Rocket {
@ -483,7 +482,7 @@ impl Rocket {
/// ```
#[inline]
pub fn mount(mut self, base: &str, routes: Vec<Route>) -> Self {
info!("🛰 {} '{}':", Magenta.paint("Mounting"), base);
info!("🛰 {} '{}':", Paint::purple("Mounting"), base);
if base.contains('<') {
error_!("Bad mount point: '{}'.", base);
@ -534,11 +533,11 @@ impl Rocket {
/// ```
#[inline]
pub fn catch(mut self, catchers: Vec<Catcher>) -> Self {
info!("👾 {}:", Magenta.paint("Catchers"));
info!("👾 {}:", Paint::purple("Catchers"));
for c in catchers {
if self.catchers.get(&c.code).map_or(false, |e| !e.is_default()) {
let msg = "(warning: duplicate catcher!)";
info_!("{} {}", c, Yellow.paint(msg));
info_!("{} {}", c, Paint::yellow(msg));
} else {
info_!("{}", c);
}
@ -685,9 +684,9 @@ impl Rocket {
let full_addr = format!("{}:{}", self.config.address, self.config.port);
launch_info!("🚀 {} {}{}",
White.paint("Rocket has launched from"),
White.bold().paint(proto),
White.bold().paint(&full_addr));
Paint::white("Rocket has launched from"),
Paint::white(proto).bold(),
Paint::white(&full_addr).bold());
let threads = self.config.workers as usize;
if let Err(e) = server.handle_threads(self, threads) {

View File

@ -1,8 +1,7 @@
use std::fmt;
use std::convert::From;
use term_painter::ToStyle;
use term_painter::Color::*;
use yansi::Color::*;
use codegen::StaticRouteInfo;
use handler::Handler;