Skip to content

Instantly share code, notes, and snippets.

@wycats
Created May 27, 2014 22:51
Show Gist options
  • Save wycats/401c71af3087eb03dc26 to your computer and use it in GitHub Desktop.
Save wycats/401c71af3087eb03dc26 to your computer and use it in GitHub Desktop.
use term;
use term::{Terminal,color};
use term::color::Color;
use term::attr::Attr;
use std::io;
use std::io::IoResult;
use std::io::stdio::StdWriter;
enum Shell {
NoColor(BasicTerminal<StdWriter>),
Color(Box<Terminal<StdWriter>>)
}
impl Shell {
fn with_out(out: StdWriter) -> Option<Shell> {
if out.isatty() {
let term: Option<term::TerminfoTerminal<StdWriter>> = Terminal::new(out);
term.map(|t| Color(box t))
} else {
Some(NoColor(BasicTerminal { writer: out }))
}
}
}
impl Terminal<StdWriter> for Shell {
fn new(out: StdWriter) -> Option<Shell> {
Shell::with_out(out)
}
fn fg(&mut self, color: color::Color) -> IoResult<bool> {
match self {
&Color(ref mut c) => c.fg(color),
&NoColor(ref mut n) => n.fg(color)
}
}
fn bg(&mut self, color: color::Color) -> IoResult<bool> {
match self {
&Color(ref mut c) => c.bg(color),
&NoColor(ref mut n) => n.bg(color)
}
}
fn attr(&mut self, attr: Attr) -> IoResult<bool> {
match self {
&Color(ref mut c) => c.attr(attr),
&NoColor(ref mut n) => n.attr(attr)
}
}
fn supports_attr(&self, attr: Attr) -> bool {
match self {
&Color(ref c) => c.supports_attr(attr),
&NoColor(ref n) => n.supports_attr(attr)
}
}
fn reset(&mut self) -> IoResult<()> {
match self {
&Color(ref mut c) => c.reset(),
&NoColor(ref mut n) => n.reset()
}
}
fn unwrap(self) -> StdWriter {
fail!("Can't unwrap a Shell")
}
fn get_ref<'a>(&'a self) -> &'a StdWriter {
match self {
&Color(ref c) => c.get_ref(),
&NoColor(ref n) => n.get_ref()
}
}
fn get_mut<'a>(&'a mut self) -> &'a mut StdWriter {
match self {
&Color(ref mut c) => c.get_mut(),
&NoColor(ref mut n) => n.get_mut()
}
}
}
impl Writer for Shell {
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
match self {
&Color(ref mut c) => c.write(buf),
&NoColor(ref mut n) => n.write(buf)
}
}
fn flush(&mut self) -> IoResult<()> {
match self {
&Color(ref mut c) => c.flush(),
&NoColor(ref mut n) => n.flush()
}
}
}
pub struct BasicTerminal<T> {
writer: T
}
impl<T: Writer> Terminal<T> for BasicTerminal<T> {
fn new(out: T) -> Option<BasicTerminal<T>> {
Some(BasicTerminal { writer: out })
}
fn fg(&mut self, _: Color) -> IoResult<bool> {
Ok(false)
}
fn bg(&mut self, _: Color) -> IoResult<bool> {
Ok(false)
}
fn attr(&mut self, _: Attr) -> IoResult<bool> {
Ok(false)
}
fn supports_attr(&self, _: Attr) -> bool {
false
}
fn reset(&mut self) -> IoResult<()> {
Ok(())
}
fn unwrap(self) -> T {
self.writer
}
fn get_ref<'a>(&'a self) -> &'a T {
&self.writer
}
fn get_mut<'a>(&'a mut self) -> &'a mut T {
&mut self.writer
}
}
impl<T: Writer> Writer for BasicTerminal<T> {
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
self.writer.write(buf)
}
fn flush(&mut self) -> IoResult<()> {
self.writer.flush()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment