func newLayoutButtons()

in pkg/display/graph/dashboard/global.go [111:143]


func newLayoutButtons(c *container.Container) ([]*button.Button, error) {
	buttons := make([]*button.Button, len(strToLayoutType))

	ls := longestString(template.Buttons.Texts)
	if ls == "" {
		return nil, fmt.Errorf("failed to parse texts of buttons")
	}

	opts := []button.Option{
		button.WidthFor(ls),
		button.FillColor(cell.ColorNumber(template.Buttons.ColorNum)),
		button.Height(template.Buttons.Height),
	}

	for _, text := range template.Buttons.Texts {
		// declare a local variable lt to avoid closure.
		lt, ok := strToLayoutType[text]
		if !ok {
			return nil, fmt.Errorf("the '%s' is not supposed to be the button's text", text)
		}

		b, err := button.New(text, func() error {
			return setLayout(c, lt)
		}, opts...)
		if err != nil {
			return nil, err
		}

		buttons[lt] = b
	}

	return buttons, nil
}