extern crate mortal;
extern crate rand;
use std::io;
use std::sync::Arc;
use std::thread::{spawn, sleep};
use std::time::Duration;
use mortal::{Color, Terminal};
use rand::{Rng, seq::SliceRandom, thread_rng};
const COLORS: &[Color] = &[
Color::Blue,
Color::Red,
Color::Green,
Color::Cyan,
Color::Magenta,
];
fn main() -> io::Result<()> {
let term = Arc::new(Terminal::new()?);
let mut handles = Vec::new();
let mut colors = COLORS.to_vec();
colors.shuffle(&mut thread_rng());
for i in 0..5 {
let name = format!("child{}", i);
let term = term.clone();
let color = colors[i];
let handle = spawn(move || {
run_task(&name, color, &term).unwrap()
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
Ok(())
}
fn run_task(name: &str, color: Color, term: &Terminal)
-> io::Result<()> {
let mut rng = thread_rng();
for _ in 0..5 {
sleep(Duration::from_millis(rng.gen_range(100..300)));
let mut lock = term.lock_write().unwrap();
lock.write_str("[")?;
lock.bold()?;
lock.set_fg(color)?;
lock.write_str(name)?;
lock.clear_attributes()?;
writeln!(lock, "]: random output: {}", rng.gen::<u8>())?;
}
Ok(())
}