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

Created a Separator System #72

Closed AnthonyMakesStuff closed 9 months ago

AnthonyMakesStuff commented 10 months ago

This allows the select and select_multiple to have separators in the middle if they are prefixed with :SEPARATOR:. It could also be used to put a description for another option.

names = [
    "Frodo Baggins",
    "Samwise Gamgee",
    "Legolas",
    "Aragorn",
    ":SEPARATOR:" + ("=" * 32),
    "[red]Sauron[/red]",
]
# Choose one item from a list
name = select(names, cursor="🢧", cursor_style="cyan")

This example with add a separator above the last option.

AnthonyMakesStuff commented 10 months ago

I have been using this in my own project, and I wanted to provide it in case anyone else would find it useful

AnthonyMakesStuff commented 10 months ago

I'm sure it's obvious, but in case anyone can't tell, I'm very new to using git

petereon commented 10 months ago

Please don't worry, I am very glad you chose to contribute to my project! Glancing over the code, I have gathered a few remarks, but I haven't found time to write them down just yet as I am somewhat pressed for time. I believe, I should be able to get to it tomorrow.

petereon commented 9 months ago

@AnthonyMakesStuff, sorry for delay, life got in the way of review.

Anyway, I've got to take a proper look today. I like this new feature, I can definitely imagine a use-case for having a way to provide sub-groups of options. This feature was already available in cutie from which I have inherited most of the code (this started as a fork and then diverged too far). I've removed it deliberately, as the implementation struck me as very unnatural.

That said, I'd envision different sort of implementation. This one relies heavily on specific formatting of strings, which provides some room for user error and is more difficult in terms of automated preparation of the data for displaying. Python has a rich set of data-structures available out of the box and this seems like an good application for the dictionary (Dict[str, List[str]]).

As an example with 'Historical' and 'Fictional' being the sections:

people = {
  'Historical': [
    'Napoleon',
    'Julius Caesar',
    'Marie Curie',
  ],
  'Fictional': [
    'Mark Watney',
    'Harry Potter',
  ]
}

select_multiple(options=people)

This would be a bit more of a hassle to implement but it would be cleaner in terms of usage and data preparation for displaying.

If you would like to have a go at it, please do let me know. Otherwise I will implement it myself in near future. Either way, thank you very much for the contribution and inspiration.