Merge branch 'file-mode' of sloum/bombadillo into develop

This commit is contained in:
Sloom Sloum Sluom IV 2019-10-09 21:23:22 -04:00 committed by Gitea
commit 282bd9d246
3 changed files with 125 additions and 1 deletions

View File

@ -17,6 +17,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"
)
@ -187,6 +188,15 @@ func (c *client) TakeControlInput() {
c.SetPercentRead()
c.Draw()
}
case 'R':
c.ClearMessage()
err := c.ReloadPage()
if err != nil {
c.SetMessage(err.Error(), false)
c.DrawMessage()
} else {
c.Draw()
}
case 'B':
// open the bookmarks browser
c.BookMarks.ToggleOpen()
@ -969,6 +979,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()
case "finger":
content, err := finger.Finger(u.Host, u.Port, u.Resource)
if err != nil {
@ -989,6 +1013,22 @@ func (c *client) Visit(url string) {
}
}
func (c *client) ReloadPage() error {
if c.PageState.Length < 1 {
return fmt.Errorf("There is no page to reload")
}
url := c.PageState.History[c.PageState.Position].Location.Full
err := c.PageState.NavigateHistory(-1)
if err != nil {
return err
}
length := c.PageState.Length
c.Visit(url)
c.PageState.Length = length
return nil
}
//------------------------------------------------\\
// + + + F U N C T I O N S + + + \\
//--------------------------------------------------\\

60
local/local.go Normal file
View File

@ -0,0 +1,60 @@
package local
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func Open(address string) (string, error) {
if !pathExists(address) {
return "", fmt.Errorf("Invalid system path: %s", address)
}
file, err := os.Open(address)
if err != nil {
return "", fmt.Errorf("Unable to open file: %s", address)
}
defer file.Close()
if pathIsDir(address) {
fileList, err := file.Readdirnames(0)
if err != nil {
return "", fmt.Errorf("Unable to read from directory: %s", address)
}
var out strings.Builder
out.WriteString(fmt.Sprintf("Current directory: %s\n\n", address))
for _, obj := range fileList {
out.WriteString(obj)
out.WriteString("\n")
}
return out.String(), nil
}
bytes, err := ioutil.ReadAll(file)
if err != nil {
return "", fmt.Errorf("Unable to read file: %s", address)
}
return string(bytes), nil
}
func pathExists(p string) bool {
exists := true
if _, err := os.Stat(p); os.IsNotExist(err) {
exists = false
}
return exists
}
func pathIsDir(p string) bool {
info, err := os.Stat(p)
if err != nil {
return false
}
return info.IsDir()
}

26
url.go
View File

@ -2,6 +2,8 @@ package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
@ -44,7 +46,29 @@ 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 {