mirror of
https://github.com/jesseduffield/lazydocker.git
synced 2026-07-21 23:01:03 +00:00
create list panel struct
This commit is contained in:
parent
cb38e48add
commit
dcce4c67ec
4 changed files with 89 additions and 25 deletions
|
|
@ -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{}) {
|
||||
|
|
|
|||
45
pkg/gui/list_panel.go
Normal file
45
pkg/gui/list_panel.go
Normal file
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue