c-bata / go-prompt

Building powerful interactive prompts in Go, inspired by python-prompt-toolkit.
https://godoc.org/github.com/c-bata/go-prompt
MIT License
5.27k stars 346 forks source link

[Question] Force prompt.Input() redraw during goroutine #255

Open Paradoxis opened 2 years ago

Paradoxis commented 2 years ago

Question

I'm working on an application where users are able to execute commands on a server, for this I decided to use this project (great work by the way!), some of these commands are asynchronous jobs which I run using goroutines. I'd like to write the output in the same terminal, however calling things like fmt.Println() breaks the current render.

If there a way I could do this? Eg using a method like InsertLineAboveInput() or something? Or maybe forcibly re-drawing the output? This is the example code I'm working with:


func pollOutput() {
    for {
        time.Sleep(time.Second))
        fmt.Println("Example line")  // This breaks the output
        prompt.InsertLineAboveInput("Example line")  // Something like this, force redraw, etc
    }
}

func main() {
    go pollOutput()  // Example, real implementation is more complex

    term := prompt.New(
        executor,
        completer,
        prompt.OptionLivePrefix(func() (string, bool) {
            if context == "" {
                return "shell>", true
            } else {
                return fmt.Sprintf("shell(%s)> ", context), true
            }
        }),
    )

    term.Run()
}

Thanks a bunch for the reply!