| | |
| | |
| | |
| |
|
| | |
| | package browser |
| |
|
| | import ( |
| | "os" |
| | "os/exec" |
| | "runtime" |
| | "time" |
| | ) |
| |
|
| | |
| | func Commands() [][]string { |
| | var cmds [][]string |
| | if exe := os.Getenv("BROWSER"); exe != "" { |
| | cmds = append(cmds, []string{exe}) |
| | } |
| | switch runtime.GOOS { |
| | case "darwin": |
| | cmds = append(cmds, []string{"/usr/bin/open"}) |
| | case "windows": |
| | cmds = append(cmds, []string{"cmd", "/c", "start"}) |
| | default: |
| | if os.Getenv("DISPLAY") != "" { |
| | |
| | cmds = append(cmds, []string{"xdg-open"}) |
| | } |
| | } |
| | cmds = append(cmds, |
| | []string{"chrome"}, |
| | []string{"google-chrome"}, |
| | []string{"chromium"}, |
| | []string{"firefox"}, |
| | ) |
| | return cmds |
| | } |
| |
|
| | |
| | func Open(url string) bool { |
| | for _, args := range Commands() { |
| | cmd := exec.Command(args[0], append(args[1:], url)...) |
| | if cmd.Start() == nil && appearsSuccessful(cmd, 3*time.Second) { |
| | return true |
| | } |
| | } |
| | return false |
| | } |
| |
|
| | |
| | |
| | |
| | func appearsSuccessful(cmd *exec.Cmd, timeout time.Duration) bool { |
| | errc := make(chan error, 1) |
| | go func() { |
| | errc <- cmd.Wait() |
| | }() |
| |
|
| | select { |
| | case <-time.After(timeout): |
| | return true |
| | case err := <-errc: |
| | return err == nil |
| | } |
| | } |
| |
|