mirror of
https://github.com/jesseduffield/lazydocker.git
synced 2026-07-21 23:01:03 +00:00
Used RenderTable to dispaly environemnt varialbles also added a check in RenderTable method to make sure code does not panic when empty array is passed
This commit is contained in:
parent
af2eb0bb84
commit
32aa08066c
2 changed files with 17 additions and 15 deletions
|
|
@ -98,30 +98,28 @@ func (gui *Gui) renderContainerEnv(container *commands.Container) error {
|
|||
mainView := gui.getMainView()
|
||||
mainView.Autoscroll = false
|
||||
mainView.Wrap = gui.Config.UserConfig.Gui.WrapMainPanel
|
||||
outputList := []string{""}
|
||||
envMapping := map[string]string{}
|
||||
padding := 0
|
||||
envVariablesList := [][]string{}
|
||||
if len(container.Details.Config.Env) > 0 {
|
||||
for _, env := range container.Details.Config.Env {
|
||||
splitEnv := strings.Split(env, "=")
|
||||
// get the length of environment variable with longest name in order to determine padding
|
||||
if len(splitEnv[0]) > padding {
|
||||
padding = len(splitEnv[0])
|
||||
}
|
||||
// if the value has = in it, lets say export "test=foo=bar" then split will result in the following
|
||||
// {"test", "foo","bar"} hence join all the elements in the slice except the first one to get value
|
||||
envMapping[utils.ColoredString(splitEnv[0], color.FgBlue)] = utils.ColoredString(
|
||||
strings.Join(splitEnv[1:], "="), color.FgYellow)
|
||||
}
|
||||
padding += 5
|
||||
for envName, envValue := range envMapping {
|
||||
outputList = append(outputList, fmt.Sprintf("%s: %s", utils.WithPadding(envName, padding), envValue))
|
||||
envVariablesList = append(envVariablesList,
|
||||
[]string{
|
||||
utils.ColoredString(splitEnv[0]+":", color.FgBlue),
|
||||
utils.ColoredString(strings.Join(splitEnv[1:], "="), color.FgYellow),
|
||||
})
|
||||
}
|
||||
} else {
|
||||
outputList[0] = "Nothing to display"
|
||||
envVariablesList = append(envVariablesList, []string{"Nothing to display"})
|
||||
}
|
||||
renderedTable, err := utils.RenderTable(envVariablesList)
|
||||
// in case of some error
|
||||
if err != nil {
|
||||
renderedTable = "Something went wrong while displaying environment variables"
|
||||
}
|
||||
return gui.T.NewTask(func(stop chan struct{}) {
|
||||
gui.renderString(gui.g, "main", strings.Join(outputList, "\n"))
|
||||
gui.renderString(gui.g, "main", renderedTable)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -165,6 +165,10 @@ func renderDisplayableList(items []Displayable, config RenderListConfig) (string
|
|||
|
||||
// RenderTable takes an array of string arrays and returns a table containing the values
|
||||
func RenderTable(stringArrays [][]string) (string, error) {
|
||||
// if empty array is given then getPadWidths will panic hence check to make sure array is not empty
|
||||
if len(stringArrays) == 0 {
|
||||
return "", errors.New("RenderTable given an empty array")
|
||||
}
|
||||
if !displayArraysAligned(stringArrays) {
|
||||
return "", errors.New("Each item must return the same number of strings to display")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue