Merge branch 'improved-local' of sloum/bombadillo into develop
This commit is contained in:
commit
c9c87f4129
|
@ -963,13 +963,13 @@ func (c *client) handleTelnet(u Url) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) handleLocal(u Url) {
|
func (c *client) handleLocal(u Url) {
|
||||||
content, err := local.Open(u.Resource)
|
content, links, err := local.Open(u.Resource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.SetMessage(err.Error(), true)
|
c.SetMessage(err.Error(), true)
|
||||||
c.DrawMessage()
|
c.DrawMessage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pg := MakePage(u, content, []string{})
|
pg := MakePage(u, content, links)
|
||||||
pg.WrapContent(c.Width - 1)
|
pg.WrapContent(c.Width - 1)
|
||||||
c.PageState.Add(pg)
|
c.PageState.Add(pg)
|
||||||
c.SetPercentRead()
|
c.SetPercentRead()
|
||||||
|
|
|
@ -4,39 +4,71 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Open(address string) (string, error) {
|
func Open(address string) (string, []string, error) {
|
||||||
|
links := make([]string, 0, 10)
|
||||||
|
|
||||||
if !pathExists(address) {
|
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)
|
file, err := os.Open(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Unable to open file: %s", address)
|
return "", links, err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
if pathIsDir(address) {
|
if pathIsDir(address) {
|
||||||
fileList, err := file.Readdirnames(0)
|
offset := 1
|
||||||
|
fileList, err := file.Readdir(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Unable to read from directory: %s", address)
|
return "", links, err
|
||||||
}
|
}
|
||||||
var out strings.Builder
|
var out strings.Builder
|
||||||
out.WriteString(fmt.Sprintf("Current directory: %s\n\n", address))
|
out.WriteString(fmt.Sprintf("Current directory: %s\n\n", address))
|
||||||
for _, obj := range fileList {
|
|
||||||
out.WriteString(obj)
|
// Handle 'addres/..' display
|
||||||
out.WriteString("\n")
|
offset = 2
|
||||||
|
upFp := filepath.Join(address, "..")
|
||||||
|
upOneLevel, _ := filepath.Abs(upFp)
|
||||||
|
info, err := os.Stat(upOneLevel)
|
||||||
|
if err == nil {
|
||||||
|
out.WriteString("[1] ")
|
||||||
|
out.WriteString(fmt.Sprintf("%-12s ", info.Mode().String()))
|
||||||
|
out.WriteString("../\n")
|
||||||
|
links = append(links, upOneLevel)
|
||||||
}
|
}
|
||||||
return out.String(), nil
|
|
||||||
|
// Sort the directory contents alphabetically
|
||||||
|
sort.Slice(fileList, func(i, j int) bool {
|
||||||
|
return fileList[i].Name() < fileList[j].Name()
|
||||||
|
})
|
||||||
|
|
||||||
|
// Handle each item in the directory
|
||||||
|
for i, obj := range fileList {
|
||||||
|
linkNum := fmt.Sprintf("[%d]", i+offset)
|
||||||
|
out.WriteString(fmt.Sprintf("%-5s ", linkNum))
|
||||||
|
out.WriteString(fmt.Sprintf("%-12s ", obj.Mode().String()))
|
||||||
|
out.WriteString(obj.Name())
|
||||||
|
if obj.IsDir() {
|
||||||
|
out.WriteString("/")
|
||||||
|
}
|
||||||
|
out.WriteString("\n")
|
||||||
|
fp := filepath.Join(address, obj.Name())
|
||||||
|
links = append(links, fp)
|
||||||
|
}
|
||||||
|
return out.String(), links, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes, err := ioutil.ReadAll(file)
|
bytes, err := ioutil.ReadAll(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Unable to read file: %s", address)
|
return "", links, err
|
||||||
}
|
}
|
||||||
return string(bytes), nil
|
return string(bytes), links, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func pathExists(p string) bool {
|
func pathExists(p string) bool {
|
||||||
|
|
2
page.go
2
page.go
|
@ -70,7 +70,7 @@ func (p *Page) WrapContent(width int) {
|
||||||
content.WriteRune('\n')
|
content.WriteRune('\n')
|
||||||
counter = 0
|
counter = 0
|
||||||
}
|
}
|
||||||
} else if ch == '\r' || ch == '\v' || ch == '\b' || ch == '\f' {
|
} else if ch == '\r' || ch == '\v' || ch == '\b' || ch == '\f' || ch == '\a' {
|
||||||
// Get rid of control characters we dont want
|
// Get rid of control characters we dont want
|
||||||
continue
|
continue
|
||||||
} else if ch == 27 {
|
} else if ch == 27 {
|
||||||
|
|
Loading…
Reference in New Issue