Search code examples
rusttput

Unable to execute `tput` command with std::io::process::Command


When I run tput cols in my terminal, it prints out the number of columns just fine. But when I run the following Rust program:

use std::io::process::{Command, ProcessOutput};

fn main() {
    let cmd = Command::new("tput cols");
    match cmd.output() {
        Ok(ProcessOutput { error: _, output: out, status: exit }) => {
            if exit.success() {
                println!("{}" , out);
                match String::from_utf8(out) {
                    Ok(res)  => println!("{}" , res),
                    Err(why) => println!("error converting to utf8: {}" , why),
                }
            } else {
                println!("Didn't exit succesfully")
            }
        }
        Err(why) => println!("Error running command: {}" , why.desc),
    }
}

I get the following error:

Error running command: no such file or directory

Does anyone know why the command doesn't run correctly? Why is it looking for a file or directory?


Solution

  • Command::new takes the name of the command to run only; arguments can be added using .arg().

    match Command::new("tput").arg("cols").output() {
        // …
    }