From 14e6ff1c1f4a042a5c7e56302605f61c4fa57856 Mon Sep 17 00:00:00 2001 From: lpessoa Date: Tue, 26 Oct 2021 13:52:24 -0300 Subject: [PATCH] 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. --- pkg/commands/docker.go | 17 ++++++ pkg/commands/os_default_platform.go | 1 + pkg/config/app_config.go | 6 +++ pkg/config/config_default_platform.go | 1 + pkg/gui/containers_panel.go | 1 + pkg/i18n/english.go | 74 ++++++++++++++------------- 6 files changed, 64 insertions(+), 36 deletions(-) diff --git a/pkg/commands/docker.go b/pkg/commands/docker.go index ce228705..98ac4825 100644 --- a/pkg/commands/docker.go +++ b/pkg/commands/docker.go @@ -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{} diff --git a/pkg/commands/os_default_platform.go b/pkg/commands/os_default_platform.go index 067bb5e6..7d289796 100644 --- a/pkg/commands/os_default_platform.go +++ b/pkg/commands/os_default_platform.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package commands diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 00f62208..2c984087 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -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, diff --git a/pkg/config/config_default_platform.go b/pkg/config/config_default_platform.go index e9e2f63b..fd1defdc 100644 --- a/pkg/config/config_default_platform.go +++ b/pkg/config/config_default_platform.go @@ -1,3 +1,4 @@ +//go:build !windows && !linux // +build !windows,!linux package config diff --git a/pkg/gui/containers_panel.go b/pkg/gui/containers_panel.go index 7b8a172e..0b6c04a2 100644 --- a/pkg/gui/containers_panel.go +++ b/pkg/gui/containers_panel.go @@ -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) } + diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 81aaf0c9..95242c9b 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -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?",