Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does open URL work? #323

Open
ephemer4l opened this issue Oct 26, 2024 · 4 comments
Open

How does open URL work? #323

ephemer4l opened this issue Oct 26, 2024 · 4 comments

Comments

@ephemer4l
Copy link
Contributor

I have just discovered that pressing enter on a selected URL message is supposed to open said URL. Unfortunately, this does not work on my system but I am not getting any error messages. How does gurk URL opening work under the hood? I am guessing that my system is missing some requirements for this.

@boxdot
Copy link
Owner

boxdot commented Oct 26, 2024

This is done here

if let Err(error) = opener::open(url) {
. We use this crate for opening urls: https://docs.rs/opener/latest/opener/.

You could try to replace this open::open call by https://docs.rs/opener/latest/opener/fn.open_browser.html. Aparently, it will use then the default browser.

@ephemer4l
Copy link
Contributor Author

ephemer4l commented Oct 26, 2024

Thanks a lot for the quick response. Honestly, I can't figure out how opener really uses xdg-open. I have a custom shell script called xdg-open that I use to open files but it seems that this does not work.

They are using

fn open_with_system_xdg_open(path: &OsStr) -> io::Result<Child> {
    Command::new("xdg-open")
        .arg(path)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
}

to open the file at first. This should just launch my custom script but it doesn't for some reason.

Edit: Even weirder thing is that the opener crate works fine when I try it on its own.

@boxdot
Copy link
Owner

boxdot commented Oct 26, 2024

Let's try to debug your script then. You can apply this patch

diff --git a/src/app.rs b/src/app.rs
index ecb7a27..4d4e0c4 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -1647,12 +1647,29 @@ fn to_emoji(s: &str) -> Option<&str> {
 }
 
 fn open_url(message: &Message, url_regex: &Regex) -> Option<()> {
+    use std::io::Write;
+
     let text = message.message.as_ref()?;
     let m = url_regex.find(text)?;
     let url = m.as_str();
-    if let Err(error) = opener::open(url) {
+
+    let mut file = std::fs::OpenOptions::new()
+        .create(true)
+        .append(true)
+        .open("/tmp/open.log")
+        .unwrap();
+    writeln!(&mut file, "opening {url}").unwrap();
+
+    if let Err(error) = std::process::Command::new("xdg-open")
+        .arg(url)
+        .stdin(std::process::Stdio::null())
+        .stdout(file.try_clone().unwrap())
+        .stderr(file.try_clone().unwrap())
+        .spawn()
+    {
         error!(url, %error, "failed to open");
     }
+
     Some(())
 }

When opening an url, it will write the stdout/stderr of xdg-open command to /tmp/open.log. You can then check what is going on there.

Just a side note: the url regex is sometimes broken for some reason. So, try it with a simple url.
Actually, this is not a regex bug, but an offset bug when opening an url behind a date separator.

@ephemer4l
Copy link
Contributor Author

ephemer4l commented Oct 26, 2024

Sorry for the late response, unfortunately I am unable to test this because for some reason I can't compile zerocopy when trying to build with cargo build. Weirdly enough this works when I am using cargo install.

Edit: --release works, testing now. False alarm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants