aisk / pick

create curses based interactive selection list in the terminal
MIT License
721 stars 60 forks source link

Default selections of multi-select #106

Open r3a1d3a1 opened 1 year ago

r3a1d3a1 commented 1 year ago

I can't tell from the README whether it's possible to have a few options selected as the dialog is presented to the user at first. For example, there may be 10 options, 3 of which are selected initially.

manurFR commented 5 months ago

Some hack for this (works with pick 2.2.0):

import pick

# Example 1: Pre-select 3 options (first, third, sixth)
preselected = [0, 2, 5]

# Example 2: Pre-select all options
# preselected = list(range(len(options)))

__original_post_init__ = pick.Picker.__post_init__

def __hacked_post_init__(self):
    __original_post_init__(self)
    self.selected_indexes.extend(preselected)

pick.Picker.__post_init__ = __hacked_post_init__

selected = pick.pick(options=options, multiselect=True)

It would be trivial to add the feature to the main code without an ugly hack. @wong2 Do you review and accept pull requests ? I see there are some of them that have never been evaluated...