ignore items with configured substrings

This commit is contained in:
Jesse Duffield 2022-10-10 21:00:41 -07:00
parent 950936b178
commit 9ec03eef45
4 changed files with 39 additions and 0 deletions

View file

@ -20,6 +20,7 @@ import (
"github.com/jesseduffield/lazydocker/pkg/config" "github.com/jesseduffield/lazydocker/pkg/config"
"github.com/jesseduffield/lazydocker/pkg/i18n" "github.com/jesseduffield/lazydocker/pkg/i18n"
"github.com/jesseduffield/lazydocker/pkg/utils" "github.com/jesseduffield/lazydocker/pkg/utils"
"github.com/samber/lo"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -218,7 +219,9 @@ func (c *DockerCommand) RefreshContainersAndServices() error {
c.Containers = containers c.Containers = containers
c.Services = services c.Services = services
c.Services = c.filterOutIgnoredServices(c.Services)
c.DisplayContainers = c.filterOutExited(displayContainers) c.DisplayContainers = c.filterOutExited(displayContainers)
c.DisplayContainers = c.filterOutIgnoredContainers(c.DisplayContainers)
c.DisplayContainers = c.sortedContainers(c.DisplayContainers) c.DisplayContainers = c.sortedContainers(c.DisplayContainers)
return nil return nil
@ -251,6 +254,22 @@ func (c *DockerCommand) filterOutExited(containers []*Container) []*Container {
return toReturn 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) // 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 // and sorted by name if c.SortContainersByState is false
func (c *DockerCommand) sortedContainers(containers []*Container) []*Container { func (c *DockerCommand) sortedContainers(containers []*Container) []*Container {

View file

@ -6,6 +6,7 @@ import (
"strings" "strings"
"github.com/docker/docker/api/types/image" "github.com/docker/docker/api/types/image"
"github.com/samber/lo"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "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 := "<none>" noneLabel := "<none>"
sort.Slice(ownImages, func(i, j int) bool { sort.Slice(ownImages, func(i, j int) bool {

View file

@ -3,10 +3,12 @@ package commands
import ( import (
"context" "context"
"sort" "sort"
"strings"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/samber/lo"
"github.com/sirupsen/logrus" "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 c.Volumes = ownVolumes
return nil return nil

View file

@ -60,6 +60,11 @@ type UserConfig struct {
// Replacements determines how we render an item's info // Replacements determines how we render an item's info
Replacements Replacements `yaml:"replacements,omitempty"` 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. // ThemeConfig is for setting the colors of panels and some text.