0

In a program I am trying to display the artwork of my currently playing spotify song using rust.

The code only works if I copy and paste the url into the argument, so I tried making a variable called arturl to use in a .arg(arturl). But that makes the code return nothing, and the arturl variable does return the correct value.

My code:

use std::process::{Command, Stdio};

fn main() {
    let arturl = Command::new("playerctl")
        .arg("metadata")
        .arg("mpris:artUrl")
        .stdout(Stdio::piped())
        .output()
        .expect("url failed");
    let arturl = String::from_utf8(arturl.stdout).unwrap();

    let picture = Command::new("chafa")
        .arg("--size=30")
        .arg(arturl)
        .stdout(Stdio::piped())
        .output()
        .expect("picture failed");
    let picture = String::from_utf8(picture.stdout).unwrap();
    println!("{}", picture);
}
2
  • What does this code print if you add println!("{:?}", arturl) (exactly this, with Debug formatting, to see possible unprintable symbols)? Commented May 22, 2022 at 5:53
  • Thanks this helps a whole lot, apparently there was a trailing new line in the string... Commented May 22, 2022 at 6:06

2 Answers 2

1

Managed to fixed this with adding a simple arturl.pop to get rid of the newline at the end of the string

fixed code:

use std::process::{Command, Stdio};

fn main() {
    let arturl = Command::new("playerctl")
        .arg("metadata")
        .arg("mpris:artUrl")
        .stdout(Stdio::piped())
        .output()
        .expect("url failed");
    let mut arturl = String::from_utf8(arturl.stdout).unwrap();
    arturl.pop();
    println!("{:?}", arturl);

    let picture = Command::new("chafa")
        .arg("--size=30")
        .arg(arturl)
        .stdout(Stdio::piped())
        .output()
        .expect("picture failed");
    let picture = String::from_utf8(picture.stdout).unwrap();
    println!("{}", picture);
}
Sign up to request clarification or add additional context in comments.

1 Comment

The more idiomatic solution would probably be to trim() the output, since this would more explicitly tell the intention. This wouldn't work if the URL can have legitimate spaces at the either end, however.
1

You should probably "clean" the string with str::trim:

use std::process::{Command, Stdio};

fn main() {
    let arturl = Command::new("playerctl")
        .arg("metadata")
        .arg("mpris:artUrl")
        .stdout(Stdio::piped())
        .output()
        .expect("url failed");
    let arturl = String::from_utf8(arturl.stdout).unwrap().trim();
    println!("{:?}", arturl);

    let picture = Command::new("chafa")
        .arg("--size=30")
        .arg(arturl)
        .stdout(Stdio::piped())
        .output()
        .expect("picture failed");
    let picture = String::from_utf8(picture.stdout).unwrap();
    println!("{}", picture);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.