From 28c2d6b277a7001ceffb5eed64d71e62b2b17590 Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Wed, 13 Nov 2019 22:23:57 -0800 Subject: [PATCH] Diversifies the web rendering backends and removes confusing configuration options --- client.go | 41 ++++++++++----------------- defaults.go | 4 +-- http/{lynx_mode.go => http_render.go} | 37 ++++++++++++------------ main.go | 1 + 4 files changed, 35 insertions(+), 48 deletions(-) rename http/{lynx_mode.go => http_render.go} (62%) diff --git a/client.go b/client.go index b590755..fa9f1ca 100644 --- a/client.go +++ b/client.go @@ -643,7 +643,7 @@ func (c *client) search(query, url, question string) { c.DrawMessage() return } else if strings.TrimSpace(entry) == "" { - c.ClearMessage() + c.ClearMessage() c.DrawMessage() return } @@ -995,17 +995,11 @@ func (c *client) handleFinger(u Url) { } func (c *client) handleWeb(u Url) { - // Following http is disabled - if strings.ToUpper(c.Options["openhttp"]) != "TRUE" { - c.SetMessage("'openhttp' is not set to true, cannot open web link", false) - c.DrawMessage() - return - } - - // Use lynxmode - if strings.ToUpper(c.Options["lynxmode"]) == "TRUE" { + wm := strings.ToLower(c.Options["webmode"]) + switch wm { + case "lynx", "w3m", "elinks": if http.IsTextFile(u.Full) { - page, err := http.Visit(u.Full, c.Width-1) + page, err := http.Visit(wm, u.Full, c.Width-1) if err != nil { c.SetMessage(fmt.Sprintf("Lynx error: %s", err.Error()), true) c.DrawMessage() @@ -1029,23 +1023,18 @@ func (c *client) handleWeb(u Url) { } c.saveFile(u, fn) } - - // Open in default web browser if available - } else { - if strings.ToUpper(c.Options["terminalonly"]) == "TRUE" { - c.SetMessage("'terminalonly' is set to true and 'lynxmode' is not enabled, cannot open web link", false) - c.DrawMessage() + case "gui": + c.SetMessage("Attempting to open in gui web browser", false) + c.DrawMessage() + msg, err := http.OpenInBrowser(u.Full) + if err != nil { + c.SetMessage(err.Error(), true) } else { - c.SetMessage("Attempting to open in web browser", false) - c.DrawMessage() - msg, err := http.OpenInBrowser(u.Full) - if err != nil { - c.SetMessage(err.Error(), true) - } else { - c.SetMessage(msg, false) - } - c.DrawMessage() + c.SetMessage(msg, false) } + default: + c.SetMessage("Current 'webmode' setting does not allow http/https", false) + c.DrawMessage() } } diff --git a/defaults.go b/defaults.go index 9da05a5..c14db8c 100644 --- a/defaults.go +++ b/defaults.go @@ -48,14 +48,12 @@ var defaultOptions = map[string]string{ "homeurl": "gopher://bombadillo.colorfield.space:70/1/user-guide.map", "savelocation": homePath(), "searchengine": "gopher://gopher.floodgap.com:70/7/v2/vs", - "openhttp": "false", "telnetcommand": "telnet", "configlocation": xdgConfigPath(), "theme": "normal", // "normal", "inverted" - "terminalonly": "true", "tlscertificate": "", "tlskey": "", - "lynxmode": "false", + "webmode": "none", // "none", "gui", "lynx", "w3m" } // homePath will return the path to your home directory as a string diff --git a/http/lynx_mode.go b/http/http_render.go similarity index 62% rename from http/lynx_mode.go rename to http/http_render.go index 782aee3..ea2e987 100644 --- a/http/lynx_mode.go +++ b/http/http_render.go @@ -13,12 +13,22 @@ type page struct { Links []string } -func Visit(url string, width int) (page, error) { +func Visit(webmode, url string, width int) (page, error) { if width > 80 { width = 80 } - w := fmt.Sprintf("-width=%d", width) - c, err := exec.Command("lynx", "-dump", w, url).Output() + var w string + switch webmode { + case "lynx": + w = "-width" + case "w3m": + w = "-cols" + case "elinks": + w = "-dump-width" + default: + 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 } @@ -28,26 +38,16 @@ func Visit(url string, width int) (page, error) { // Returns false on err or non-text type // Else returns true func IsTextFile(url string) bool { - c, err := exec.Command("lynx", "-dump", "-head", url).Output() + resp, err := http.Head(url) if err != nil { return false } - content := string(c) - content = strings.ToLower(content) - headers := strings.Split(content, "\n") - for _, header := range headers { - if strings.Contains(header, "content-type:") && strings.Contains(header, "text") { - return true - } else if strings.Contains(header, "content-type:") { - return false - } + ctype := resp.Header.Get("content-type") + if strings.Contains(ctype, "text") || ctype == "" { + return true } - // If we made it here, there is no content-type header. - // So in the event of the unknown, lets render to the - // screen. This will allow redirects to get rendered - // as well. - return true + return false } func parseLinks(c string) page { @@ -72,7 +72,6 @@ func parseLinks(c string) page { out.Links = append(out.Links, strings.TrimSpace(ls[1])) } return out - } func Fetch(url string) ([]byte, error) { diff --git a/main.go b/main.go index 4b644bb..84627c8 100644 --- a/main.go +++ b/main.go @@ -66,6 +66,7 @@ func saveConfig() error { func validateOpt(opt, val string) bool { var validOpts = map[string][]string{ + "webmode": []string{"none", "gui", "lynx", "w3m", "elinks"}, "openhttp": []string{"true", "false"}, "theme": []string{"normal", "inverse"}, "terminalonly": []string{"true", "false"},