Skip to content

Commit 5c180e9

Browse files
committed
#191 Docker Compose support: first sort by compose project and then by name
In 99% of cases container name anyway contains its compose project name. For example we have a folder wp which has docker-compose.yaml with two services: db and wordpress. Then after docker-compose up container names will be: wp_wordpress_1 and wp_db_1. But in docker-compose.yaml for a specific service may be explicitly set `container_name` e.g. for wordpress service it may be just "blog". Or some other container that is not part of compose project may have a similar name like wp_admin. Then on sorting by name we'll see: blog <- part of wp compose project wp_admin <- NOT part of wp compose project wp_db <- part of wp compose project To keep container always together on name sorting we'll first sort by compose project and only then by container name inside of the project. Thus we'll always have: wp_admin blog wp_db In this case blog and wp_db was sorted separately.
1 parent 145ed61 commit 5c180e9

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

container/sort.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"regexp"
66
"sort"
7+
"strings"
78

89
"github.com/bcicen/ctop/config"
910
)
@@ -19,7 +20,13 @@ var stateMap = map[string]int{
1920
}
2021

2122
var idSorter = func(c1, c2 *Container) bool { return c1.Id < c2.Id }
22-
var nameSorter = func(c1, c2 *Container) bool { return c1.GetMeta("name") < c2.GetMeta("name") }
23+
var nameSorter = func(c1, c2 *Container) bool {
24+
composeCmp := strings.Compare(c1.GetMeta("compose project"), c2.GetMeta("compose project"))
25+
if composeCmp == 0 {
26+
return c1.GetMeta("name") < c2.GetMeta("name")
27+
}
28+
return composeCmp < 0
29+
}
2330

2431
var Sorters = map[string]sortMethod{
2532
"id": idSorter,

0 commit comments

Comments
 (0)