diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-02-14 17:15:18 +0100 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-02-14 17:15:18 +0100 |
commit | 66c739237cc352fedf6276a3163097c1f1f32bd4 (patch) | |
tree | efdc61c079473e4d86f38454cd1568337e39f219 /crates/libmpv2/src/mpv.rs | |
parent | fix(yt/watch): Always open a `mpv` window (diff) | |
download | yt-66c739237cc352fedf6276a3163097c1f1f32bd4.zip |
fix(crates/libmpv2/Mpv::command): Correctly escape arguments
This allows us to avoid all these ad-hoc command escaping `format!` invocations.
Diffstat (limited to 'crates/libmpv2/src/mpv.rs')
-rw-r--r-- | crates/libmpv2/src/mpv.rs | 26 |
1 files changed, 10 insertions, 16 deletions
diff --git a/crates/libmpv2/src/mpv.rs b/crates/libmpv2/src/mpv.rs index 07d0976..6f26d0f 100644 --- a/crates/libmpv2/src/mpv.rs +++ b/crates/libmpv2/src/mpv.rs @@ -526,19 +526,6 @@ impl Mpv { }) } - /// Execute a command - pub fn execute(&self, name: &str, args: &[&str]) -> Result<()> { - if args.is_empty() { - debug!("Running mpv command: '{}'", name); - } else { - debug!("Running mpv command: '{} {}'", name, args.join(" ")); - } - - self.command(name, args)?; - - Ok(()) - } - /// Load a configuration file. The path has to be absolute, and a file. pub fn load_config(&self, path: &str) -> Result<()> { let file = CString::new(path)?.into_raw(); @@ -562,7 +549,7 @@ impl Mpv { /// Send a command to the `Mpv` instance. This uses `mpv_command_string` internally, /// so that the syntax is the same as described in the [manual for the input.conf](https://mpv.io/manual/master/#list-of-input-commands). /// - /// Note that you may have to escape strings with `""` when they contain spaces. + /// Note that this function escapes the arguments for you. /// /// # Examples /// @@ -583,12 +570,19 @@ impl Mpv { /// # } /// ``` pub fn command(&self, name: &str, args: &[&str]) -> Result<()> { - let mut cmd = name.to_owned(); + fn escape(input: &str) -> String { + input.replace('"', "\\\"") + } + + let mut cmd = escape(name); for elem in args { cmd.push(' '); - cmd.push_str(elem); + cmd.push('"'); + cmd.push_str(&escape(elem)); + cmd.push('"'); } + debug!("Running mpv command: '{}'", cmd); let raw = CString::new(cmd)?; mpv_err((), unsafe { |