Use sort containers by state by default

Implements #224
Use sort containers by state as default behavior

Legacy sort by name behavior can be set using a user config setting.
`sortContainersByName` is false by default.
This commit is contained in:
lpessoa 2021-10-26 13:52:24 -03:00
parent a0386618f9
commit 14e6ff1c1f
6 changed files with 64 additions and 36 deletions

View file

@ -243,6 +243,7 @@ func (c *DockerCommand) RefreshContainersAndServices() error {
c.Containers = containers c.Containers = containers
c.Services = services c.Services = services
c.DisplayContainers = c.filterOutExited(displayContainers) c.DisplayContainers = c.filterOutExited(displayContainers)
c.DisplayContainers = c.sortedContainers(c.DisplayContainers)
return nil return nil
} }
@ -274,6 +275,22 @@ func (c *DockerCommand) filterOutExited(containers []*Container) []*Container {
return toReturn return toReturn
} }
// sortedContainers returns containers sorted by state if c.SortContainersByState is true (follows 1- running, 2- exited, 3- created)
// and sorted by name if c.SortContainersByState is false
func (c *DockerCommand) sortedContainers(containers []*Container) []*Container {
if !c.Config.UserConfig.Gui.SortContainersByName {
sort.Slice(containers, func(i, j int) bool {
states := map[string]int{
"running": 1,
"exited": 2,
"created": 3,
}
return states[containers[i].Container.State] < states[containers[j].Container.State]
})
}
return containers
}
// obtainStandaloneContainers returns standalone containers. Standalone containers are containers which are either one-off containers, or whose service is not part of this docker-compose context // obtainStandaloneContainers returns standalone containers. Standalone containers are containers which are either one-off containers, or whose service is not part of this docker-compose context
func (c *DockerCommand) obtainStandaloneContainers(containers []*Container, services []*Service) []*Container { func (c *DockerCommand) obtainStandaloneContainers(containers []*Container, services []*Service) []*Container {
standaloneContainers := []*Container{} standaloneContainers := []*Container{}

View file

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows // +build !windows
package commands package commands

View file

@ -112,6 +112,11 @@ type GuiConfig struct {
// WrapMainPanel determines whether we use word wrap on the main panel // WrapMainPanel determines whether we use word wrap on the main panel
WrapMainPanel bool `yaml:"wrapMainPanel,omitempty"` WrapMainPanel bool `yaml:"wrapMainPanel,omitempty"`
// SortContainersByName determines if containers should be sorted by name.
// By default, containers are now sorted by status. This setting allows users to
// use legacy behaviour instead.
SortContainersByName bool `yaml:"sortContainersByName, omitempty"`
} }
// CommandTemplatesConfig determines what commands actually get called when we // CommandTemplatesConfig determines what commands actually get called when we
@ -325,6 +330,7 @@ func GetDefaultConfig() UserConfig {
ShowAllContainers: false, ShowAllContainers: false,
ReturnImmediately: false, ReturnImmediately: false,
WrapMainPanel: false, WrapMainPanel: false,
SortContainersByName: false,
}, },
Reporting: "undetermined", Reporting: "undetermined",
ConfirmOnQuit: false, ConfirmOnQuit: false,

View file

@ -1,3 +1,4 @@
//go:build !windows && !linux
// +build !windows,!linux // +build !windows,!linux
package config package config

View file

@ -618,3 +618,4 @@ func (gui *Gui) openContainerInBrowser(container *commands.Container) error {
link := fmt.Sprintf("http://%s:%d/", ip, port.PublicPort) link := fmt.Sprintf("http://%s:%d/", ip, port.PublicPort)
return gui.OSCommand.OpenLink(link) return gui.OSCommand.OpenLink(link)
} }

View file

@ -84,6 +84,7 @@ type TranslationSet struct {
RunCustomCommand string RunCustomCommand string
ViewBulkCommands string ViewBulkCommands string
OpenInBrowser string OpenInBrowser string
SortContainersByState string
LogsTitle string LogsTitle string
ConfigTitle string ConfigTitle string
@ -154,6 +155,7 @@ func englishSet() TranslationSet {
RunCustomCommand: "run predefined custom command", RunCustomCommand: "run predefined custom command",
ViewBulkCommands: "view bulk commands", ViewBulkCommands: "view bulk commands",
OpenInBrowser: "open in browser (first port is http)", OpenInBrowser: "open in browser (first port is http)",
SortContainersByState: "sort containers by state",
AnonymousReportingTitle: "Help make lazydocker better", AnonymousReportingTitle: "Help make lazydocker better",
AnonymousReportingPrompt: "Would you like to enable anonymous reporting data to help improve lazydocker?", AnonymousReportingPrompt: "Would you like to enable anonymous reporting data to help improve lazydocker?",