From 5213c3ffbd8afcdaa0eaa8df4a11f33d38584987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Dzivjak?= Date: Sat, 4 Feb 2023 11:06:00 +0000 Subject: [PATCH] open files inside helix --- Cargo.lock | 4 ++-- helix-term/Cargo.toml | 2 +- helix-term/src/commands.rs | 40 +++++++++++++++++++++++++++----------- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf89c11edff32..2d9fdafa8f046 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1577,9 +1577,9 @@ checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" [[package]] name = "open" -version = "3.4.0" +version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21ecf2487e799604735754d2c5896106785987b441b5aee58f242e4d4963179a" +checksum = "bd61e3bf9d78956c72ee864bba52431f7f43994b21a17e9e72596a81bd61075b" dependencies = [ "pathdiff", ] diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml index 665ace0056f7d..f07df6b95481c 100644 --- a/helix-term/Cargo.toml +++ b/helix-term/Cargo.toml @@ -56,7 +56,7 @@ pulldown-cmark = { version = "0.9", default-features = false } # file type detection content_inspector = "0.2.4" # openning URLs -open = "3.2.0" +open = "4.0.0" # config toml = "0.7" diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 5506c36f349e2..3659df9898261 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -62,7 +62,7 @@ use crate::{ use crate::job::{self, Jobs}; use futures_util::StreamExt; -use std::{collections::HashMap, fmt, future::Future}; +use std::{collections::HashMap, fmt, future::Future, io}; use std::{collections::HashSet, num::NonZeroUsize}; use std::{ @@ -1144,17 +1144,35 @@ fn goto_file_impl(cx: &mut Context, action: Action) { let p = sel.trim(); if !p.is_empty() { if let Ok(url) = Url::parse(p) { - if let Err(e) = open::that(url.as_str()) { - cx.editor.set_error(format!( - "Open file failed for url '{}': {:?}", - url.as_str(), - e - )); + // file:// urls are opened inside helix + if url.scheme() != "file" { + let commands = open::commands(url.as_str()); + cx.jobs.callback(async { + let mut error: Option = None; + for cmd in commands { + let mut command = tokio::process::Command::new(cmd.get_program()); + command.args(cmd.get_args()); + match command.output().await { + Ok(_) => return Ok(job::Callback::Editor(Box::new(|_| {}))), + Err(e) => { + error = Some(e); + } + }; + } + match error { + Some(e) => Ok(job::Callback::Editor(Box::new(move |editor| { + editor.set_error(format!("Open url failed: {:?}", e)) + }))), + None => Ok(job::Callback::Editor(Box::new(move |editor| { + editor.set_error(format!("Open file failed: no command found")) + }))), + } + }); + return; + } + if let Err(e) = cx.editor.open(&PathBuf::from(p), action) { + cx.editor.set_error(format!("Open file failed: {:?}", e)); } - return; - } - if let Err(e) = cx.editor.open(&PathBuf::from(p), action) { - cx.editor.set_error(format!("Open file failed: {:?}", e)); } } }