From 9ec03eef4573601931663f719839295488c779b3 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 10 Oct 2022 21:00:41 -0700 Subject: [PATCH] ignore items with configured substrings --- pkg/commands/docker.go | 19 +++++++++++++++++++ pkg/commands/image.go | 7 +++++++ pkg/commands/volume.go | 8 ++++++++ pkg/config/app_config.go | 5 +++++ 4 files changed, 39 insertions(+) diff --git a/pkg/commands/docker.go b/pkg/commands/docker.go index 8b2825c4..c932e377 100644 --- a/pkg/commands/docker.go +++ b/pkg/commands/docker.go @@ -20,6 +20,7 @@ import ( "github.com/jesseduffield/lazydocker/pkg/config" "github.com/jesseduffield/lazydocker/pkg/i18n" "github.com/jesseduffield/lazydocker/pkg/utils" + "github.com/samber/lo" "github.com/sirupsen/logrus" ) @@ -218,7 +219,9 @@ func (c *DockerCommand) RefreshContainersAndServices() error { c.Containers = containers c.Services = services + c.Services = c.filterOutIgnoredServices(c.Services) c.DisplayContainers = c.filterOutExited(displayContainers) + c.DisplayContainers = c.filterOutIgnoredContainers(c.DisplayContainers) c.DisplayContainers = c.sortedContainers(c.DisplayContainers) return nil @@ -251,6 +254,22 @@ func (c *DockerCommand) filterOutExited(containers []*Container) []*Container { return toReturn } +func (c *DockerCommand) filterOutIgnoredContainers(containers []*Container) []*Container { + return lo.Filter(containers, func(container *Container, _ int) bool { + return !lo.SomeBy(c.Config.UserConfig.Ignore, func(ignore string) bool { + return strings.Contains(container.Name, ignore) + }) + }) +} + +func (c *DockerCommand) filterOutIgnoredServices(services []*Service) []*Service { + return lo.Filter(services, func(service *Service, _ int) bool { + return !lo.SomeBy(c.Config.UserConfig.Ignore, func(ignore string) bool { + return strings.Contains(service.Name, ignore) + }) + }) +} + // 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 { diff --git a/pkg/commands/image.go b/pkg/commands/image.go index 7cbb142e..8bde987a 100644 --- a/pkg/commands/image.go +++ b/pkg/commands/image.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/docker/docker/api/types/image" + "github.com/samber/lo" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -144,6 +145,12 @@ func (c *DockerCommand) RefreshImages() ([]*Image, error) { } } + ownImages = lo.Filter(ownImages, func(image *Image, _ int) bool { + return !lo.SomeBy(c.Config.UserConfig.Ignore, func(ignore string) bool { + return strings.Contains(image.Name, ignore) + }) + }) + noneLabel := "" sort.Slice(ownImages, func(i, j int) bool { diff --git a/pkg/commands/volume.go b/pkg/commands/volume.go index 75419915..489bef0d 100644 --- a/pkg/commands/volume.go +++ b/pkg/commands/volume.go @@ -3,10 +3,12 @@ package commands import ( "context" "sort" + "strings" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/client" + "github.com/samber/lo" "github.com/sirupsen/logrus" ) @@ -60,6 +62,12 @@ func (c *DockerCommand) RefreshVolumes() error { } } + ownVolumes = lo.Filter(ownVolumes, func(volume *Volume, _ int) bool { + return !lo.SomeBy(c.Config.UserConfig.Ignore, func(ignore string) bool { + return strings.Contains(volume.Name, ignore) + }) + }) + c.Volumes = ownVolumes return nil diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 104c55ca..c146a099 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -60,6 +60,11 @@ type UserConfig struct { // Replacements determines how we render an item's info Replacements Replacements `yaml:"replacements,omitempty"` + + // For demo purposes: any list item with one of these strings as a substring + // will be filtered out and not displayed. + // Not documented because it's subject to change + Ignore []string `yaml:"ignore,omitempty"` } // ThemeConfig is for setting the colors of panels and some text.