Skip to content

Commit

Permalink
Auto merge of #4178 - nrc:shell, r=alexcrichton
Browse files Browse the repository at this point in the history
Support having a generic writer instead of a termcolor StandardStream

This is requied by the RLS (and presumably any other client who wants to use Cargo as a lib and redirect output)

r? @alexcrichton
  • Loading branch information
bors committed Jun 17, 2017
2 parents b4b7ed5 + 6f45cb6 commit c339214
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 35 deletions.
109 changes: 78 additions & 31 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ pub enum Verbosity {
}

pub struct Shell {
err: StandardStream,
err: ShellOut,
verbosity: Verbosity,
choice: ColorChoice,
}

enum ShellOut {
Write(Box<Write>),
Stream(StandardStream, ColorChoice),
}

#[derive(PartialEq, Clone, Copy)]
Expand All @@ -30,9 +34,18 @@ pub enum ColorChoice {
impl Shell {
pub fn new() -> Shell {
Shell {
err: StandardStream::stderr(ColorChoice::CargoAuto.to_termcolor_color_choice()),
err: ShellOut::Stream(
StandardStream::stderr(ColorChoice::CargoAuto.to_termcolor_color_choice()),
ColorChoice::CargoAuto,
),
verbosity: Verbosity::Verbose,
}
}

pub fn from_write(out: Box<Write>) -> Shell {
Shell {
err: ShellOut::Write(out),
verbosity: Verbosity::Verbose,
choice: ColorChoice::CargoAuto,
}
}

Expand All @@ -44,24 +57,13 @@ impl Shell {
match self.verbosity {
Verbosity::Quiet => Ok(()),
_ => {
self.err.reset()?;
self.err.set_color(ColorSpec::new()
.set_bold(true)
.set_fg(Some(color)))?;
if justified {
write!(self.err, "{:>12}", status)?;
} else {
write!(self.err, "{}", status)?;
}
self.err.reset()?;
write!(self.err, " {}\n", message)?;
Ok(())
self.err.print(status, message, color, justified)
}
}
}

pub fn err(&mut self) -> &mut StandardStream {
&mut self.err
pub fn err(&mut self) -> &mut Write {
self.err.as_write()
}

pub fn status<T, U>(&mut self, status: T, message: U) -> CargoResult<()>
Expand Down Expand Up @@ -117,23 +119,68 @@ impl Shell {
}

pub fn set_color_choice(&mut self, color: Option<&str>) -> CargoResult<()> {
let cfg = match color {
Some("always") => ColorChoice::Always,
Some("never") => ColorChoice::Never,
if let ShellOut::Stream(ref mut err, ref mut cc) = self.err {
let cfg = match color {
Some("always") => ColorChoice::Always,
Some("never") => ColorChoice::Never,

Some("auto") |
None => ColorChoice::CargoAuto,

Some(arg) => bail!("argument for --color must be auto, always, or \
never, but found `{}`", arg),
};
*cc = cfg;
*err = StandardStream::stderr(cfg.to_termcolor_color_choice());
}
Ok(())
}

Some("auto") |
None => ColorChoice::CargoAuto,
pub fn color_choice(&self) -> ColorChoice {
match self.err {
ShellOut::Stream(_, cc) => cc,
ShellOut::Write(_) => ColorChoice::Never,
}
}
}

Some(arg) => bail!("argument for --color must be auto, always, or \
never, but found `{}`", arg),
};
self.choice = cfg;
self.err = StandardStream::stderr(cfg.to_termcolor_color_choice());
return Ok(());
impl ShellOut {
fn print(&mut self,
status: &fmt::Display,
message: &fmt::Display,
color: Color,
justified: bool) -> CargoResult<()> {
match *self {
ShellOut::Stream(ref mut err, _) => {
err.reset()?;
err.set_color(ColorSpec::new()
.set_bold(true)
.set_fg(Some(color)))?;
if justified {
write!(err, "{:>12}", status)?;
} else {
write!(err, "{}", status)?;
}
err.reset()?;
write!(err, " {}\n", message)?;
}
ShellOut::Write(ref mut w) => {
if justified {
write!(w, "{:>12}", status)?;
} else {
write!(w, "{}", status)?;
}
write!(w, " {}\n", message)?;
}
}
Ok(())
}

pub fn color_choice(&self) -> ColorChoice {
self.choice
fn as_write(&mut self) -> &mut Write {
match *self {
ShellOut::Stream(ref mut err, _) => err,
ShellOut::Write(ref mut w) => w,
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/cargo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ extern crate termcolor;
extern crate toml;
extern crate url;

use std::io::Write;
use std::fmt;
use std::error::Error;

Expand Down
1 change: 0 additions & 1 deletion src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::BTreeMap;
use std::env;
use std::fs;
use std::io::Write;
use std::path::Path;

use serde::{Deserialize, Deserializer};
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_rustc/job_queue.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashSet;
use std::collections::hash_map::HashMap;
use std::fmt;
use std::io::{self, Write};
use std::io;
use std::mem;
use std::sync::mpsc::{channel, Sender, Receiver};

Expand Down
1 change: 0 additions & 1 deletion src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::env;
use std::fs::{self, File};
use std::io::Write;
use std::iter::repeat;
use std::time::Duration;

Expand Down

0 comments on commit c339214

Please sign in to comment.