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

@ -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,15 +31,31 @@ func Open(address string) (string, []string, error) {
var out strings.Builder
out.WriteString(fmt.Sprintf("Current directory: %s\n\n", address))
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)
linkNum := fmt.Sprintf("[%d]", i+offset)
out.WriteString(fmt.Sprintf("%-5s ", linkNum))
out.WriteString(fmt.Sprintf("%s ", obj.Mode().String()))
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)

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 {