Skip to content

[Bug] Usage of Ctrl+C binding corrupt STTY settings #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sattellite opened this issue May 26, 2020 · 4 comments
Closed

[Bug] Usage of Ctrl+C binding corrupt STTY settings #176

sattellite opened this issue May 26, 2020 · 4 comments
Labels

Comments

@sattellite
Copy link

Bug reports

Expected Behavior

After my program is finished with ControlC binding, I expect that Ctrl+C combination will work in the terminal as it did before the program launch.

Current Behavior and Steps to Reproduce

After finished my program by ControlC binding and Ctrl+C combination do nothing.

p := prompt.New(
	// ... other options
	prompt.OptionAddKeyBind(prompt.KeyBind{
		Key: prompt.ControlC,
		Fn: func(b *prompt.Buffer) { os.Exit(0) }
	}),
)

p.Run()

If I send text command to executor for exit it calls os.Exit(0) too and program finished correctly.

Program exit if I press Ctrl+C in opened prompt and stty settings is corrupted.

I got settings before run program with stty -a > stty.before and after finish program by CTRL+C with stty -a > stty.after.

--- stty.before	2020-05-26 15:30:40.827199004 +0300
+++ stty.after	2020-05-26 15:30:47.212105711 +0300
@@ -3,8 +3,8 @@
 eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
 werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
 -parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
--ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
+-ignbrk -brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff
 -iuclc -ixany imaxbel iutf8
 opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig icanon -iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
+-isig icanon -iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
 echoctl echoke -flusho -extproc

Disabled this options: -brkint -icrnl -ixon -isig

Option isig enable interrupt, quit, and suspend special characters.

More info about stty https://linux.die.net/man/1/stty

For restore initial behavior need use command reset.

Context

  • Operating System: Fedora Linux, CentOS
  • Terminal Emulator: gnome-terminal
  • commit revision: d043be0
@sattellite sattellite added the bug label May 26, 2020
@chriswalz
Copy link

Is there a workaround for this?

@sattellite
Copy link
Author

Is there a workaround for this?

I using my CLI command with reset after it, but this is ugly hack
my-cli-command && reset

@sattellite
Copy link
Author

Found workaround in chriswalz/bit. Code sample here.

At begin of programm add defered exit handler with recover. Deferred function calls are executed in Last In First Out order. It will be called by the most recent. And bind a function with panic to ControlC combination.

...
func main() {
	defer handleExit()
	p := prompt.New(
		// ... other options
		prompt.OptionAddKeyBind(prompt.KeyBind{
			Key: prompt.ControlC,
			Fn: exit
		}),
	)

	p.Run()
}

type Exit int

func exit(_ *prompt.Buffer) {
	panic(Exit(0))
}

func handleExit() {
        v := recover()
	switch v.(type) {
	case nil:
		return
	case Exit:
		os.Exit(int(v))
	default:
		fmt.Printf("%+v", v)
	}
}
...

@josephspurrier
Copy link

MR: #239 resolves this problem. The code sample above no longer works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants