AlecAivazis / survey

A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
MIT License
4.07k stars 349 forks source link

Input suggestion problem #452

Open nemati21 opened 1 year ago

nemati21 commented 1 year ago

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?

nhatthm commented 1 year ago

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:

nhatthm commented 1 year ago

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
nemati21 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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:

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

nemati21 commented 1 year ago

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 commented 1 year ago

Thanks both! I managed to reproduce the problem.

x64bugreport commented 10 months ago

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