From ee0f2e3e9b58147a9378cfe63cd5c8a2d83f2a18 Mon Sep 17 00:00:00 2001 From: Justin Overfelt Date: Mon, 6 May 2019 18:33:18 -0400 Subject: [PATCH] change openBrowser to use conditional compilation rather than a runtime switch --- cui/cui.go | 5 ++++- gopher/bookmark.go | 4 ---- gopher/gopher.go | 30 ++++-------------------------- gopher/open_browser_darwin.go | 9 +++++++++ gopher/open_browser_linux.go | 9 +++++++++ gopher/open_browser_other.go | 11 +++++++++++ gopher/open_browser_windows.go | 9 +++++++++ gopher/url.go | 6 ++++-- gopher/view.go | 2 +- 9 files changed, 51 insertions(+), 34 deletions(-) create mode 100644 gopher/open_browser_darwin.go create mode 100644 gopher/open_browser_linux.go create mode 100644 gopher/open_browser_other.go create mode 100644 gopher/open_browser_windows.go diff --git a/cui/cui.go b/cui/cui.go index 7b7d49c..a851764 100644 --- a/cui/cui.go +++ b/cui/cui.go @@ -163,5 +163,8 @@ func HandleAlternateScreen(opt string) { cmd := exec.Command("tput", opt) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout - cmd.Run() + err := cmd.Run() + if err != nil { + panic(err) + } } diff --git a/gopher/bookmark.go b/gopher/bookmark.go index 88d39ba..24ac80f 100644 --- a/gopher/bookmark.go +++ b/gopher/bookmark.go @@ -63,7 +63,3 @@ func (b Bookmarks) IniDump() string { } return out } - -func MakeBookmarks() Bookmarks { - return Bookmarks{[]string{}, []string{}} -} diff --git a/gopher/gopher.go b/gopher/gopher.go index de8535e..41cca9a 100644 --- a/gopher/gopher.go +++ b/gopher/gopher.go @@ -8,8 +8,6 @@ import ( "fmt" "io/ioutil" "net" - "os/exec" - "runtime" "strings" "time" ) @@ -90,10 +88,11 @@ func Visit(addr, openhttp string) (View, error) { if u.Gophertype == "h" { if res, tf := isWebLink(u.Resource); tf && strings.ToUpper(openhttp) == "TRUE" { - err := openbrowser(res) + err := openBrowser(res) if err != nil { return View{}, err } + return View{}, fmt.Errorf("") } } @@ -113,12 +112,12 @@ func Visit(addr, openhttp string) (View, error) { return MakeView(u, pageContent), nil } -func GetType(t string) string { +func getType(t string) string { if val, ok := types[t]; ok { return val } - return "???" + return "???" } func isWebLink(resource string) (string, bool) { @@ -128,24 +127,3 @@ func isWebLink(resource string) (string, bool) { } return "", false } - -func openbrowser(url string) error { - // gist.github.com/hyg/9c4afcd91fe24316cbf0 - var err error - - switch runtime.GOOS { - case "linux": - err = exec.Command("xdg-open", url).Start() - case "windows": - err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() - case "darwin": - err = exec.Command("open", url).Start() - default: - err = fmt.Errorf("Unsupported os for browser detection") - } - if err != nil { - return err - } - - return nil -} diff --git a/gopher/open_browser_darwin.go b/gopher/open_browser_darwin.go new file mode 100644 index 0000000..edafe36 --- /dev/null +++ b/gopher/open_browser_darwin.go @@ -0,0 +1,9 @@ +// +build darwin + +package gopher + +import "os/exec" + +func openBrowser(url string) error { + return exec.Command("open", url).Start() +} diff --git a/gopher/open_browser_linux.go b/gopher/open_browser_linux.go new file mode 100644 index 0000000..2ce35c9 --- /dev/null +++ b/gopher/open_browser_linux.go @@ -0,0 +1,9 @@ +// +build linux + +package gopher + +import "os/exec" + +func openBrowser(url string) error { + return exec.Command("xdg-open", url).Start() +} diff --git a/gopher/open_browser_other.go b/gopher/open_browser_other.go new file mode 100644 index 0000000..1659ea3 --- /dev/null +++ b/gopher/open_browser_other.go @@ -0,0 +1,11 @@ +// +build !linux +// +build !darwin +// +build !windows + +package gopher + +import "fmt" + +func openBrowser(url string) error { + return fmt.Errorf("Unsupported os for browser detection") +} diff --git a/gopher/open_browser_windows.go b/gopher/open_browser_windows.go new file mode 100644 index 0000000..b57c9d6 --- /dev/null +++ b/gopher/open_browser_windows.go @@ -0,0 +1,9 @@ +// +build windows + +package gopher + +import "os/exec" + +func openBrowser(url string) error { + return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() +} diff --git a/gopher/url.go b/gopher/url.go index fd467f4..6a0e941 100644 --- a/gopher/url.go +++ b/gopher/url.go @@ -57,13 +57,15 @@ func MakeUrl(u string) (Url, error) { } if out.Host == "" { - return out, errors.New("No host.") + return out, errors.New("no host") } if out.Scheme == "gopher" && out.Port == "" { out.Port = "70" - } else if out.Scheme == "http" || out.Scheme == "https" && out.Port == "" { + } else if out.Scheme == "http" && out.Port == "" { out.Port = "80" + } else if out.Scheme == "https" && out.Port == "" { + out.Port = "443" } if out.Gophertype == "" && (out.Resource == "" || out.Resource == "/") { diff --git a/gopher/view.go b/gopher/view.go index 90ee256..3afb745 100644 --- a/gopher/view.go +++ b/gopher/view.go @@ -52,7 +52,7 @@ func (v *View) ParseMap() { } else if len(line) >= 4 { fulllink := fmt.Sprintf("%s:%s/%s%s", line[2], line[3], string(line[0][0]), line[1]) v.Links = append(v.Links, fulllink) - linktext := fmt.Sprintf("(%s) %2d %s", GetType(string(line[0][0])), len(v.Links), title) + linktext := fmt.Sprintf("(%s) %2d %s", getType(string(line[0][0])), len(v.Links), title) v.Content[i] = linktext } }