func()

in cli/azd/pkg/ux/multi_select.go [131:223]


func (p *MultiSelect) Ask(ctx context.Context) ([]*MultiSelectChoice, error) {
	if p.canvas == nil {
		p.canvas = NewCanvas(p).WithWriter(p.options.Writer)
	}

	if !*p.options.EnableFiltering {
		p.cursor.HideCursor()
	}

	defer func() {
		p.cursor.ShowCursor()
	}()

	if err := p.canvas.Run(); err != nil {
		return nil, err
	}

	done := func() {
		if err := p.canvas.Update(); err != nil {
			log.Printf("Error updating canvas: %s\n", err.Error())
		}
	}

	err := p.input.ReadInput(ctx, nil, func(args *internal.KeyPressEventArgs) (bool, error) {
		defer done()

		if args.Cancelled {
			p.cancelled = true
			return false, nil
		}

		p.showHelp = args.Hint

		if *p.options.EnableFiltering {
			p.filter = strings.TrimSpace(args.Value)
		}

		// Ensure currentIndex is initialized if there are any choices.
		if p.currentIndex == nil && len(p.filteredChoices) > 0 {
			p.currentIndex = Ptr(0)
		}

		optionCount := len(p.filteredChoices)
		if optionCount > 0 {
			if args.Key == keyboard.KeyArrowUp {
				p.currentIndex = Ptr(((*p.currentIndex - 1 + optionCount) % optionCount))
			} else if args.Key == keyboard.KeyArrowDown {
				p.currentIndex = Ptr(((*p.currentIndex + 1) % optionCount))
			} else if args.Key == keyboard.KeySpace {
				choice := p.filteredChoices[*p.currentIndex]
				choice.Selected = !choice.Selected

				if choice.Selected {
					p.selectedChoices[choice.Value] = choice
				} else {
					delete(p.selectedChoices, choice.Value)
				}
			}
		}

		if args.Key == keyboard.KeyArrowRight {
			for _, choice := range p.choices {
				choice.Selected = true
				p.selectedChoices[choice.Value] = choice
			}
		} else if args.Key == keyboard.KeyArrowLeft {
			for _, choice := range p.choices {
				choice.Selected = false
				delete(p.selectedChoices, choice.Value)
			}
		}

		if args.Key == keyboard.KeyEnter {
			p.submitted = true
			p.validate()

			if !p.hasValidationError {
				p.complete = true
			}
		}

		if p.complete {
			return false, nil
		}

		return true, nil
	})
	if err != nil {
		return nil, err
	}

	return p.sortSelectedChoices(), nil
}