pub fn exec_process<F>(cmd: Command, f: F) -> CmdExpand description
Creates a command that executes an external process.
This command spawns an external process asynchronously and returns a message produced by the provided closure with the process’s output. The process runs in the background and doesn’t block the UI.
§Arguments
cmd- Thestd::process::Commandto executef- A closure that processes the command output and returns aMsg
§Returns
A command that executes the external process
§Examples
use bubbletea_rs::{command, Model, Msg};
use std::process::Command;
#[derive(Debug)]
struct GitStatusMsg(String);
struct MyModel {
git_status: String,
}
impl Model for MyModel {
fn init() -> (Self, Option<command::Cmd>) {
let model = Self { git_status: String::new() };
// Run git status command
let mut cmd = Command::new("git");
cmd.arg("status").arg("--short");
let exec_cmd = command::exec_process(cmd, |result| {
match result {
Ok(output) => {
let status = String::from_utf8_lossy(&output.stdout).to_string();
Box::new(GitStatusMsg(status)) as Msg
}
Err(e) => {
Box::new(GitStatusMsg(format!("Error: {}", e))) as Msg
}
}
});
(model, Some(exec_cmd))
}
fn update(&mut self, msg: Msg) -> Option<command::Cmd> {
if let Some(GitStatusMsg(status)) = msg.downcast_ref::<GitStatusMsg>() {
self.git_status = status.clone();
}
None
}
fn view(&self) -> String {
format!("Git status:\n{}", self.git_status)
}
}