peterh / liner

Pure Go line editor with history, inspired by linenoise
MIT License
1.04k stars 132 forks source link

Mechanism for stopping a State without destroying it? #41

Closed jnjackins closed 9 years ago

jnjackins commented 9 years ago

I have some code like this:

        for {
            prompt := liner.NewLiner()
            line, err := prompt.Prompt("> ")
            prompt.Close()
            if err != nil {
                if err == io.EOF {
                    return
                }
                log.Print(err)
            } else {
                parse(line)
            }
        }

I need to close prompt because parse may need to take over stdin. It seems wasteful to call NewLiner with each iteration; is there interest in adding methods like Start and Stop in addition to Close?

jnjackins commented 9 years ago

A better justification: my current method means I can't use the history feature without calling ReadHistory and WriteHistory on each iteration.

jnjackins commented 9 years ago

Here is a quick hack that solves my issue: https://github.com/jnjackins/liner/commit/4a96ae625abcc0e981b5e20562b51036c9b11f24

peterh commented 9 years ago

Thanks for the suggestion.

TerminalMode() already exists to allow you to reset and restore the terminal mode.

Start/Stop is probably a cleaner interface, but unfortunately it is too late to go back in time and remove TerminalMode.

jnjackins commented 9 years ago

Ah, somehow I didn't catch that TerminalMode is what I wanted. Obvious in hindsight. Thanks!