Adds support for local files, making bombadillo a functional pager

This commit is contained in:
sloumdrone 2019-10-04 22:50:06 -07:00
parent 66acf6102f
commit 484fb77aa3
3 changed files with 61 additions and 1 deletions

View File

@ -15,6 +15,7 @@ import (
"tildegit.org/sloum/bombadillo/gemini"
"tildegit.org/sloum/bombadillo/gopher"
"tildegit.org/sloum/bombadillo/http"
"tildegit.org/sloum/bombadillo/local"
"tildegit.org/sloum/bombadillo/telnet"
)
@ -966,6 +967,20 @@ func (c *client) Visit(url string) {
c.SetMessage("'openhttp' is not set to true, cannot open web link", false)
c.DrawMessage()
}
case "local":
content, err := local.Open(u.Resource)
if err != nil {
c.SetMessage(err.Error(), true)
c.DrawMessage()
return
}
pg := MakePage(u, content, []string{})
pg.WrapContent(c.Width - 1)
c.PageState.Add(pg)
c.SetPercentRead()
c.ClearMessage()
c.SetHeaderUrl()
c.Draw()
default:
c.SetMessage(fmt.Sprintf("%q is not a supported protocol", u.Scheme), true)
c.DrawMessage()

21
local/local.go Normal file
View File

@ -0,0 +1,21 @@
package local
import (
"fmt"
"io/ioutil"
"os"
)
func Open(address string) (string, error) {
file, err := os.Open(address)
if err != nil {
return "", fmt.Errorf("Unable to open file: %s", address)
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
if err != nil {
return "", fmt.Errorf("Unable to read file: %s", address)
}
return string(bytes), nil
}

26
url.go
View File

@ -2,6 +2,8 @@ package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
@ -37,7 +39,29 @@ type Url struct {
// an error (or nil).
func MakeUrl(u string) (Url, error) {
var out Url
re := regexp.MustCompile(`^((?P<scheme>[a-zA-Z]+):\/\/)?(?P<host>[\w\-\.\d]+)(?::(?P<port>\d+)?)?(?:/(?P<type>[01345679gIhisp])?)?(?P<resource>.*)?$`)
if local := strings.HasPrefix(u, "local://"); u[0] == '/' || u[0] == '.' || u[0] == '~' || local {
if local && len(u) > 8 {
u = u[8:]
}
home, err := os.UserHomeDir()
if err != nil {
home = ""
}
u = strings.Replace(u, "~", home, 1)
res, err := filepath.Abs(u)
if err != nil {
return out, fmt.Errorf("Invalid path, unable to parse")
}
out.Scheme = "local"
out.Host = ""
out.Port = ""
out.Mime = ""
out.Resource = res
out.Full = out.Scheme + "://" + out.Resource
return out, nil
}
re := regexp.MustCompile(`^((?P<scheme>[a-zA-Z]+):\/\/)?(?P<host>[\w\-\.\d/]+)(?::(?P<port>\d+)?)?(?:/(?P<type>[01345679gIhisp])?)?(?P<resource>.*)?$`)
match := re.FindStringSubmatch(u)
if valid := re.MatchString(u); !valid {