Skip to content

Commit 998724d

Browse files
chore: sort agent /list-directory output (coder#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.
1 parent 5979c32 commit 998724d

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

agent/ls.go

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"regexp"
99
"runtime"
10+
"slices"
1011
"strings"
1112

1213
"github.com/shirou/gopsutil/v4/disk"
@@ -103,6 +104,17 @@ func listFiles(query LSRequest) (LSResponse, error) {
103104
})
104105
}
105106

107+
// Sort alphabetically: directories then files
108+
slices.SortFunc(respContents, func(a, b LSFile) int {
109+
if a.IsDir && !b.IsDir {
110+
return -1
111+
}
112+
if !a.IsDir && b.IsDir {
113+
return 1
114+
}
115+
return strings.Compare(a.Name, b.Name)
116+
})
117+
106118
absolutePath := pathToArray(absolutePathString)
107119

108120
return LSResponse{

agent/ls_internal_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,16 @@ func TestListFilesSuccess(t *testing.T) {
137137
require.NoError(t, err)
138138

139139
require.Equal(t, tmpDir, resp.AbsolutePathString)
140-
require.ElementsMatch(t, []LSFile{
140+
// Output is sorted
141+
require.Equal(t, []LSFile{
141142
{
142-
Name: "repos",
143-
AbsolutePathString: reposDir,
143+
Name: "Downloads",
144+
AbsolutePathString: downloadsDir,
144145
IsDir: true,
145146
},
146147
{
147-
Name: "Downloads",
148-
AbsolutePathString: downloadsDir,
148+
Name: "repos",
149+
AbsolutePathString: reposDir,
149150
IsDir: true,
150151
},
151152
{

0 commit comments

Comments
 (0)