Diversifies the web rendering backends and removes confusing configuration options

This commit is contained in:
sloumdrone 2019-11-13 22:23:57 -08:00
parent 0281458763
commit 28c2d6b277
4 changed files with 35 additions and 48 deletions

View File

@ -643,7 +643,7 @@ func (c *client) search(query, url, question string) {
c.DrawMessage() c.DrawMessage()
return return
} else if strings.TrimSpace(entry) == "" { } else if strings.TrimSpace(entry) == "" {
c.ClearMessage() c.ClearMessage()
c.DrawMessage() c.DrawMessage()
return return
} }
@ -995,17 +995,11 @@ func (c *client) handleFinger(u Url) {
} }
func (c *client) handleWeb(u Url) { func (c *client) handleWeb(u Url) {
// Following http is disabled wm := strings.ToLower(c.Options["webmode"])
if strings.ToUpper(c.Options["openhttp"]) != "TRUE" { switch wm {
c.SetMessage("'openhttp' is not set to true, cannot open web link", false) case "lynx", "w3m", "elinks":
c.DrawMessage()
return
}
// Use lynxmode
if strings.ToUpper(c.Options["lynxmode"]) == "TRUE" {
if http.IsTextFile(u.Full) { 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 { if err != nil {
c.SetMessage(fmt.Sprintf("Lynx error: %s", err.Error()), true) c.SetMessage(fmt.Sprintf("Lynx error: %s", err.Error()), true)
c.DrawMessage() c.DrawMessage()
@ -1029,23 +1023,18 @@ func (c *client) handleWeb(u Url) {
} }
c.saveFile(u, fn) c.saveFile(u, fn)
} }
case "gui":
// Open in default web browser if available c.SetMessage("Attempting to open in gui web browser", false)
} else { c.DrawMessage()
if strings.ToUpper(c.Options["terminalonly"]) == "TRUE" { msg, err := http.OpenInBrowser(u.Full)
c.SetMessage("'terminalonly' is set to true and 'lynxmode' is not enabled, cannot open web link", false) if err != nil {
c.DrawMessage() c.SetMessage(err.Error(), true)
} else { } else {
c.SetMessage("Attempting to open in web browser", false) c.SetMessage(msg, false)
c.DrawMessage()
msg, err := http.OpenInBrowser(u.Full)
if err != nil {
c.SetMessage(err.Error(), true)
} else {
c.SetMessage(msg, false)
}
c.DrawMessage()
} }
default:
c.SetMessage("Current 'webmode' setting does not allow http/https", false)
c.DrawMessage()
} }
} }

View File

@ -48,14 +48,12 @@ var defaultOptions = map[string]string{
"homeurl": "gopher://bombadillo.colorfield.space:70/1/user-guide.map", "homeurl": "gopher://bombadillo.colorfield.space:70/1/user-guide.map",
"savelocation": homePath(), "savelocation": homePath(),
"searchengine": "gopher://gopher.floodgap.com:70/7/v2/vs", "searchengine": "gopher://gopher.floodgap.com:70/7/v2/vs",
"openhttp": "false",
"telnetcommand": "telnet", "telnetcommand": "telnet",
"configlocation": xdgConfigPath(), "configlocation": xdgConfigPath(),
"theme": "normal", // "normal", "inverted" "theme": "normal", // "normal", "inverted"
"terminalonly": "true",
"tlscertificate": "", "tlscertificate": "",
"tlskey": "", "tlskey": "",
"lynxmode": "false", "webmode": "none", // "none", "gui", "lynx", "w3m"
} }
// homePath will return the path to your home directory as a string // homePath will return the path to your home directory as a string

View File

@ -13,12 +13,22 @@ type page struct {
Links []string Links []string
} }
func Visit(url string, width int) (page, error) { func Visit(webmode, url string, width int) (page, error) {
if width > 80 { if width > 80 {
width = 80 width = 80
} }
w := fmt.Sprintf("-width=%d", width) var w string
c, err := exec.Command("lynx", "-dump", w, url).Output() 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 { if err != nil {
return page{}, err return page{}, err
} }
@ -28,26 +38,16 @@ func Visit(url string, width int) (page, error) {
// Returns false on err or non-text type // Returns false on err or non-text type
// Else returns true // Else returns true
func IsTextFile(url string) bool { func IsTextFile(url string) bool {
c, err := exec.Command("lynx", "-dump", "-head", url).Output() resp, err := http.Head(url)
if err != nil { if err != nil {
return false return false
} }
content := string(c) ctype := resp.Header.Get("content-type")
content = strings.ToLower(content) if strings.Contains(ctype, "text") || ctype == "" {
headers := strings.Split(content, "\n") return true
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
}
} }
// If we made it here, there is no content-type header. return false
// So in the event of the unknown, lets render to the
// screen. This will allow redirects to get rendered
// as well.
return true
} }
func parseLinks(c string) page { func parseLinks(c string) page {
@ -72,7 +72,6 @@ func parseLinks(c string) page {
out.Links = append(out.Links, strings.TrimSpace(ls[1])) out.Links = append(out.Links, strings.TrimSpace(ls[1]))
} }
return out return out
} }
func Fetch(url string) ([]byte, error) { func Fetch(url string) ([]byte, error) {

View File

@ -66,6 +66,7 @@ func saveConfig() error {
func validateOpt(opt, val string) bool { func validateOpt(opt, val string) bool {
var validOpts = map[string][]string{ var validOpts = map[string][]string{
"webmode": []string{"none", "gui", "lynx", "w3m", "elinks"},
"openhttp": []string{"true", "false"}, "openhttp": []string{"true", "false"},
"theme": []string{"normal", "inverse"}, "theme": []string{"normal", "inverse"},
"terminalonly": []string{"true", "false"}, "terminalonly": []string{"true", "false"},