olup otilapan
olup otilapan

Reputation: 504

Multiline input in console

Been looking a bit everywhere for this, but nothing quite answers it.

I can create a multiline prompt input in the console with Go by having a scan or bufio.readstring loop, and check for an end-of-input character to end input. Or I can even pass whatever character to bufio.readstring so that I can keep reading after \n has been entered.

But in both cases, user can not backspace its way to a precedent line, because the previous line have been entered and validated.

How would it work, to let user backspace to the previous line, or move cursor freely between lines of a console input ?

I can imagine something with clearing terminal and redrawing. Is that the only way ?

Upvotes: 2

Views: 894

Answers (1)

Vorsprung
Vorsprung

Reputation: 34307

You could use a readline library, as this demo shows

package main

import (
    "github.com/chzyer/readline"
)

func main() {
    rl, err := readline.NewEx(&readline.Config{
        Prompt:                 "> ",
        HistoryFile:            "/tmp/readline-multiline",
        DisableAutoSaveHistory: true,
    })
    if err != nil {
        panic(err)
    }
    defer rl.Close()

    for {
        cmd, err := rl.Readline()
        if err != nil {
            break
        }
        rl.SaveHistory(cmd)
    }
}

Upvotes: 2

Related Questions