From 552e21113902ba5e37aeca6e91541a2608a61008 Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Tue, 22 Oct 2019 22:02:32 -0700 Subject: [PATCH] Adds web support in lynx mode --- bombadillo.1 | 9 +++++++-- client.go | 44 +++++++++++++++++++++++++++++++----------- defaults.go | 2 +- http/lynx_mode.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 4 +++- 5 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 http/lynx_mode.go diff --git a/bombadillo.1 b/bombadillo.1 index 1fea65a..c03d56f 100644 --- a/bombadillo.1 +++ b/bombadillo.1 @@ -215,8 +215,13 @@ homeurl The url that \fBbombadillo\fP navigates to when the program loads or when the \fIhome\fP or \fIh\fP LINE COMMAND is issued. This should be a valid url. If a scheme/protocol is not included, gopher will be assumed. .TP .B +lynxmode +Will use lynx as a rendering engine for http/https requests if lynx is installed and \fIopenhttp\fP is set to \fItrue\fP. Valid values are \fItrue\fP and \fIfalse\fP. +.TP +.B openhttp -Tells the client whether or not to try to follow web (http/https) links. If set to \fItrue\fP, \fBbombadillo\fP will try to open a user's default web browser to the link in question. Any value other than \fItrue\fP is considered false. +Tells the client whether or not to try to follow web (http/https) links. If set to \fItrue\fP, \fBbombadillo\fP will try to open a user's default web browser to the link in question. Valid values are \fItrue\fP and \fIfalse\fP. + .TP .B savelocation @@ -232,7 +237,7 @@ Tells the client what command to use to start a telnet session. Should be a vali .TP .B terminalonly -Sets whether or not to try to open non-text files served via gemini in gui programs or not. If set to \fItrue\fP, bombdaillo will only attempt to use terminal programs to open files. If set to anything else, \fBbombadillo\fP may choose from the appropriate programs installed on the system, if one is present. +Sets whether or not to try to open non-text files served via gemini in gui programs or not. If set to \fItrue\fP \fBbombdaillo\fP will only attempt to use terminal programs to open files. If set to \fIfalse\fP \fBbombadillo\fP may choose from the appropriate programs installed on the system, including graphical ones. .TP .B theme diff --git a/client.go b/client.go index 71b998d..4b49f77 100644 --- a/client.go +++ b/client.go @@ -972,19 +972,41 @@ func (c *client) Visit(url string) { } c.Draw() case "http", "https": - c.SetMessage("Attempting to open in web browser", false) - c.DrawMessage() - if strings.ToUpper(c.Options["openhttp"]) == "TRUE" { - msg, err := http.OpenInBrowser(u.Full) - if err != nil { - c.SetMessage(err.Error(), true) - } else { - c.SetMessage(msg, false) - } - c.DrawMessage() - } else { + if strings.ToUpper(c.Options["openhttp"]) != "TRUE" { c.SetMessage("'openhttp' is not set to true, cannot open web link", false) c.DrawMessage() + return + } + switch strings.ToUpper(c.Options["lynxmode"]) { + case "TRUE": + page, err := http.Visit(u.Full, c.Width - 1) + if err != nil { + c.SetMessage(fmt.Sprintf("Lynx error: %s", err.Error()), true) + c.DrawMessage() + return + } + pg := MakePage(u, page.Content, page.Links) + pg.WrapContent(c.Width - 1) + c.PageState.Add(pg) + c.SetPercentRead() + c.ClearMessage() + c.SetHeaderUrl() + c.Draw() + default: + 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() + } 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() + } } case "local": content, err := local.Open(u.Resource) diff --git a/defaults.go b/defaults.go index e3273d2..a7d1661 100644 --- a/defaults.go +++ b/defaults.go @@ -16,12 +16,12 @@ var defaultOptions = map[string]string{ "savelocation": userinfo.HomeDir, "searchengine": "gopher://gopher.floodgap.com:70/7/v2/vs", "openhttp": "false", - "httpbrowser": "lynx", "telnetcommand": "telnet", "configlocation": userinfo.HomeDir, "theme": "normal", // "normal", "inverted" "terminalonly": "true", "tlscertificate": "", "tlskey": "", + "lynxmode": "false", } diff --git a/http/lynx_mode.go b/http/lynx_mode.go new file mode 100644 index 0000000..325b759 --- /dev/null +++ b/http/lynx_mode.go @@ -0,0 +1,49 @@ +package http + +import ( + "fmt" + "os/exec" + "strings" +) + +type page struct { + Content string + Links []string +} + +func Visit(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() + if err != nil { + return page{}, err + } + return parseLinks(string(c)), nil +} + +func parseLinks(c string) page { + var out page + contentUntil := strings.LastIndex(c, "References") + if contentUntil >= 1 { + out.Content = c[:contentUntil] + } else { + out.Content = c + out.Links = make([]string, 0) + return out + } + links := c[contentUntil+11:] + links = strings.TrimSpace(links) + linkSlice := strings.Split(links, "\n") + out.Links = make([]string, 0, len(linkSlice)) + for _, link := range linkSlice { + ls := strings.SplitN(link, ".", 2) + if len(ls) < 2 { + continue + } + out.Links = append(out.Links, strings.TrimSpace(ls[1])) + } + return out + +} diff --git a/main.go b/main.go index 249139d..69eba38 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main -// Bombadillo is a gopher and gemini client for the terminal of unix or unix-like systems. +// Bombadillo is an internet client for the terminal of unix or +// unix-like systems. // // Copyright (C) 2019 Brian Evans // @@ -68,6 +69,7 @@ func validateOpt(opt, val string) bool { "openhttp": []string{"true", "false"}, "theme": []string{"normal", "inverse"}, "terminalonly": []string{"true", "false"}, + "lynxmode": []string{"true", "false"}, } opt = strings.ToLower(opt)