peterh/liner

Unexpected behaviour when sending SIGINT to long prompts

adamroyjones opened this issue · 2 comments

I'm running Debian (sid) with Alacritty as my terminal emulator. (I can also reproduce the issue below with Gnome Terminal.)

If I call the Prompt method with a long string and then send a SIGINT, the program will exit (with a status code of 1) instead of cancelling the prompt. This example reproduces it:

package main

import (
	"fmt"
	"reflect"
	"strings"

	"github.com/peterh/liner"
)

func main() {
	l := liner.NewLiner()
	cols := int(reflect.ValueOf(l).Elem().FieldByName("columns").Int())
	var err error

	// Ctrl-C resets the prompt, as expected.
	_, err = l.Prompt(genString(cols - 10))
	fmt.Printf("err: %v\n", err)

	// This kills the program when hitting Ctrl-C, exiting with a status code of 1.
	_, err = l.Prompt(genString(cols - 9))
	fmt.Printf("err: %v\n", err)
}

func genString(n int) string {
	var s strings.Builder
	for i := 0; i < n; i++ {
		s.WriteString("a")
	}
	return s.String()
}

The issue is the call here to the ReadLine method from bufio. (This is reached from this region.)

I'm not (immediately) sure what the right fix would be. You've no doubt thought a lot more about these kinds of questions than I have—what do you think?

peterh commented

I'm not (immediately) sure what the right fix would be. You've no doubt thought a lot more about these kinds of questions than I have—what do you think?

I think that prompts should be short and to the point. > or similar, with maybe a single word to indicate mode or perhaps one number (eg. line number or sequence number) at most.

I think that the correct fix is to document that liner.Prompt will go into "unsupported mode" if you try to use a long prompt (which is what it does and why you see Ctrl-C breaking). I realize that this doesn't help when the user shrinks their terminal emulator to a tragically small width, but liner isn't really useful in that scenario anyway.

Thanks for getting back to me—I agree about the shape an appropriate prompt ought to have and that documentation is probably the best approach. I know that the above example of mine is... pathological...