survey icon indicating copy to clipboard operation
survey copied to clipboard

Allow editing of existing text

Open syvanpera opened this issue 5 years ago • 15 comments

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 avatar May 17 '19 17:05 syvanpera

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

syvanpera avatar May 17 '19 17:05 syvanpera

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

MarkusFreitag avatar May 17 '19 22:05 MarkusFreitag

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

AlecAivazis avatar May 24 '19 16:05 AlecAivazis

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

syvanpera avatar May 24 '19 17:05 syvanpera

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.

AlecAivazis avatar May 24 '19 19:05 AlecAivazis

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?

syvanpera avatar May 25 '19 06:05 syvanpera

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!

AlecAivazis avatar May 25 '19 06:05 AlecAivazis

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?

syvanpera avatar May 25 '19 06:05 syvanpera

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?

AlecAivazis avatar May 25 '19 07:05 AlecAivazis

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

Wulfheart avatar Jun 05 '19 13:06 Wulfheart

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.

syvanpera avatar Jun 05 '19 16:06 syvanpera

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 avatar Jan 18 '22 19:01 PierreKieffer

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 
}

PierreKieffer avatar Jan 19 '22 07:01 PierreKieffer

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?

MikeDaniel18 avatar Mar 13 '22 19:03 MikeDaniel18

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

  • The Default value is now editable (instead of just shown in parentheses as part of the prompt);
  • This feature makes the most sense when the cursor is visible, otherwise editing is hard;
  • Submitting an empty value no longer takes to the value Default as the answer.

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?

mislav avatar Mar 14 '22 16:03 mislav