better alignment for health check

This commit is contained in:
Jesse Duffield 2020-09-23 20:16:04 +10:00
parent 386334b1f0
commit 1dd0875323
2 changed files with 23 additions and 15 deletions

View file

@ -251,31 +251,38 @@ type ContainerCliStat struct {
func (c *Container) GetDisplayStrings(isFocused bool) []string {
image := strings.TrimPrefix(c.Container.Image, "sha256:")
return []string{c.GetDisplayStatus(), c.Name, c.GetDisplayCPUPerc(), utils.ColoredString(image, color.FgMagenta)}
return []string{c.GetDisplayStatus(), c.GetDisplaySubstatus(), c.Name, c.GetDisplayCPUPerc(), utils.ColoredString(image, color.FgMagenta)}
}
// GetDisplayStatus returns the colored status of the container
func (c *Container) GetDisplayStatus() string {
state := c.Container.State
if c.Container.State == "exited" {
return utils.ColoredString(state+" ("+strconv.Itoa(c.Details.State.ExitCode)+")", c.GetColor())
}
return utils.ColoredString(state, c.GetColor()) + c.healthStatusString()
return utils.ColoredString(c.Container.State, c.GetColor())
}
func (c *Container) healthStatusString() string {
// GetDisplayStatus returns the exit code if the container has exited, and the health status if the container is running (and has a health check)
func (c *Container) GetDisplaySubstatus() string {
switch c.Container.State {
case "exited":
return utils.ColoredString(
fmt.Sprintf("(%s)", strconv.Itoa(c.Details.State.ExitCode)), c.GetColor(),
)
case "running":
return c.getHealthStatus()
default:
return ""
}
}
func (c *Container) getHealthStatus() string {
healthStatusColorMap := map[string]color.Attribute{
"healthy": color.FgGreen,
"unhealthy": color.FgRed,
"starting": color.FgYellow,
}
if c.Container.State != "running" {
return ""
}
healthStatus := c.Details.State.Health.Status
if healthStatusColor, ok := healthStatusColorMap[healthStatus]; ok {
return utils.ColoredString(" ("+healthStatus+")", healthStatusColor)
return utils.ColoredString(fmt.Sprintf("(%s)", healthStatus), healthStatusColor)
}
return ""
}

View file

@ -1,9 +1,10 @@
package commands
import (
"github.com/docker/docker/api/types/container"
"os/exec"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types"
"github.com/fatih/color"
"github.com/jesseduffield/lazydocker/pkg/utils"
@ -24,11 +25,11 @@ type Service struct {
func (s *Service) GetDisplayStrings(isFocused bool) []string {
if s.Container == nil {
return []string{utils.ColoredString("none", color.FgBlue), s.Name, ""}
return []string{utils.ColoredString("none", color.FgBlue), "", s.Name, ""}
}
cont := s.Container
return []string{cont.GetDisplayStatus(), s.Name, cont.GetDisplayCPUPerc()}
return []string{cont.GetDisplayStatus(), cont.GetDisplaySubstatus(), s.Name, cont.GetDisplayCPUPerc()}
}
// Remove removes the service's containers