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()
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()
}
}

View File

@ -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

View File

@ -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) {

View File

@ -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"},