From c948be18dcd0e0c423f40563451c3fa366aca041 Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Sat, 16 Nov 2019 17:38:11 -0800 Subject: [PATCH] Adds better formatting/options for local protocol directory listings --- local/local.go | 41 +++++++++++++++++++++++++++++------------ page.go | 2 +- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/local/local.go b/local/local.go index f257cbe..1948ec7 100644 --- a/local/local.go +++ b/local/local.go @@ -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 } diff --git a/page.go b/page.go index 734fab4..4c8c0d8 100644 --- a/page.go +++ b/page.go @@ -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 {