diff --git a/http/http_render.go b/http/http_render.go index ea2e987..f68ee2d 100644 --- a/http/http_render.go +++ b/http/http_render.go @@ -8,12 +8,16 @@ import ( "strings" ) -type page struct { +// Page represents the contents and links or an http/https document +type Page struct { Content string Links []string } -func Visit(webmode, url string, width int) (page, error) { +// Visit is the main entry to viewing a web document in bombadillo. +// It takes a url, a terminal width, and which web backend the user +// currently has set. Visit returns a Page and an error +func Visit(webmode, url string, width int) (Page, error) { if width > 80 { width = 80 } @@ -26,17 +30,18 @@ func Visit(webmode, url string, width int) (page, error) { case "elinks": w = "-dump-width" default: - return page{}, fmt.Errorf("Invalid webmode setting") + return Page{}, fmt.Errorf("Invalid webmode setting") } c, err := exec.Command(webmode, "-dump", w, fmt.Sprintf("%d", width), url).Output() if err != nil { - return page{}, err + return Page{}, err } return parseLinks(string(c)), nil } -// Returns false on err or non-text type -// Else returns true +// IsTextFile makes an http(s) head request to a given URL +// and determines if the content-type is text based. It then +// returns a bool func IsTextFile(url string) bool { resp, err := http.Head(url) if err != nil { @@ -50,8 +55,8 @@ func IsTextFile(url string) bool { return false } -func parseLinks(c string) page { - var out page +func parseLinks(c string) Page { + var out Page contentUntil := strings.LastIndex(c, "References") if contentUntil >= 1 { out.Content = c[:contentUntil] @@ -74,6 +79,9 @@ func parseLinks(c string) page { return out } +// Fetch makes an http(s) request and returns the []bytes +// for the response and an error. Fetch is used for saving +// the source file of an http(s) document func Fetch(url string) ([]byte, error) { resp, err := http.Get(url) if err != nil { diff --git a/http/open_browser_linux.go b/http/open_browser_linux.go index 5be7640..3b7dfee 100644 --- a/http/open_browser_linux.go +++ b/http/open_browser_linux.go @@ -2,21 +2,27 @@ package http -import "os/exec" +import ( + "fmt" + "os" + "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 func OpenInBrowser(url string) (string, error) { - // Check for a local display server, this is - // not a silver bullet but should help ssh - // connected users on many systems get accurate - // messaging and not spin off processes needlessly - err := exec.Command("type", "Xorg").Run() - if err != nil { + 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, check 'webmode' setting") } - // Use start rather than run or output in order - // to release the process and not block - err := exec.Command("xdg-open", url).Start() + // 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 }