rich icon indicating copy to clipboard operation
rich copied to clipboard

[REQUEST] option to respond to choices with digits

Open tlambert03 opened this issue 4 years ago • 9 comments

How would you improve Rich?

related to #306, It would be nice to make it easier to pick from a variety of annoying-to-type choices:

choices = ['Some really long and annoying thing to type', 'Another really long...']
Prompt.ask('pick one', choices=choices, with_digits=True)

could prompt as follows (or something like this ... where the valid responses are now digits)

[1] Some really long and annoying thing to type
[2] Another really long...
pick one [1/2]: 

What problem does it solve for you?

makes it much easier to request categorical values

in #306, readline was mentioned, but I didn't follow, so let me know if there's already a better way to do this. thanks for rich!!

tlambert03 avatar Dec 23 '21 19:12 tlambert03

So I don't know if I should've done this lol but I had a stab at this. (First time doing this so still scared to make a PR :eyes:)

TheTrio avatar Dec 24 '21 18:12 TheTrio

nice! just tried it out, and it works for me. exactly what I was looking for.

only thought I had (and i'm not sure about this) is whether it should be possible to use either the number or the text. So, given the prompt

[1] choice a
[2] choice b
pick_one (1): 

entering either "1" or "choice a" would be valid and give "choice a", etc...

that's more of a stylistic API choice that's up to @willmcgugan

but this would be great for me. maybe make a PR and see if it's ok with @willmcgugan?

tlambert03 avatar Dec 24 '21 19:12 tlambert03

only thought I had (and i'm not sure about this) is whether it should be possible to use either the number or the text.

Yes, that could be an option.

but this would be great for me. maybe make a PR and see if it's ok with @willmcgugan?

I was just waiting for him to green-light the idea before we got into implementation details of the PR. Not really sure of how this all is supposed to work lol.

TheTrio avatar Dec 24 '21 21:12 TheTrio

I was just waiting for him to green-light the idea before we got into implementation details of the PR.

opening a PR is a very reasonable thing to do, even if you're just proposing something :) It will make it easier for him to directly comment on your implementation, to voice concerns or request changes inline. If he's opposed, it's trivial to say thanks but no thanks and close it :)

tlambert03 avatar Dec 24 '21 21:12 tlambert03

side note: there's a couple places in the code where you need to check whether self.choices is not None (e.g. before indexing into it with self.choices.index). not sure if you have a linter, but it will show up for sure with mypy/pylance/etc... thanks again!

tlambert03 avatar Dec 24 '21 21:12 tlambert03

Thanks for your suggestions! Have created a PR :-)

TheTrio avatar Dec 24 '21 22:12 TheTrio

Why not use the prompt to display the digits? Separate lines with \n.

Prompt.ask('[1] foo\n[2] bar\nPick one')

willmcgugan avatar Dec 31 '21 10:12 willmcgugan

Thanks @willmcgugan It's not so much about "how do I display numbers", it's more about what the user has to do. Wouldn't they have to literally respond by typing "[1] foo"? See original post for motivation here: I think they should be able to type "1" and be done

tlambert03 avatar Dec 31 '21 12:12 tlambert03

I see, you could make the choices 1/2, etc.

Sure, I suppose... but then the user has to do the indexing themselves. If what they actually care about is the "foo" value, that means a number of rich users are duplicating that same logic.

So yeah, it's a "convenience" feature request... not something that has no workaround

tlambert03 avatar Dec 31 '21 12:12 tlambert03

CLosing, but would consider a PR.

willmcgugan avatar Sep 20 '22 10:09 willmcgugan

Did I solve your problem?

Why not buy the devs a coffee to say thanks?

github-actions[bot] avatar Sep 20 '22 10:09 github-actions[bot]