use ipipe::Pipe;
use std::thread;
use std::io::{BufRead, BufReader, Write};

const CANCEL: u8 = 24;

fn main() {
    let pipe = Pipe::create().unwrap();
    println!("Name: {}", pipe.path().display());

    let writer = pipe.clone();
    thread::spawn(move || print_nums(writer));
    for line in BufReader::new(pipe).lines() {
        println!("{}", line.unwrap());
    }
}

fn print_nums(mut pipe: Pipe) {
    for i in 1..=10 {
        // pipe.write_all(format!("{}\n", i).as_bytes());
        writeln!(&mut pipe, "{}", i).unwrap();
    }
    write!(&mut pipe, "{}", CANCEL as char).unwrap();
}