diff --git a/src/interpreter/flamegraph.rs b/src/interpreter/flamegraph.rs index 79b114c..c1c559f 100644 --- a/src/interpreter/flamegraph.rs +++ b/src/interpreter/flamegraph.rs @@ -1,4 +1,5 @@ use crate::interpreter::clickhouse::Columns; +use crate::utils::open_url_command; use anyhow::{Error, Result}; use crossterm::event::{self, Event as CrosstermEvent, KeyEventKind}; use flamelens::app::{App, AppResult}; @@ -9,7 +10,6 @@ use futures::channel::mpsc; use ratatui::backend::CrosstermBackend; use ratatui::Terminal; use std::io; -use std::process::{Command, Stdio}; use tokio::time::{sleep, Duration}; use urlencoding::encode; use warp::http::header::{HeaderMap, HeaderValue}; @@ -116,13 +116,11 @@ pub async fn open_in_speedscope(block: Columns) -> Result<()> { // NOTE: here we need a webserver, since we cannot use localProfilePath due to browser // policies - let mut child = Command::new("xdg-open") - .arg(format!( - "https://www.speedscope.app/#profileURL={}", - encode(&format!("http://{}/", bind_address)) - )) - .stderr(Stdio::null()) - .stdout(Stdio::null()) + let url = format!( + "https://www.speedscope.app/#profileURL={}", + encode(&format!("http://{}/", bind_address)) + ); + let mut child = open_url_command(&url) .spawn() .map_err(|e| Error::msg(format!("Cannot find/execute xdg-open ({})", e)))?; diff --git a/src/utils.rs b/src/utils.rs index 28dae32..0287fbd 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -108,16 +108,30 @@ pub fn edit_query(query: &String, settings: &HashMap) -> Result< return Ok(query); } +pub fn open_url_command(url: &str) -> Command { + let mut cmd = if cfg!(target_os = "windows") { + let mut c = Command::new("cmd"); + c.args(["/C", "start", "", url]); // "" to avoid stealing the first quoted argument as window title + c + } else if cfg!(target_os = "macos") { + let mut c = Command::new("open"); + c.arg(url); + c + } else { + let mut c = Command::new("xdg-open"); + c.arg(url); + c + }; + + cmd.stderr(Stdio::null()).stdout(Stdio::null()); + cmd +} + pub fn open_graph_in_browser(graph: String) -> Result<()> { - let graph = encode(&graph); - Command::new("xdg-open") - .arg(format!( - "https://dreampuf.github.io/GraphvizOnline/#{}", - graph - )) - // NOTE: avoid breaking of the chdig rendering (though this hides errors...) - .stderr(Stdio::null()) - .stdout(Stdio::null()) - .status()?; + let url = format!( + "https://dreampuf.github.io/GraphvizOnline/#{}", + encode(&graph) + ); + open_url_command(&url).status()?; return Ok(()); }