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.Services = services
c.DisplayContainers = c.filterOutExited(displayContainers)
c.DisplayContainers = c.sortedContainers(c.DisplayContainers)
return nil
}
@ -274,6 +275,22 @@ func (c *DockerCommand) filterOutExited(containers []*Container) []*Container {
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
func (c *DockerCommand) obtainStandaloneContainers(containers []*Container, services []*Service) []*Container {
standaloneContainers := []*Container{}

View file

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

View file

@ -112,6 +112,11 @@ type GuiConfig struct {
// WrapMainPanel determines whether we use word wrap on the main panel
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
@ -325,6 +330,7 @@ func GetDefaultConfig() UserConfig {
ShowAllContainers: false,
ReturnImmediately: false,
WrapMainPanel: false,
SortContainersByName: false,
},
Reporting: "undetermined",
ConfirmOnQuit: false,

View file

@ -1,3 +1,4 @@
//go:build !windows && !linux
// +build !windows,!linux
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)
return gui.OSCommand.OpenLink(link)
}

View file

@ -84,6 +84,7 @@ type TranslationSet struct {
RunCustomCommand string
ViewBulkCommands string
OpenInBrowser string
SortContainersByState string
LogsTitle string
ConfigTitle string
@ -118,42 +119,43 @@ func englishSet() TranslationSet {
Donate: "Donate",
Confirm: "Confirm",
Return: "return",
FocusMain: "focus main panel",
Navigate: "navigate",
Execute: "execute",
Close: "close",
Menu: "menu",
Scroll: "scroll",
OpenConfig: "open lazydocker config",
EditConfig: "edit lazydocker config",
Cancel: "cancel",
Remove: "remove",
HideStopped: "Hide/Show stopped containers",
ForceRemove: "force remove",
RemoveWithVolumes: "remove with volumes",
RemoveService: "remove containers",
Stop: "stop",
Restart: "restart",
Rebuild: "rebuild",
Recreate: "recreate",
PreviousContext: "previous tab",
NextContext: "next tab",
Attach: "attach",
ViewLogs: "view logs",
RemoveImage: "remove image",
RemoveVolume: "remove volume",
RemoveWithoutPrune: "remove without deleting untagged parents",
PruneContainers: "prune exited containers",
PruneVolumes: "prune unused volumes",
PruneImages: "prune unused images",
StopAllContainers: "stop all containers",
RemoveAllContainers: "remove all containers (forced)",
ViewRestartOptions: "view restart options",
ExecShell: "exec shell",
RunCustomCommand: "run predefined custom command",
ViewBulkCommands: "view bulk commands",
OpenInBrowser: "open in browser (first port is http)",
Return: "return",
FocusMain: "focus main panel",
Navigate: "navigate",
Execute: "execute",
Close: "close",
Menu: "menu",
Scroll: "scroll",
OpenConfig: "open lazydocker config",
EditConfig: "edit lazydocker config",
Cancel: "cancel",
Remove: "remove",
HideStopped: "Hide/Show stopped containers",
ForceRemove: "force remove",
RemoveWithVolumes: "remove with volumes",
RemoveService: "remove containers",
Stop: "stop",
Restart: "restart",
Rebuild: "rebuild",
Recreate: "recreate",
PreviousContext: "previous tab",
NextContext: "next tab",
Attach: "attach",
ViewLogs: "view logs",
RemoveImage: "remove image",
RemoveVolume: "remove volume",
RemoveWithoutPrune: "remove without deleting untagged parents",
PruneContainers: "prune exited containers",
PruneVolumes: "prune unused volumes",
PruneImages: "prune unused images",
StopAllContainers: "stop all containers",
RemoveAllContainers: "remove all containers (forced)",
ViewRestartOptions: "view restart options",
ExecShell: "exec shell",
RunCustomCommand: "run predefined custom command",
ViewBulkCommands: "view bulk commands",
OpenInBrowser: "open in browser (first port is http)",
SortContainersByState: "sort containers by state",
AnonymousReportingTitle: "Help make lazydocker better",
AnonymousReportingPrompt: "Would you like to enable anonymous reporting data to help improve lazydocker?",