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

Add possibility for color per entry #54

Closed juxeii closed 1 year ago

juxeii commented 1 year ago

Would it be possible to have an option for colors per entry in lets say multi-select? It would be perfect to show the user critical/important choices(e.g. in red). So, instead of just providing a list of options, would it be possible to provide a list of tuples, with first entry the text, second entry the color of the text? If no color is given, just use the default one.

Thx for this great module!

petereon commented 1 year ago

Hi @juxeii , you can color and style options using rich syntax, it is partly demonstrated in the example. I have included the relevant excerpt extended with a bit more styling below.

names = [
      "Frodo [bold]Baggins[/bold]",
      "[#ff0000]Samwise Gamgee[/#ff0000]",
      "Legolas",
      "Aragorn",
      "[blue]Sauron[/blue]",
  ]
  name = select(names, cursor=">", cursor_style="cyan")

I am aware it requires a bit of manual work to massage the strings, but I would not really like to have tuples as I want to have beaupy dead-simple.

juxeii commented 1 year ago

Thanks, did not know that! Works nicely.

juxeii commented 1 year ago

I have an issue with the colors in select_multiple. I color some options, for example:

image The issue is that I cannot see the current active selection, which I specified with cursor_style='green'

Now, when I do not color the options, like so:

image The cursor style is now back and the users see the current selection.

Am I doing something wrong here? No matter which color I choose for the options, as soon as they are colored, I can no longer see the selection.

petereon commented 1 year ago

Thanks for reporting, this is a good catch. It's a trivial issue but I can't think of an elegant fix. Let me mull this over for a bit, I'll probably have it resolved during the weekend.

petereon commented 1 year ago

There is now a pre-release 3.5.3b0 that resolves this, also available on PyPI. Feel free to test around with your use-case and if everything works, I'll do a full release.

juxeii commented 1 year ago

Hi, thx. Seems to work.

petereon commented 1 year ago

Released this as full release (3.5.3) today.