Adds better formatting/options for local protocol directory listings

This commit is contained in:
sloumdrone 2019-11-16 17:38:11 -08:00
parent 3ee7bd9c7b
commit c948be18dc
2 changed files with 30 additions and 13 deletions

View File

@ -4,13 +4,13 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"path/filepath"
"sort"
"strings"
)
func Open(address string) (string, []string, error) {
links := make([]string, 0, 10)
links := make([]string, 0, 10)
if !pathExists(address) {
return "", links, fmt.Errorf("Invalid system path: %s", address)
@ -23,6 +23,7 @@ func Open(address string) (string, []string, error) {
defer file.Close()
if pathIsDir(address) {
offset := 1
fileList, err := file.Readdir(0)
if err != nil {
return "", links, fmt.Errorf("Unable to read from directory: %s", address)
@ -30,25 +31,41 @@ func Open(address string) (string, []string, error) {
var out strings.Builder
out.WriteString(fmt.Sprintf("Current directory: %s\n\n", address))
sort.Slice(fileList, func(i, j int) bool {
return fileList[i].Name() < fileList[j].Name()
})
if address != "/" {
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)
}
}
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()))
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)
fp := filepath.Join(address, obj.Name())
links = append(links, fp)
}
return out.String(), links, nil
}
bytes, err := ioutil.ReadAll(file)
if err != nil {
return "", links ,fmt.Errorf("Unable to read file: %s", address)
return "", links, fmt.Errorf("Unable to read file: %s", address)
}
return string(bytes), links, nil
}

View File

@ -70,7 +70,7 @@ func (p *Page) WrapContent(width int) {
content.WriteRune('\n')
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
continue
} else if ch == 27 {