bchao1 / bullet

🚅 Interactive prompts made simple. Build a prompt like stacking blocks.
https://pypi.org/project/bullet/
MIT License
3.55k stars 113 forks source link

do not ignore ctrl+c on linux #15 #16

Closed cs01 closed 5 years ago

cs01 commented 5 years ago

CTRL+C worked fine on mac, but on ubuntu it's not working. This is because when stdin's fd is set to raw mode, special processing of special characters (yes those are the terms used in the man page :laughing: ) is no longer performed (see the INTR character in http://man7.org/linux/man-pages/man3/termios.3.html). This means the terminal doesn't translate characters into actions, it just passes along the character's raw data.

This PR

cc @florimondmanca

florimondmanca commented 5 years ago

Just gave this a try @cs01 and macOS doesn't seem to enjoy this. 😂 See below — strange formatting not mine.

23:41 in bullet on  master via bullet via 🐍 3.7.2
$ python examples/colorful.py

Please choose a fruit:
Traceback (most recent call last):
        banana                      File "examples/colorful.py", line 20, in <module>   orange
         result = cli.launch()
        strawberry              File "/Users/Florimond/Desktop/bullet/bullet/client.py", line 200, in launch
                                c = utils.getchar()
                                                     File "/Users/Florimond/Desktop/bullet/bullet/utils.py", line 53, in getchar
                                                    c = mygetc()
                                                                  File "/Users/Florimond/Desktop/bullet/bullet/utils.py", line 43, in mygetc
                                                                with make_raw_except_interrupt(fd):
                     File "/Users/Florimond/.pyenv/versions/3.7.2/lib/python3.7/contextlib.py", line 112, in __enter__
                                          return next(self.gen)
                                                                 File "/Users/Florimond/Desktop/bullet/bullet/utils.py", line 33, in make_raw_except_interrupt
                                                                                  _enable_ctrl_c(fd)
                      File "/Users/Florimond/Desktop/bullet/bullet/utils.py", line 26, in _enable_ctrl_c
                            termios.tcsetattr(fd, termios.TCIOFLUSH, mode)
                                                                          termios.error: (22, 'Invalid argument')

Also after this fails and exits, I'm not able to Ctrl+C in my terminal anymore, the prompt is off (see below) and I have to restart a terminal session to get back to normal.

23:41 in bullet on  master via bullet via 🐍 3.7.2
                                                    $
rcfox commented 5 years ago

Just curious: Why not just handle chr(3) (the Ctrl-C character)?

bchao1 commented 5 years ago

Just checked it out, maybe add an KEYBOARD_INTERRUPT flag in the launch() function to indicate force-exit(Ctrl+C)? Then move the cursor down to the last item position (the console would look messy if the prompt is force-exited lol).

cs01 commented 5 years ago

@rcfox I see you already added this to https://github.com/Mckinsey666/bullet/pull/20, so I will close this PR.

Just checked it out, maybe add an KEYBOARD_INTERRUPT flag in the launch() function to indicate force-exit (Ctrl+C)?

IMO the default behavior of a tool like this should definitely not ignore ctrl+c. Are there any cases the user would want to use the value ctrl+c? I've never used a cli tool that didn't let you get out by pressing ctrl+c.

Just curious: Why not just handle chr(3) (the Ctrl-C character)?

No particular reason. Given @florimondmanca's trouble it might not have been the best way to go. I think your PR will work nicely though.