Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Input suggestion problem #452

Open
nemati21 opened this issue Sep 19, 2022 · 8 comments
Open

Input suggestion problem #452

nemati21 opened this issue Sep 19, 2022 · 8 comments
Labels

Comments

@nemati21
Copy link

nemati21 commented Sep 19, 2022

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 nemati21 changed the title Suggestion problem Input suggestion problem Sep 19, 2022
@nhatthm
Copy link

nhatthm commented Sep 22, 2022

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
Copy link

nhatthm commented Sep 22, 2022

If I remove this line

survey/input.go

Line 186 in a98a037

cursor.Up(1)

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

@nemati21
Copy link
Author

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

@mislav
Copy link
Collaborator

mislav commented Sep 28, 2022

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.

@nhatthm
Copy link

nhatthm commented Sep 29, 2022

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

Screen.Recording.2022-09-29.at.08.17.20.mov

Last working version 2.2.13

Screen.Recording.2022-09-29.at.08.23.10.mov

@nemati21
Copy link
Author

nemati21 commented Oct 17, 2022

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

@mislav
Copy link
Collaborator

mislav commented Oct 17, 2022

Thanks both! I managed to reproduce the problem.

@x64bugreport
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants