[REQUEST] option to respond to choices with digits
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!!
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:)
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?
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.
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 :)
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!
Thanks for your suggestions! Have created a PR :-)
Why not use the prompt to display the digits? Separate lines with \n.
Prompt.ask('[1] foo\n[2] bar\nPick one')
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
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
CLosing, but would consider a PR.