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

Allow editing of existing text #196

Open
syvanpera opened this issue May 17, 2019 · 15 comments
Open

Allow editing of existing text #196

syvanpera opened this issue May 17, 2019 · 15 comments
Labels

Comments

@syvanpera
Copy link

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

syvanpera commented May 17, 2019

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

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

@AlecAivazis
Copy link
Owner

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

@syvanpera
Copy link
Author

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

@AlecAivazis
Copy link
Owner

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

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

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

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

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 AlecAivazis modified the milestone: v2 May 28, 2019
@Wulfheart
Copy link

Wulfheart commented Jun 5, 2019

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

@syvanpera
Copy link
Author

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

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

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

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

mislav commented Mar 14, 2022

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: 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?

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

7 participants