diff --git a/.changes/doc-to-Encoding-usage-in-shell.md b/.changes/doc-to-Encoding-usage-in-shell.md new file mode 100644 index 000000000..1efefb151 --- /dev/null +++ b/.changes/doc-to-Encoding-usage-in-shell.md @@ -0,0 +1,6 @@ +--- +shell: patch +shell-js: patch +--- + +Docs on example to Encoding usage in `Command::spawn`. No user facing changes. diff --git a/plugins/shell/src/process/mod.rs b/plugins/shell/src/process/mod.rs index 6f350c181..0c80f12d0 100644 --- a/plugins/shell/src/process/mod.rs +++ b/plugins/shell/src/process/mod.rs @@ -242,7 +242,9 @@ impl Command { /// .setup(|app| { /// let handle = app.handle().clone(); /// tauri::async_runtime::spawn(async move { - /// let (mut rx, mut child) = handle.shell().command("cargo") + /// let (mut rx, mut child) = handle + /// .shell() + /// .command("cargo") /// .args(["tauri", "dev"]) /// .spawn() /// .expect("Failed to spawn cargo"); @@ -260,7 +262,34 @@ impl Command { /// } /// }); /// Ok(()) - /// }); + /// }); + /// ``` + /// + /// Depending on the command you spawn, it might output in a specific encoding, to parse the output lines in this case: + /// + /// ```rust,no_run + /// use tauri_plugin_shell::{process::{CommandEvent, Encoding}, ShellExt}; + /// tauri::Builder::default() + /// .setup(|app| { + /// let handle = app.handle().clone(); + /// tauri::async_runtime::spawn(async move { + /// let (mut rx, mut child) = handle + /// .shell() + /// .command("some-program") + /// .arg("some-arg") + /// .spawn() + /// .expect("Failed to spawn some-program"); + /// + /// let encoding = Encoding::for_label(b"windows-1252").unwrap(); + /// while let Some(event) = rx.recv().await { + /// if let CommandEvent::Stdout(line) = event { + /// let (decoded, _, _) = encoding.decode(&line); + /// println!("got: {decoded}"); + /// } + /// } + /// }); + /// Ok(()) + /// }); /// ``` pub fn spawn(self) -> crate::Result<(Receiver, CommandChild)> { let raw = self.raw_out;