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:
glendsoza 2022-01-14 12:27:30 +05:30
parent af2eb0bb84
commit 32aa08066c
2 changed files with 17 additions and 15 deletions

View file

@ -98,30 +98,28 @@ func (gui *Gui) renderContainerEnv(container *commands.Container) error {
mainView := gui.getMainView() mainView := gui.getMainView()
mainView.Autoscroll = false mainView.Autoscroll = false
mainView.Wrap = gui.Config.UserConfig.Gui.WrapMainPanel mainView.Wrap = gui.Config.UserConfig.Gui.WrapMainPanel
outputList := []string{""} envVariablesList := [][]string{}
envMapping := map[string]string{}
padding := 0
if len(container.Details.Config.Env) > 0 { if len(container.Details.Config.Env) > 0 {
for _, env := range container.Details.Config.Env { for _, env := range container.Details.Config.Env {
splitEnv := strings.Split(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 // 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 // {"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( envVariablesList = append(envVariablesList,
strings.Join(splitEnv[1:], "="), color.FgYellow) []string{
} utils.ColoredString(splitEnv[0]+":", color.FgBlue),
padding += 5 utils.ColoredString(strings.Join(splitEnv[1:], "="), color.FgYellow),
for envName, envValue := range envMapping { })
outputList = append(outputList, fmt.Sprintf("%s: %s", utils.WithPadding(envName, padding), envValue))
} }
} else { } 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{}) { return gui.T.NewTask(func(stop chan struct{}) {
gui.renderString(gui.g, "main", strings.Join(outputList, "\n")) gui.renderString(gui.g, "main", renderedTable)
}) })
} }

View file

@ -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 // RenderTable takes an array of string arrays and returns a table containing the values
func RenderTable(stringArrays [][]string) (string, error) { 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) { if !displayArraysAligned(stringArrays) {
return "", errors.New("Each item must return the same number of strings to display") return "", errors.New("Each item must return the same number of strings to display")
} }