#rust sidecar - wanna make a sidecar that I can handle on rust side without event

10 messages · Page 1 of 1 (latest)

fast field
#

I can now understand that the tauri docs are made with event in mind so that each line output is a event but can I handle it myself in rust side?

#
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="stylesheet" href="styles.css" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Tauri App</title>
    <script type="module" src="/main.js" defer></script>
    <style>
      .logo.vanilla:hover {
        filter: drop-shadow(0 0 2em #ffe21c);
      }
    </style>
  </head>

  <body>
    <div class="container">
      <h1>Welcome to Tauri!</h1>

      <div class="row">
        <a href="https://tauri.app" target="_blank">
          <img src="/assets/tauri.svg" class="logo tauri" alt="Tauri logo" />
        </a>
        <a
          href="https://developer.mozilla.org/en-US/docs/Web/JavaScript"
          target="_blank"
        >
          <img
            src="/assets/javascript.svg"
            class="logo vanilla"
            alt="JavaScript logo"
          />
        </a>
      </div>

      <p>Click on the Tauri logo to learn more about the framework</p>

      <form class="row" id="greet-form">
        <input id="greet-input" placeholder="Enter a name..." />
        <button type="submit">Greet</button>
      </form>

      <p id="greet-msg"></p>
      
      <form class="row" id="rusty-form">
        <input id="args" placeholder="-V"/>
        <button id="run-sidecar-button">Run Sidecar Rusty Torrent</button>
      </form>
      <p id="rusty-torrent-output"></p>
    </div>
  </body>
</html>
#
const { invoke } = window.__TAURI__.tauri

const { Command } = window.__TAURI__.shell


let greetInputEl;
let greetMsgEl;
let rustyOutputMsgEl;
let rustyInput;
//let rustyTorrent;
let rustyOutput;


async function greet() {
  // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
  greetMsgEl.textContent = await invoke("greet", { name: greetInputEl.value});
}
async function fn_rusty_torrent() {
    rustyOutput =  await invoke("spawn_sidecar");
  console.log(rustyOutput);
   //let rustyOutput = await rustyTorrent.execute();
   //console.log(rustyOutput);
   rustyOutputMsgEl.innerHTML = "<p>stdout: «" + rustyOutput.stdout + "»</p>";
   rustyOutputMsgEl.innerHTML += "<p>stderr: «" + rustyOutput.stderr + "»</p>";
   rustyOutputMsgEl.innerHTML += "<p>code: «" + rustyOutput.code + "»</p>";
   rustyOutputMsgEl.innerHTML += "<p>signal: «" + rustyOutput.signal + "»</p>";
}



window.addEventListener("DOMContentLoaded", () => {
  greetInputEl = document.querySelector("#greet-input");
  greetMsgEl = document.querySelector("#greet-msg");
  rustyOutputMsgEl = document.querySelector("#rusty-torrent-output");
  
  rustyInput = document.querySelector("#args");
  
  document.querySelector("#greet-form").addEventListener("submit", (e) => {
    e.preventDefault();
    greet();
  });
  document.querySelector("#rusty-form").addEventListener(("submit"), (e) => {
    e.preventDefault();
    //rustyTorrent = Command.sidecar('binaries/rusty_torrent')
    fn_rusty_torrent();
  });
});
#
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tauri::api::process::{Command,CommandEvent};



// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet(name: &str) -> String {
  format!("Hello, {}!", name)
}


#[tauri::command]
async fn spawn_sidecar(/*text: &str*/) -> String {
  match Command::new_sidecar("rusty_torrent") {
    Ok(sidecar) => {
      match sidecar.spawn() {
        Ok((mut rx, mut child)) => {
          child.write("message from Rust\n".as_bytes()).expect("Failed to write to sidecar");
          drop(child);
          let mut output = String::new();
          while let Some(event) = rx.recv().await {
            if let CommandEvent::Stdout(line) = event {
              output.push_str(&line);
            }
          }
          format!("{}", output)
        },
        Err(e) => { 
          println!("error spawning `rusty_torrent` sidecar: {}", e);
          format!("{}", e)
        }
      }
    },
    Err(e) => {
      println!("error creating `rusty_torrent` sidecar: {}", e);
      format!("{}",e)
    }
  }
}

fn main() {
  tauri::Builder::default()
    .invoke_handler(tauri::generate_handler![greet,spawn_sidecar])
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}
#
{
  "build": {
    "beforeDevCommand": "",
    "beforeBuildCommand": "",
    "devPath": "../src",
    "distDir": "../src",
    "withGlobalTauri": true
  },
  "package": {
    "productName": "FreeBeerF",
    "version": "0.0.1"
  },
  "tauri": {
    "allowlist": {
      "all": false,
      "shell": {
        "all": false,
        "sidecar": true,
        "scope": [
          { "name": "binaries/rusty_torrent", "sidecar": true}
        ]
      }
    },
    "bundle": {
      "active": true,
      "targets": "all",
      "identifier": "com.tauri.dev",
      "icon": [
        "icons/32x32.png",
        "icons/128x128.png",
        "icons/[email protected]",
        "icons/icon.icns",
        "icons/icon.ico"
      ],
      "externalBin": [
        "binaries/rusty_torrent"
      ]
    },
    "security": {
      "csp": "default-src 'self'"
    },
    "windows": [
      {
        "fullscreen": false,
        "resizable": true,
        "title": "FreeBeerF",
        "width": 800,
        "height": 600
      }
    ]
  }
}
#

so with this code I am not getting the output

#

@wooden ember could you take a look at this?

#

if I add .args(["-V"]) just before spawn I get output

dusk palm
#

Your code only returns the output once the sidecar is done executing. Judging by the name "rusty_torrent" that sounds like this could take a while, right?

#

And you're right, that's why the docs focus on events. Events would allow you to get the output while the sidecar is running, and not just when it's done.