2023-12-09 22:59:31 +00:00
|
|
|
//go:build !js
|
2020-01-02 01:50:15 +00:00
|
|
|
// +build !js
|
|
|
|
|
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
2020-11-21 07:35:37 +00:00
|
|
|
"fmt"
|
2020-01-02 01:50:15 +00:00
|
|
|
"os/exec"
|
2020-11-21 07:35:37 +00:00
|
|
|
"path/filepath"
|
2020-01-02 01:50:15 +00:00
|
|
|
"runtime"
|
|
|
|
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
|
2020-01-02 01:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// OpenURL opens a web browser to the given URL.
|
|
|
|
//
|
|
|
|
// On Linux this will look for xdg-open or try a few common browser names.
|
2023-12-09 22:59:31 +00:00
|
|
|
// On Windows this uses the “start“ command.
|
|
|
|
// On MacOS this uses the “open“ command.
|
2020-01-02 01:50:15 +00:00
|
|
|
func OpenURL(url string) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
go windowsOpenURL(url)
|
|
|
|
} else if runtime.GOOS == "linux" {
|
|
|
|
go linuxOpenURL(url)
|
|
|
|
} else if runtime.GOOS == "darwin" {
|
|
|
|
go macOpenURL(url)
|
|
|
|
} else {
|
|
|
|
log.Error("OpenURL: don't know how to open URLs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 07:35:37 +00:00
|
|
|
// OpenLocalURL opens a web browser to a local HTML path.
|
|
|
|
// It converts a relative path like "guidebook/index.html" to its absolute
|
|
|
|
// filesystem location.
|
|
|
|
func OpenLocalURL(path string) {
|
|
|
|
abs, _ := filepath.Abs(path)
|
|
|
|
url := fmt.Sprintf("file:///%s", abs)
|
|
|
|
fmt.Printf("Open Local URL: %s\n", url)
|
|
|
|
OpenURL(url)
|
|
|
|
}
|
|
|
|
|
2020-01-02 01:50:15 +00:00
|
|
|
func windowsOpenURL(url string) {
|
|
|
|
_, err := exec.Command("start", url).Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("native.windowsOpenURL(%s): %s", url, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func macOpenURL(url string) {
|
|
|
|
_, err := exec.Command("open", url).Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("native.macOpenURL(%s): %s", url, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func linuxOpenURL(url string) {
|
|
|
|
// Commands to look for.
|
|
|
|
var commands = []string{
|
|
|
|
"xdg-open",
|
|
|
|
"firefox",
|
|
|
|
"google-chrome",
|
|
|
|
"chromium-browser",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, command := range commands {
|
|
|
|
log.Debug("OpenURL(linux): try %s %s", command, url)
|
|
|
|
_, err := exec.Command(command, url).Output()
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Error(
|
|
|
|
"native.linuxOpenURL(%s): could not find browser executable, tried %+v",
|
|
|
|
url, commands,
|
|
|
|
)
|
|
|
|
}
|