diff --git a/pkg/gui/containers_panel.go b/pkg/gui/containers_panel.go index 4f6bb941..2e245ab2 100644 --- a/pkg/gui/containers_panel.go +++ b/pkg/gui/containers_panel.go @@ -13,6 +13,7 @@ import ( "github.com/jesseduffield/lazydocker/pkg/commands" "github.com/jesseduffield/lazydocker/pkg/config" "github.com/jesseduffield/lazydocker/pkg/utils" + "github.com/samber/lo" ) // list panel functions @@ -95,41 +96,46 @@ func (gui *Gui) handleContainerSelect(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) renderContainerEnv(container *commands.Container) error { if !container.DetailsLoaded() { return gui.T.NewTask(func(stop chan struct{}) { - _ = gui.renderString(gui.g, "main", gui.Tr.WaitingForContainerInfo) + _ = gui.renderStringMain(gui.Tr.WaitingForContainerInfo) }) } mainView := gui.getMainView() mainView.Autoscroll = false mainView.Wrap = gui.Config.UserConfig.Gui.WrapMainPanel - envVariablesList := [][]string{} - renderedTable := gui.Tr.NothingToDisplay - if len(container.Details.Config.Env) > 0 { - var err error - for _, env := range container.Details.Config.Env { - splitEnv := strings.SplitN(env, "=", 2) - key := splitEnv[0] - value := "" - if len(splitEnv) > 1 { - value = splitEnv[1] - } - envVariablesList = append(envVariablesList, - []string{ - utils.ColoredString(key+":", color.FgGreen), - utils.ColoredString(value, color.FgYellow), - }) - } - renderedTable, err = utils.RenderTable(envVariablesList) - if err != nil { - gui.Log.Error(err) - renderedTable = gui.Tr.CannotDisplayEnvVariables - } - } + return gui.T.NewTask(func(stop chan struct{}) { - _ = gui.renderString(gui.g, "main", renderedTable) + _ = gui.renderString(gui.g, "main", gui.containerEnv(container)) }) } +func (gui *Gui) containerEnv(container *commands.Container) string { + if len(container.Details.Config.Env) == 0 { + return gui.Tr.NothingToDisplay + } + + envVarsList := lo.Map(container.Details.Config.Env, func(envVar string, _ int) []string { + splitEnv := strings.SplitN(envVar, "=", 2) + key := splitEnv[0] + value := "" + if len(splitEnv) > 1 { + value = splitEnv[1] + } + return []string{ + utils.ColoredString(key+":", color.FgGreen), + utils.ColoredString(value, color.FgYellow), + } + }) + + output, err := utils.RenderTable(envVarsList) + if err != nil { + gui.Log.Error(err) + return gui.Tr.CannotDisplayEnvVariables + } + + return output +} + func (gui *Gui) renderContainerConfig(container *commands.Container) error { if !container.DetailsLoaded() { return gui.T.NewTask(func(stop chan struct{}) { diff --git a/pkg/gui/list_panel.go b/pkg/gui/list_panel.go new file mode 100644 index 00000000..d1db53eb --- /dev/null +++ b/pkg/gui/list_panel.go @@ -0,0 +1,45 @@ +package gui + +import ( + "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazydocker/pkg/utils" +) + +type ListPanel[T comparable] struct { + toColumns func(T) []string + + selectedIdx int + list FilteredList[T] + view *gocui.View +} + +func (self *ListPanel[T]) setSelectedLineIdx(value int) { + clampedValue := -1 + if self.list.Len() > 0 { + clampedValue = utils.Clamp(value, 0, self.list.Len()-1) + } + + self.selectedIdx = clampedValue +} + +// moves the cursor up or down by the given amount (up for negative values) +func (self *ListPanel[T]) moveSelectedLine(delta int) { + self.setSelectedLineIdx(self.selectedIdx + delta) +} + +func (self *ListPanel[T]) SelectNextLine() { + self.moveSelectedLine(1) +} + +func (self *ListPanel[T]) SelectPrevLine() { + self.moveSelectedLine(-1) +} + +// list panel at the side of the screen that renders content to the main panel +type SideListPanel[T comparable] struct { + contextKeyPrefix string + + ListPanel[T] + + contextIdx int +} diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index e597e21e..fc344aff 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -258,6 +258,10 @@ func (gui *Gui) renderString(g *gocui.Gui, viewName, s string) error { return nil } +func (gui *Gui) renderStringMain(s string) error { + return gui.renderString(gui.g, "main", s) +} + // reRenderString sets the main view's content, without changing its origin func (gui *Gui) reRenderStringMain(s string) { gui.reRenderString("main", s) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index fe1ae3de..187ee09a 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -408,3 +408,12 @@ func IsValidHexValue(v string) bool { return true } + +func Clamp(x int, min int, max int) int { + if x < min { + return min + } else if x > max { + return max + } + return x +}