~exitb/retrofit

97b7315f98bdeec78a49bcb9700bbcbc34d0fdc1 — exitb 1 year, 10 months ago cbd3ade
Add option of file output
1 files changed, 20 insertions(+), 3 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +20 -3
@@ 28,7 28,8 @@ enum Error {
    Adapter(adapter::Error),
    WriteOut(std::io::Error),
    TerminationHandlerSetting(ctrlc::Error),
    FileOpening(std::io::Error),
    InputFileOpening(std::io::Error),
    OutputFileCreating(std::io::Error),
}

type Result<T> = std::result::Result<T, Error>;


@@ 77,6 78,14 @@ fn retrofit() -> Result<()> {
                .default_value("8bit"),
        )
        .arg(
            Arg::with_name("output")
                .short("o")
                .long("output")
                .value_name("FILE")
                .help("Redirects the output to a file instead of the standard output")
                .takes_value(true)
        )
        .arg(
            Arg::with_name("ice-colors")
                .short("i")
                .long("ice-colors")


@@ 109,7 118,7 @@ fn retrofit() -> Result<()> {

    let source = match matches.value_of("FILE") {
        Some(filename) => {
            let file = File::open(filename).map_err(Error::FileOpening)?;
            let file = File::open(filename).map_err(Error::InputFileOpening)?;
            Box::new(file) as Box<dyn Read>
        }
        None => Box::new(stdin()) as Box<dyn Read>,


@@ 120,7 129,15 @@ fn retrofit() -> Result<()> {
    let hide_cursor = baud.is_some() || matches.occurrences_of("hide-cursor") > 0;
    let ice_colors = matches.occurrences_of("ice-colors") > 0;

    let mut output = BufWriter::new(stdout());
    let target = match matches.value_of("output") {
        Some(filename) => {
            let file = File::create(filename).map_err(Error::OutputFileCreating)?;
            Box::new(file) as Box<dyn Write>
        }
        None => Box::new(stdout()) as Box<dyn Write>
    };

    let mut output = BufWriter::new(target);

    BufReader::new(source)
        .bytes()