AlecAivazis / survey

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

Allow editing of existing text #196

Open syvanpera opened 5 years ago

syvanpera commented 5 years ago

I couldn't find a way to edit some existing text using survey. You can set the Default text for a prompt, which will show it as a grey text next to the prompt and is used if no input is given. But I'd like to be able to edit this default text and save that when enter is pressed.

syvanpera commented 5 years ago

Actually now that I think of it maybe the cleanest way to do this would be to display the text set in the response initially. So basically:

name := "Some editable text"
prompt := &survey.Input{
    Message: "ping",
}
survey.AskOne(prompt, &name, nil)

would display a prompt with "Some editable text" in it already

MarkusFreitag commented 5 years ago

Yes correct, that would be the recommended way to achieve such "dynamic" input components.

AlecAivazis commented 5 years ago

I'm going to close this since its been resolved. Thanks again for following through with this @MarkusFreitag

syvanpera commented 5 years ago

Umm, so are you saying this should work? Because at least the example I gave, does not

AlecAivazis commented 5 years ago

Ah sorry! I was under the impression @MarkusFreitag had tested it and verified it worked. I'm going to reopen this ticket and assign it a Feature tag.

syvanpera commented 5 years ago

Ah okay. I might be willing to explore if I can implement this. I'm just a bit new with the different ways go modules are built as I've only made my own modules using the new go module feature and I'm not quite sure how to start contributing to for example this project. Can you give me a quick run down on how to build and test this project?

AlecAivazis commented 5 years ago

Awesome! I'd love to help you anyway I can. This project uses run to handle various tasks so once you have downloaded that with go get github.com/alecaivazis/run you can run the tests with run tests. Since you won't have to touch any packages that are outside of survey you shouldn't need to do anything fancy for gomodules to work.

Also, if you prefer or want more immediate feedback, feel free to reach out on the gopher slack channel!

syvanpera commented 5 years ago

Okay, cool. I got run installed, but the tests are failing. Should they pass currently? Do you have own channel for this project on gophers slack?

AlecAivazis commented 5 years ago

Nope, you can just DM me 😄

They should be passing (travis has a green light). Wanna send me some screenshots of what you're seeing?

Wulfheart commented 5 years ago

@syvanpera Do you have anything public? I'd be happy to dig in. I consider this feature crucial for every cli input package.

syvanpera commented 5 years ago

Unfortunately not yet, I've been way too busy lately and next week I'm on vacation so this is going to take a while.

PierreKieffer commented 2 years ago

Up on this feature which would also interest me, has there been any particular progress? One solution would be to write this data to a temporary file and open it via the survey.Editor method, Is there a way to do this ? Something like : prompt := &survey.Editor{ Message: "Shell code snippet", FileName: "*.sh", DefaultContent : "foobar", }

And when the temp file is opened in text editor, the file is pre filled with DefaultContent

PierreKieffer commented 2 years ago

So, my mistake. After exploring the code, the method does exist via the Editor. You must pass a default value and set the AppendDefault bool to true. This method allows to open the editor (vim by default) and edit the default text value.

func Edit() error {
        content := ""

        var menu = &survey.Editor{
                Message:       "Edit : ",
                FileName:      "*.json",
                Default:       `{"foo" : "bar"}`,
                AppendDefault: true,
        }   

        err := survey.AskOne(menu, &content)
        if err != nil {
                return err 
        }   
        fmt.Println(content)
        return nil 
}
MikeDaniel18 commented 2 years ago

Has anyone been able to do this with the Input survey type? I had a crack at it but didn't have much luck. @AlecAivazis any thoughts on this? Is this a simple implementation for someone familiar with the codebase?

mislav commented 2 years ago

I do not think it's possible with the current state of Survey, but I think it could be possible with minimal changes. I've made a spike here: https://github.com/AlecAivazis/survey/compare/master...mislav:default-editable

I do not think that Survey can shoehorn this new behavior of Default in a backwards-compatible way, but maybe there could be a new option that the user could opt into for each prompt?