aplay 0.1.0

A Command Line Audio Player
use clap::{app_from_crate, crate_description, crate_authors, crate_name, crate_version, Arg};
use colored::Colorize;
use std::io::BufReader;
use std::path::Path;
use rodio::{Decoder, OutputStream, Sink};

fn main() {
    let matches = app_from_crate!()
        .arg(Arg::with_name("FILE")
            .help("Sets the input file to use")
            .required(true)
            .index(1))
        .get_matches();

    let (_stream, stream_handle) = match OutputStream::try_default() {
        Ok((stream, stream_handle)) => (stream, stream_handle),
        Err(error) => {
            clap_error(error.to_string());
            std::process::exit(1);
        },
    };

    let sink = match Sink::try_new(&stream_handle) {
        Ok(data) => data,
        Err(error) => {
            clap_error(error.to_string());
            std::process::exit(1);
        }
    };

    let path = Path::new(matches.value_of("FILE").unwrap());

    if !path.is_file() {
        clap_error("Couldn't find <FILE>");
        std::process::exit(1);
    };

    let file = match std::fs::File::open(path) {
        Ok(data) => data,
        Err(error) => {
            clap_error(error.to_string());
            std::process::exit(1);
        },
    };

    let file = BufReader::new(file);

    let source = match Decoder::new(file) {
        Ok(data) => data,
        Err(error) => {
            clap_error(error.to_string());
            std::process::exit(1);
        }
    };
    sink.append(source);

    println!("Playing \"{}\"", path.file_name().unwrap().to_string_lossy());
    sink.sleep_until_end();
}

fn clap_error<T: AsRef<str>>(text: T) {
    eprintln!("{} {}\n", "error:".red().bold(), text.as_ref());
    eprintln!("For more information try {}", "--help".green());
}