Opening html files in browser now works as expected with url resources

This commit is contained in:
sloumdrone 2019-03-28 08:24:49 -07:00
parent 6ff500f386
commit 36f94158f6
5 changed files with 70 additions and 19 deletions

View File

@ -14,6 +14,7 @@ import (
"strconv" "strconv"
) )
var helplocation string ="gopher://colorfield.space:70/1/bombadillo-info"
var history gopher.History = gopher.MakeHistory() var history gopher.History = gopher.MakeHistory()
var screen *cui.Screen var screen *cui.Screen
var userinfo, _ = user.Current() var userinfo, _ = user.Current()
@ -75,7 +76,7 @@ func search(u string) error {
entry := cui.GetLine() entry := cui.GetLine()
quickMessage("Searching...", false) quickMessage("Searching...", false)
searchurl := fmt.Sprintf("%s\t%s", u, entry) searchurl := fmt.Sprintf("%s\t%s", u, entry)
sv, err := gopher.Visit(searchurl) sv, err := gopher.Visit(searchurl, options["openhttp"])
if err != nil { if err != nil {
quickMessage("Searching...", true) quickMessage("Searching...", true)
return err return err
@ -135,6 +136,8 @@ func simple_command(a string) error {
toggle_bookmarks() toggle_bookmarks()
case "SEARCH": case "SEARCH":
return search(options["searchengine"]) return search(options["searchengine"])
case "HELP":
return go_to_url(helplocation)
default: default:
return fmt.Errorf("Unknown action %q", a) return fmt.Errorf("Unknown action %q", a)
@ -144,7 +147,7 @@ func simple_command(a string) error {
func go_to_url(u string) error { func go_to_url(u string) error {
quickMessage("Loading...", false) quickMessage("Loading...", false)
v, err := gopher.Visit(u) v, err := gopher.Visit(u, options["openhttp"])
if err != nil { if err != nil {
quickMessage("Loading...", true) quickMessage("Loading...", true)
return err return err
@ -172,7 +175,7 @@ func go_to_link(l string) error {
if item <= linkcount { if item <= linkcount {
linkurl := history.Collection[history.Position].Links[item - 1] linkurl := history.Collection[history.Position].Links[item - 1]
quickMessage("Loading...", false) quickMessage("Loading...", false)
v, err := gopher.Visit(linkurl) v, err := gopher.Visit(linkurl, options["openhttp"])
if err != nil { if err != nil {
quickMessage("Loading...", true) quickMessage("Loading...", true)
return err return err
@ -386,15 +389,19 @@ func main() {
case 'q', 'Q': case 'q', 'Q':
cui.Exit() cui.Exit()
case 'b': case 'b':
history.GoBack() success := history.GoBack()
if success {
mainWindow.Scrollposition = 0 mainWindow.Scrollposition = 0
redrawScreen = true redrawScreen = true
}
case 'B': case 'B':
toggle_bookmarks() toggle_bookmarks()
case 'f', 'F': case 'f', 'F':
history.GoForward() success := history.GoForward()
if success {
mainWindow.Scrollposition = 0 mainWindow.Scrollposition = 0
redrawScreen = true redrawScreen = true
}
case ':': case ':':
redrawScreen = true redrawScreen = true
cui.MoveCursorTo(screen.Height - 1, 0) cui.MoveCursorTo(screen.Height - 1, 0)

View File

@ -75,7 +75,7 @@ func (s *scanner) scanText() Token {
capInput := strings.ToUpper(buf.String()) capInput := strings.ToUpper(buf.String())
switch capInput { switch capInput {
case "DELETE", "ADD", "WRITE", "SET", "RECALL", "R", "SEARCH", case "DELETE", "ADD", "WRITE", "SET", "RECALL", "R", "SEARCH",
"W", "A", "D", "S", "Q", "QUIT", "B", "BOOKMARKS", "H", "HOME": "W", "A", "D", "S", "Q", "QUIT", "B", "BOOKMARKS", "H", "HOME", "HELP":
return Token{Action, capInput} return Token{Action, capInput}
} }

View File

@ -9,6 +9,9 @@ import (
"net" "net"
"io/ioutil" "io/ioutil"
"time" "time"
"os/exec"
"runtime"
"fmt"
) )
@ -84,12 +87,22 @@ func Retrieve(u Url) ([]byte, error) {
// types that makes it easy to create a Url, make a request // types that makes it easy to create a Url, make a request
// to that Url, and add the response and Url to a View. // to that Url, and add the response and Url to a View.
// Returns a copy of the view and an error (or nil). // Returns a copy of the view and an error (or nil).
func Visit(addr string) (View, error) { func Visit(addr, openhttp string) (View, error) {
u, err := MakeUrl(addr) u, err := MakeUrl(addr)
if err != nil { if err != nil {
return View{}, err return View{}, err
} }
if u.Gophertype == "h" {
if res, tf := isWebLink(u.Resource); tf && strings.ToUpper(openhttp) == "TRUE" {
err := openbrowser(res)
if err != nil {
return View{}, err
}
return View{}, fmt.Errorf("")
}
}
text, err := Retrieve(u) text, err := Retrieve(u)
if err != nil { if err != nil {
return View{}, err return View{}, err
@ -113,3 +126,31 @@ func GetType(t string) string {
} }
func isWebLink(resource string) (string, bool) {
split := strings.SplitN(resource, ":", 2)
if first := strings.ToUpper(split[0]); first == "URL" && len(split) > 1 {
return split[1], true
}
return "", false
}
func openbrowser(url string) error {
// gist.github.com/hyg/9c4afcd91fe24316cbf0
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("Unsupported os for browser detection")
}
if err != nil {
return err
}
return nil
}

View File

@ -66,12 +66,14 @@ func (h History) Get() (*View, error) {
// When called it decrements the current position and // When called it decrements the current position and
// displays the content for the View in that position. // displays the content for the View in that position.
// If history is at position 0, no action is taken. // If history is at position 0, no action is taken.
func (h *History) GoBack() { func (h *History) GoBack() bool {
if h.Position > 0 { if h.Position > 0 {
h.Position-- h.Position--
} else { return true
fmt.Print("\a")
} }
fmt.Print("\a")
return false
} }
@ -79,13 +81,14 @@ func (h *History) GoBack() {
// When called it increments the current position and // When called it increments the current position and
// displays the content for the View in that position. // displays the content for the View in that position.
// If history is at position len - 1, no action is taken. // If history is at position len - 1, no action is taken.
func (h *History) GoForward() { func (h *History) GoForward() bool {
if h.Position + 1 < h.Length { if h.Position + 1 < h.Length {
h.Position++ h.Position++
h.DisplayCurrentView() return true
} else {
fmt.Print("\a")
} }
fmt.Print("\a")
return false
} }
// The "DisplayCurrentView" receiver is called by a history // The "DisplayCurrentView" receiver is called by a history

View File

@ -32,7 +32,7 @@ type Url struct {
// an error (or nil). // an error (or nil).
func MakeUrl(u string) (Url, error) { func MakeUrl(u string) (Url, error) {
var out Url var out Url
re := regexp.MustCompile(`^((?P<scheme>gopher|http|https|ftp|telnet):\/\/)?(?P<host>[\w\-\.\d]+)(?::(?P<port>\d+)?)?(?:/(?P<type>[01345679gIhisp])?)?(?P<resource>(?:\/.*)?)?$`) re := regexp.MustCompile(`^((?P<scheme>gopher|http|https|ftp|telnet):\/\/)?(?P<host>[\w\-\.\d]+)(?::(?P<port>\d+)?)?(?:/(?P<type>[01345679gIhisp])?)?(?P<resource>(?:[\/|Uu].*)?)?$`)
match := re.FindStringSubmatch(u) match := re.FindStringSubmatch(u)
if valid := re.MatchString(u); valid != true { if valid := re.MatchString(u); valid != true {