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

Updates to the YesNo prompt #29

Closed zeroSteiner closed 5 years ago

zeroSteiner commented 5 years ago

This PR makes the following three notable changes to the bullet.client.YesNo class.

  1. Fixes a bug so that when default='y' is specified, the .launch() method returns True.
    • Reproduction steps below
  2. Makes the prompt prefix an optional keyword argument to YesNo.__init__. This specifically will let users remove if they'd like or set it to something like [Y/n] to make the default obvious.
  3. Supports answers of full "Yes" and "No" for users that like to be explicit. Technically "Ye" will evaluate to yes since there's no overlap with No.

Demonstration

In [1]: import bullet                                                                                                                                                                                              

In [2]: bullet.YesNo('Are you hungry? ').launch(default='y')                                                                                                                                                       
[y/n] Are you hungry? 
Out[2]: True

In [3]: bullet.YesNo('Are you hungry? ').launch(default='y')                                                                                                                                                       
[y/n] Are you hungry? yes
Out[3]: True

In [4]: bullet.YesNo('Are you hungry? ').launch(default='y')                                                                                                                                                       
[y/n] Are you hungry? no
Out[4]: False

In [5]: bullet.YesNo('Are you hungry? ').launch(default='y')                                                                                                                                                       
[y/n] Are you hungry? n
Out[5]: False

In [6]: bullet.YesNo('Are you hungry? [Y/n] ', prompt_prefix='').launch(default='y')                                                                                                                               
Are you hungry? [Y/n] 
Out[6]: True

Bug Reproduction / Demonstration

In [1]: import bullet                                                                                                                                                                                              

In [2]: bullet.YesNo('Are you hungry? ').launch(default='y')                                                                                                                                                       
[y/n] Are you hungry? 
Out[2]: False

Thanks!

bchao1 commented 5 years ago

Thanks!