From 3ee7bd9c7b32fc11c8c8abb8bbf677e55d4392c9 Mon Sep 17 00:00:00 2001 From: Brian Evans Date: Fri, 15 Nov 2019 14:48:50 -0800 Subject: [PATCH 1/3] Makes local protocol navigable --- client.go | 4 ++-- local/local.go | 34 ++++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/client.go b/client.go index 23f4925..ea75444 100644 --- a/client.go +++ b/client.go @@ -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() diff --git a/local/local.go b/local/local.go index 430a499..f257cbe 100644 --- a/local/local.go +++ b/local/local.go @@ -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 { From c948be18dcd0e0c423f40563451c3fa366aca041 Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Sat, 16 Nov 2019 17:38:11 -0800 Subject: [PATCH 2/3] 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 { From 8e4091aac14b33687d0f82f786d0e2ca08567d1e Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Sun, 17 Nov 2019 14:58:43 -0800 Subject: [PATCH 3/3] Simplifies .. navigation and passes actual errors --- local/local.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/local/local.go b/local/local.go index 1948ec7..07adc57 100644 --- a/local/local.go +++ b/local/local.go @@ -18,7 +18,7 @@ func Open(address string) (string, []string, error) { file, err := os.Open(address) if err != nil { - return "", links, fmt.Errorf("Unable to open file: %s", address) + return "", links, err } defer file.Close() @@ -26,28 +26,29 @@ func Open(address string) (string, []string, error) { offset := 1 fileList, err := file.Readdir(0) if err != nil { - return "", links, fmt.Errorf("Unable to read from directory: %s", address) + return "", links, err } 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) - } + // Handle 'addres/..' display + 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 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)) @@ -65,7 +66,7 @@ func Open(address string) (string, []string, error) { bytes, err := ioutil.ReadAll(file) if err != nil { - return "", links, fmt.Errorf("Unable to read file: %s", address) + return "", links, err } return string(bytes), links, nil }