From 31415effdf5a9ced19934a681cb044a732174088 Mon Sep 17 00:00:00 2001 From: Tunglies Date: Mon, 29 Dec 2025 10:58:55 +0800 Subject: [PATCH] docs(shell): update example to include Encoding usage in `Command::spawn` (#3183) * docs(shell): update example to include Encoding usage in `Command::spawn` * docs(shell): update patch level to minor for Encoding usage in `Command::spawn` documentation * Update .changes/doc-to-Encoding-usage-in-shell.md Co-authored-by: Tony <68118705+Legend-Master@users.noreply.github.com> * docs(shell): update examples to demonstrate manual Encoding usage in command output * Update plugins/shell/src/process/mod.rs Co-authored-by: Tony <68118705+Legend-Master@users.noreply.github.com> * docs(shell): update example to use 'windows-1252' encoding in command output * Update plugins/shell/src/process/mod.rs Co-authored-by: Tony <68118705+Legend-Master@users.noreply.github.com> * docs(shell): update example command in documentation to use 'some-program' with 'some-arg' * Bump shell-js in change file * Fix indent --- .changes/doc-to-Encoding-usage-in-shell.md | 6 ++++ plugins/shell/src/process/mod.rs | 33 ++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 .changes/doc-to-Encoding-usage-in-shell.md 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;