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

Implement a more robust algorithm for opening web links on unices #93

Merged
merged 4 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions webbrowser/open_browser_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package webbrowser

import "os/exec"

// Open opens `url` in default system browser.
func Open(url string) (string, error) {
err := exec.Command("open", url).Start()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions webbrowser/open_browser_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package webbrowser

import "fmt"

// Open opens `url` in default system browser, but not on this OS.
func Open(url string) (string, error) {
return "", fmt.Errorf("unsupported OS for default HTTP handling. Set a command in the config")
}
55 changes: 34 additions & 21 deletions webbrowser/open_browser_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,40 @@ import (
"os/exec"
)

// OpenInBrowser checks for the presence of a display server
// and environment variables indicating a gui is present. If found
// then xdg-open is called on a url to open said url in the default
// gui web browser for the system
// Open opens `url` in default system browser. It tries to do so in two
// ways (xdg-open and $BROWSER). It only works if there is a display
// server working.
//
// bouncepaw: I tried to support TTYs as well. The idea was to open
// a browser in foreground and return back to amfora after the browser
// is closed. While all browsers I tested opened correctly (w3m, lynx),
// I couldn't make it restore amfora correctly. The screen always ended
// up distorted. None of my stunts with altscreen buffers helped.
func Open(url string) (string, error) {
disp := os.Getenv("DISPLAY")
wayland := os.Getenv("WAYLAND_DISPLAY")
_, err := exec.LookPath("Xorg")
if disp == "" && wayland == "" && err != nil {
return "", fmt.Errorf("no gui is available")
var (
// In prev versions there was also Xorg executable checked for.
// I don't see any reason to check for it.
xorgDisplay = os.Getenv("DISPLAY")
waylandDisplay = os.Getenv("WAYLAND_DISPLAY")
xdgOpenPath, xdgOpenNotFoundErr = exec.LookPath("xdg-open")
envBrowser = os.Getenv("BROWSER")
)
switch {
case xorgDisplay == "" && waylandDisplay == "":
return "", fmt.Errorf("no display server was found")
case xdgOpenNotFoundErr == nil: // Prefer xdg-open over $BROWSER
// Use start rather than run or output in order
// to make browser running in background.
if err := exec.Command(xdgOpenPath, url).Start(); err != nil {
return "", err
}
return "Opened in system default web browser", nil
case envBrowser != "":
if err := exec.Command(xdgOpenPath, url).Start(); err != nil {
bouncepaw marked this conversation as resolved.
Show resolved Hide resolved
return "", err
}
return "Opened in system default web browser", nil
default:
return "", fmt.Errorf("could not determine system browser")
}

_, err = exec.LookPath("xdg-open")
if err != nil {
return "", fmt.Errorf("xdg-open command not found, cannot open in web browser")
}
// Use start rather than run or output in order
// to release the process and not block
err = exec.Command("xdg-open", url).Start()
if err != nil {
return "", err
}
return "Opened in system default web browser", nil
}
1 change: 1 addition & 0 deletions webbrowser/open_browser_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package webbrowser

import "os/exec"

// Open opens `url` in default system browser.
func Open(url string) (string, error) {
err := exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
if err != nil {
Expand Down