Skip to content

Commit

Permalink
open files inside helix
Browse files Browse the repository at this point in the history
  • Loading branch information
matoous committed Mar 10, 2023
1 parent da4e5cf commit 5213c3f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion helix-term/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 29 additions & 11 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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<io::Error> = 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));
}
}
}
Expand Down

0 comments on commit 5213c3f

Please sign in to comment.