use std::io::stdio::StdWriter;
pub struct ShellConfig {
- color: bool,
- verbose: bool
+ pub color: bool,
+ pub verbose: bool,
+ pub tty: bool
}
-enum AdequateTerminal {
- NoColor(BasicTerminal<StdWriter>),
- Color(Box<Terminal<StdWriter>>)
+enum AdequateTerminal<T> {
+ NoColor(BasicTerminal<T>),
+ Color(Box<Terminal<T>>)
}
-pub struct Shell {
- terminal: AdequateTerminal,
+pub struct Shell<T> {
+ terminal: AdequateTerminal<T>,
config: ShellConfig
}
-impl Shell {
- fn create(out: StdWriter, config: ShellConfig) -> Option<Shell> {
- let term = if out.isatty() {
- let term: Option<term::TerminfoTerminal<StdWriter>> = Terminal::new(out);
- term.map(|t| Color(box t))
+impl<T: Writer + Send> Shell<T> {
+ pub fn create(out: T, config: ShellConfig) -> Option<Shell<T>> {
+ if config.tty {
+ let term: Option<term::TerminfoTerminal<T>> = Terminal::new(out);
+ term.map(|t| Shell { terminal: Color(box t as Box<Terminal<T>>), config: config })
} else {
- Some(NoColor(BasicTerminal { writer: out }))
- };
-
- term.map(|term| Shell { terminal: term, config: config })
+ Some(Shell { terminal: NoColor(BasicTerminal { writer: out }), config: config })
+ }
}
- pub fn verbose(&mut self, callback: |&mut Shell| -> IoResult<()>) -> IoResult<()> {
+ pub fn verbose(&mut self, callback: |&mut Shell<T>| -> IoResult<()>) -> IoResult<()> {
if self.config.verbose {
return callback(self)
}
}
}
-impl Terminal<StdWriter> for Shell {
- fn new(out: StdWriter) -> Option<Shell> {
- Shell::create(out, ShellConfig { color: true, verbose: false })
+impl<T: Writer + Send> Terminal<T> for Shell<T> {
+ fn new(out: T) -> Option<Shell<T>> {
+ Shell::create(out, ShellConfig { color: true, verbose: false, tty: false })
}
fn fg(&mut self, color: color::Color) -> IoResult<bool> {
}
}
- fn unwrap(self) -> StdWriter {
- fail!("Can't unwrap a Shell")
+ fn unwrap(self) -> T {
+ fail!("Can't unwrap a Shell");
}
- fn get_ref<'a>(&'a self) -> &'a StdWriter {
+ fn get_ref<'a>(&'a self) -> &'a T {
match self.terminal {
Color(ref c) => c.get_ref(),
NoColor(ref n) => n.get_ref()
}
}
- fn get_mut<'a>(&'a mut self) -> &'a mut StdWriter {
+ fn get_mut<'a>(&'a mut self) -> &'a mut T {
match self.terminal {
Color(ref mut c) => c.get_mut(),
NoColor(ref mut n) => n.get_mut()
}
}
-impl Writer for Shell {
+impl<T: Writer + Send> Writer for Shell<T> {
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
match self.terminal {
Color(ref mut c) => c.write(buf),
writer: T
}
-impl<T: Writer> Terminal<T> for BasicTerminal<T> {
+impl<T: Writer + Send> Terminal<T> for BasicTerminal<T> {
fn new(out: T) -> Option<BasicTerminal<T>> {
Some(BasicTerminal { writer: out })
}
}
}
-impl<T: Writer> Writer for BasicTerminal<T> {
+impl<T: Writer + Send> Writer for BasicTerminal<T> {
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
self.writer.write(buf)
}
// use std::io::fs::{mkdir_recursive,rmdir_recursive};
+use std;
use std::io;
use std::io::fs;
use std::io::process::{ProcessOutput,ProcessExit};
use std::vec::Vec;
use std::fmt::Show;
use ham = hamcrest;
+use cargo::core::shell;
use cargo::util::{process,ProcessBuilder,CargoError};
use cargo::util::result::ProcessError;
}
}
+#[deriving(Clone,Eq)]
+struct ShellWrites {
+ expected: String
+}
+
+impl ham::SelfDescribing for ShellWrites {
+ fn describe(&self) -> String {
+ format!("`{}` written to the shell", self.expected)
+ }
+}
+
+impl<'a> ham::Matcher<&'a mut shell::Shell<std::io::MemWriter>> for ShellWrites {
+ fn matches(&self, actual: &mut shell::Shell<std::io::MemWriter>) -> ham::MatchResult {
+ use term::Terminal;
+
+ let actual = std::str::from_utf8_lossy(actual.get_ref().get_ref()).to_str();
+ ham::expect(actual == self.expected, actual)
+ }
+}
+
+pub fn shell_writes<T: Show>(string: T) -> Box<ShellWrites> {
+ box ShellWrites { expected: string.to_str() }
+}
+
pub trait ResultTest<T,E> {
fn assert(self) -> T;
}
}
}
}
+
+impl<T> ResultTest<T,()> for Option<T> {
+ fn assert(self) -> T {
+ match self {
+ Some(val) => val,
+ None => fail!("Option was None")
+ }
+ }
+}
+
+pub trait Tap {
+ fn tap(mut self, callback: |&mut Self|) -> Self;
+}
+
+impl<T> Tap for T {
+ fn tap(mut self, callback: |&mut T|) -> T {
+ callback(&mut self);
+ self
+ }
+}