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..07adc57 100644 --- a/local/local.go +++ b/local/local.go @@ -4,39 +4,71 @@ 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, err } defer file.Close() if pathIsDir(address) { - fileList, err := file.Readdirnames(0) + offset := 1 + fileList, err := file.Readdir(0) if err != nil { - return "", 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)) - for _, obj := range fileList { - out.WriteString(obj) - out.WriteString("\n") + + // 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) } - return out.String(), nil + + // 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)) + 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) + } + return out.String(), links, nil } bytes, err := ioutil.ReadAll(file) if err != nil { - return "", fmt.Errorf("Unable to read file: %s", address) + return "", links, err } - return string(bytes), nil + return string(bytes), links, nil } func pathExists(p string) bool { 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 {