mirror of
https://github.com/coder/coder.git
synced 2026-09-21 20:51:01 +08:00
chore: sort agent /list-directory output (#17218)
This sorts the `contents` list alphabetically, but with directories before everything else. This is purely for UX on the Coder Desktop side, where the user only really cares about directories, and files are just for providing context in the file picker.
This commit is contained in:
+12
@@ -7,6 +7,7 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/shirou/gopsutil/v4/disk"
|
||||
@@ -103,6 +104,17 @@ func listFiles(query LSRequest) (LSResponse, error) {
|
||||
})
|
||||
}
|
||||
|
||||
// Sort alphabetically: directories then files
|
||||
slices.SortFunc(respContents, func(a, b LSFile) int {
|
||||
if a.IsDir && !b.IsDir {
|
||||
return -1
|
||||
}
|
||||
if !a.IsDir && b.IsDir {
|
||||
return 1
|
||||
}
|
||||
return strings.Compare(a.Name, b.Name)
|
||||
})
|
||||
|
||||
absolutePath := pathToArray(absolutePathString)
|
||||
|
||||
return LSResponse{
|
||||
|
||||
@@ -137,17 +137,18 @@ func TestListFilesSuccess(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, tmpDir, resp.AbsolutePathString)
|
||||
require.ElementsMatch(t, []LSFile{
|
||||
{
|
||||
Name: "repos",
|
||||
AbsolutePathString: reposDir,
|
||||
IsDir: true,
|
||||
},
|
||||
// Output is sorted
|
||||
require.Equal(t, []LSFile{
|
||||
{
|
||||
Name: "Downloads",
|
||||
AbsolutePathString: downloadsDir,
|
||||
IsDir: true,
|
||||
},
|
||||
{
|
||||
Name: "repos",
|
||||
AbsolutePathString: reposDir,
|
||||
IsDir: true,
|
||||
},
|
||||
{
|
||||
Name: "file.txt",
|
||||
AbsolutePathString: textFile,
|
||||
|
||||
Reference in New Issue
Block a user