Add list tools; normalize tool names and paths in transcript
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package dirlist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const keep = 64<<10 - 100
|
||||
|
||||
func List(path string) (string, error) {
|
||||
entries, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(entries) == 0 {
|
||||
return fmt.Sprintf("%s is empty.\n", path), nil
|
||||
}
|
||||
b := &strings.Builder{}
|
||||
for i, e := range entries {
|
||||
name := e.Name()
|
||||
info, err := os.Stat(filepath.Join(path, name))
|
||||
if err == nil && info.IsDir() {
|
||||
name += "/"
|
||||
}
|
||||
if b.Len()+len(name)+1 > keep {
|
||||
fmt.Fprintf(b, "(%d more entries not shown; output stops at 64 KiB)\n", len(entries)-i)
|
||||
break
|
||||
}
|
||||
b.WriteString(name + "\n")
|
||||
}
|
||||
return b.String(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user