Created
January 10, 2015 20:25
-
-
Save adamhjk/4ecd72d4f633945d26f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Compiling delivery v0.0.1 (file:///Users/adam/src/opscode/delivery/opscode/delivery-cli) | |
/Users/adam/src/opscode/delivery/opscode/delivery-cli/src/utils/say.rs:11:12: 11:25 error: wrong number of lifetime parameters: expected 1, found 0 [E0107] | |
/Users/adam/src/opscode/delivery/opscode/delivery-cli/src/utils/say.rs:11 guard: JoinGuard<()> | |
^~~~~~~~~~~~~ | |
error: aborting due to previous error | |
Could not compile `delivery`. | |
To learn more, run the command again with --verbose. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate term; | |
use std::time::duration::Duration; | |
use std::sync::mpsc::{Sender, Receiver}; | |
use std::io::timer::{sleep}; | |
use std::sync::mpsc::channel; | |
use std::thread::{Thread, JoinGuard}; | |
pub struct Spinner { | |
tx: Sender<isize>, | |
guard: JoinGuard<()> | |
} | |
impl Spinner { | |
pub fn start() -> Spinner { | |
let (tx, rx) = channel::<isize>(); | |
let spinner = Thread::spawn(move|| { Spinner::spin(rx) }); | |
Spinner{ tx: tx, guard: spinner } | |
} | |
pub fn stop(self) { | |
let _ = self.tx.send(1); | |
let _ = self.guard.join(); | |
} | |
fn spin(rx: Receiver<isize>) { | |
let spinner_chars = vec!["|", "/", "-", "\\",]; | |
for spin in spinner_chars.iter().cycle() { | |
say("white", "\x08"); | |
say("yellow", *spin); | |
let r = rx.try_recv(); | |
match r { | |
Ok(_) => { | |
say("white", "\x08 \x08"); | |
break; | |
}, | |
Err(_) => { | |
sleep(Duration::milliseconds(100i64)); | |
continue; | |
} | |
} | |
} | |
} | |
} | |
pub fn say(color: &str, to_say: &str) { | |
let mut t = term::stdout().unwrap(); | |
let color_const = match color { | |
"green" => term::color::BRIGHT_GREEN, | |
"yellow" => term::color::BRIGHT_YELLOW, | |
"red" => term::color::BRIGHT_RED, | |
"magenta" => term::color::BRIGHT_MAGENTA, | |
"white" => term::color::WHITE, | |
_ => term::color::WHITE | |
}; | |
t.fg(color_const).unwrap(); | |
(write!(t, "{}", to_say)).unwrap(); | |
t.reset().unwrap() | |
} | |
pub fn sayln(color: &str, to_say: &str) { | |
let mut t = term::stdout().unwrap(); | |
say(color, to_say); | |
(write!(t, "\n")).unwrap(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment