Makes local protocol navigable

This commit is contained in:
Brian Evans 2019-11-15 14:48:50 -08:00
parent bb4fed6d6b
commit 3ee7bd9c7b
2 changed files with 26 additions and 12 deletions

View File

@ -963,13 +963,13 @@ func (c *client) handleTelnet(u Url) {
}
func (c *client) handleLocal(u Url) {
content, err := local.Open(u.Resource)
content, links, err := local.Open(u.Resource)
if err != nil {
c.SetMessage(err.Error(), true)
c.DrawMessage()
return
}
pg := MakePage(u, content, []string{})
pg := MakePage(u, content, links)
pg.WrapContent(c.Width - 1)
c.PageState.Add(pg)
c.SetPercentRead()

View File

@ -4,39 +4,53 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
)
func Open(address string) (string, error) {
func Open(address string) (string, []string, error) {
links := make([]string, 0, 10)
if !pathExists(address) {
return "", fmt.Errorf("Invalid system path: %s", address)
return "", links, fmt.Errorf("Invalid system path: %s", address)
}
file, err := os.Open(address)
if err != nil {
return "", fmt.Errorf("Unable to open file: %s", address)
return "", links, fmt.Errorf("Unable to open file: %s", address)
}
defer file.Close()
if pathIsDir(address) {
fileList, err := file.Readdirnames(0)
fileList, err := file.Readdir(0)
if err != nil {
return "", fmt.Errorf("Unable to read from directory: %s", address)
return "", links, 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)
sort.Slice(fileList, func(i, j int) bool {
return fileList[i].Name() < fileList[j].Name()
})
for i, obj := range fileList {
linkNum := fmt.Sprintf("[%d]", i+1)
out.WriteString(fmt.Sprintf("%-5s ", linkNum))
out.WriteString(fmt.Sprintf("%s ", obj.Mode().String()))
out.WriteString(obj.Name())
out.WriteString("\n")
fp := filepath.Join(address, obj.Name())
links = append(links, fp)
}
return out.String(), nil
return out.String(), links, nil
}
bytes, err := ioutil.ReadAll(file)
if err != nil {
return "", fmt.Errorf("Unable to read file: %s", address)
return "", links ,fmt.Errorf("Unable to read file: %s", address)
}
return string(bytes), nil
return string(bytes), links, nil
}
func pathExists(p string) bool {