survey icon indicating copy to clipboard operation
survey copied to clipboard

Input suggestion problem

Open nemati21 opened this issue 2 years ago • 8 comments

When I select an item in the input suggestion with the enter button, the previous question is removed from CLI. How can I fix it?

nemati21 avatar Sep 19 '22 09:09 nemati21

I have the same problem, tested by compiling with all the versions back til 2.2.13 which doesn't have the issue. This is the only PR in 2.2.14

My system:

  • macOS 12.6
  • zsh 5.9
  • GOARCH="amd64"

nhatthm avatar Sep 22 '22 20:09 nhatthm

If I remove this line

https://github.com/AlecAivazis/survey/blob/a98a037e3f0b51b8a992bfdd4c50debb70a53553/input.go#L186

The answer is correctly printed after selecting a suggestion.

However, the random answer (without selecting a suggestion) is not, it's printed one line after the prompt. For example

? Topic [tab for suggestions] asd
? Topic asd

nhatthm avatar Sep 22 '22 20:09 nhatthm

I customized the InputQuestionTemplate and removed "enter to select" from the shown message that the user uses the right arrow to select an item but it isn't a good solution. it must be fixed

nemati21 avatar Sep 26 '22 07:09 nemati21

Hi, thanks for the reports! Please try to create some code that uses Survey that acts as a small reproduction case for the problem. For examples on how this can look like, please see the ./examples directory in this repo.

Even a screenshot from your terminal (before and after the line in question disappears) would be helpful.

Additionally, when reporting these issues, please also tell us your OS and terminal emulator used.

mislav avatar Sep 28 '22 14:09 mislav

Code

package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/AlecAivazis/survey/v2"
)

func main() {
	var answer string

	suggests := []string{"Amelia", "Ava", "Elijah", "Emma", "Liam", "Mateo", "Noah", "Oliver", "Olivia", "Sophia"}

	err := survey.AskOne(&survey.Input{
		Message: "What is your name?",
		Suggest: func(toComplete string) []string {
			if len(toComplete) == 0 {
				return suggests
			}

			var matches []string

			for _, s := range suggests {
				if strings.Contains(s, toComplete) {
					matches = append(matches, s)
				}
			}

			return matches
		},
	}, &answer)

	if err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		os.Exit(1)
	}

	// print the answers
	fmt.Printf("Hi %s.\n", answer)
}

Env:

  • macOS 12.6
  • zsh 5.9
  • iTerm2 3.4.16

Stable version 2.3.6

https://user-images.githubusercontent.com/1154587/192954112-80f27f6f-861e-4e89-996b-f00e4a0a3d5b.mov

Last working version 2.2.13

https://user-images.githubusercontent.com/1154587/192954747-9c09858d-071e-41a3-850a-b23b9a2c3e4e.mov

nhatthm avatar Sep 29 '22 06:09 nhatthm

Code:

package main

import (
	"fmt"
	"path/filepath"

	"github.com/AlecAivazis/survey/v2"
)

const questionCount = 2
var currentQuestion = 1

func main() {
	var fileName, filePath string

	fileNameQs := &survey.Input{Message: `File Name: ("sample")`}
	_ = survey.AskOne(fileNameQs, &fileName, survey.WithValidator(survey.Required), survey.WithIcons(setupQuestionIcon))

	filePathQs := &survey.Input{Message: `File Path: ("/home/snapp/go")`, Suggest: func(toComplete string) []string {
		directories, _ := filepath.Glob(toComplete + "*")
		return directories
	},
	}

	_ = survey.AskOne(filePathQs, &filePath, survey.WithValidator(survey.Required), survey.WithIcons(setupQuestionIcon))

	fmt.Printf("\nFile %s saved in %s\n", fileName, filePath)
}

func setupQuestionIcon(icon *survey.IconSet) {
	icon.Question.Text = fmt.Sprintf("[%d/%d]", currentQuestion, questionCount)
	currentQuestion++
}

Env: OS: Ubuntu 22.04

Select an item by enter: https://drive.google.com/file/d/18850bWyvFNihPF5qq1LuOu5L9LpajeVg/view?usp=sharing

Select an item by arrow or type: https://drive.google.com/file/d/1g7f7C-3rp3YBGYeUcQOh4Qa2U72kjq_0/view?usp=sharing

nemati21 avatar Oct 17 '22 17:10 nemati21

Thanks both! I managed to reproduce the problem.

mislav avatar Oct 17 '22 18:10 mislav

@mislav This problem still exists, when will it be fixed?

x64bugreport avatar Aug 28 '23 09:08 x64bugreport