2

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102

1 Answers1

4

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

match Command::new("tput").arg("cols").output() {
    // …
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • 2
    This does indeed solve the problem. Now I just got to get to produce better output. Right now running `tput` returns 80 no matter the size of the terminal. – Kurtis Nusbaum Dec 28 '14 at 03:15