petereon / beaupy

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

KeyboardInterrupt exception in select_multiple #94

Closed Schwitzd closed 3 weeks ago

Schwitzd commented 3 weeks ago

I have this code snipped:

try:
    selected_apps = select_multiple(bloatware_detected, pagination=True, page_size=20)
except KeyboardInterrupt as ki:
    raise SystemExit() from ki

When I press CTRL+C during the selection the KeyboardInterrupt exception is bypassed.

I'm using the latest release of beaupy.

Cheers

petereon commented 3 weeks ago

Default behavior among all the CLI elements of beaupy is not to raise on keyboard interrupt. If you'd prefer it to raise, you can set this behavior as follows:

from beaupy import select_multiple, Config

Config.raise_on_interrupt = True

try:
    selected_apps = select_multiple(bloatware_detected, pagination=True, page_size=20)
except KeyboardInterrupt as ki:
    raise SystemExit() from ki

Please let me know if this fits your use case.

EDIT: For completeness, I'd like to mention another config option Config.raise_on_escape, which controls whether beaupy raises on the press of the ESC key while in CLI context. This raises a custom exception - Abort. The two config options in tandem can be used to differentiate between a "hard" exit in case of CTRL+C and a "soft" exit in case of ESC.

Schwitzd commented 3 weeks ago

wooow so fast, exactly what I was looking for <3