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 {