petereon / beaupy

A Python library of interactive CLI elements you have been looking for
https://petereon.github.io/beaupy/
MIT License
183 stars 13 forks source link

Using ESC as an interrupt key #32

Closed MatanRubin closed 1 year ago

MatanRubin commented 1 year ago

Hi, I'm trying to define ESC as an interrupt key so that I can later use it as a "back" button in my application.

When I tried just adding it to DefaultKeys.interrupt, it didn't work... I tried debugging it and I saw a bunch of issues readchar has with capturing single ESC hits (because it waits for another key that follows ESC, or something like that).

While I'm sure readchar has good technical arguments there, it seems weird that there isn't a simple solution to that, because many programs (e.g. vim) use ESC to do stuff, even when it's not followed by another key.

Have you encountered this issue? Any idea of how to work around it?

My current workaround is to to add a double-ESC to the interrupt keys, but that seems silly:

beaupy.DefaultKeys.interrupt.append(readchar.key.ESC + readchar.key.ESC)
petereon commented 1 year ago

Hi, thank you for reporting. It's a use case I should have thought about but didn't, I'll investigate whether there is some sound workaround within beaupy.

I will also try to reach out to maintainer of readchar, but I am currently fairly cluttered with my day-job so it might not be quite as fast.

petereon commented 1 year ago

I did some research, seems like readchar maintainer is aware but given his broad scope he can't fix this in the upstream (https://github.com/magmax/python-readchar/issues/94).

However, given beaupy only scopes reading of characters down to console form elements, it might be viable to implement this in beaupy itself. I'll need some time to ponder it and do some additional research into this. I'll try to get around to this sometime this week.

petereon commented 1 year ago

I didn't feel like monkey-patching readchar so I switched to click for input handling. Related PR https://github.com/petereon/beaupy/pull/34

Cube707 commented 1 year ago

you could also switch to readchar.readchar() instead of readchar.readkey() and handle each character yourself. You however lose the convenient auto-key-building inside readkey() that returnes you ARROW key and similar.