AlecAivazis / survey

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

Is there a way to write data in temp file before opening Editor ? #400

Closed PierreKieffer closed 2 years ago

PierreKieffer commented 2 years ago

I would like to modify data that already exists. 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

Or maybe there is another solution to edit something as input? thanks in advance

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 
}