Skip to content

Commit

Permalink
Drop options_map_func
Browse files Browse the repository at this point in the history
  • Loading branch information
wong2 committed Aug 31, 2022
1 parent 2b42293 commit 5e03169
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 101 deletions.
35 changes: 0 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,41 +56,6 @@ interactive selection list in the terminal.
multiple items by hitting SPACE
- `min_selection_count`: (optional) for multi select feature to
dictate a minimum of selected items before continuing
- `options_map_func`: (optional) a mapping function to pass each
option through before displaying

## Options Map Function

If your options are not in a format that you want displayed (such as a
dictionary), you can pass in a mapping function which each option will
be run through. The return value of the function will be displayed.

> the selected option returned will be the original value and not the
> displayed return result from the `options_map_func` function.
**pick** options map function example:

>>> from pick import pick

>>> title = 'Please choose an option: '
>>> options = [{'label': 'option1'}, {'label': 'option2'}, {'label': 'option3'}]

>>> def get_label(option): return option.get('label')

>>> selected = pick(options, title, indicator='*', options_map_func=get_label)
>>> print(selected)

**displays**:

Please choose an option:

* option1
option2
option3

**outputs**:

>>> ({ 'label': 'option1' }, 0)

## Community Projects

Expand Down
20 changes: 0 additions & 20 deletions example/options_map_func.py

This file was deleted.

28 changes: 6 additions & 22 deletions src/pick/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import curses
from dataclasses import dataclass, field
from typing import (
Callable,
Generic,
List,
Optional,
Sequence,
Tuple,
TypeVar,
Union,
)
from typing import List, Optional, Sequence, Tuple, Union

__all__ = ["Picker", "pick"]

Expand All @@ -23,19 +14,17 @@
SYMBOL_CIRCLE_EMPTY = "◯"

KEY_T = int
OPTIONS_MAP_VALUE_T = TypeVar("OPTIONS_MAP_VALUE_T")
PICK_RETURN_T = Tuple[OPTIONS_MAP_VALUE_T, int]
PICK_RETURN_T = Tuple[str, int]


@dataclass
class Picker(Generic[OPTIONS_MAP_VALUE_T]):
options: Sequence[OPTIONS_MAP_VALUE_T]
class Picker:
options: Sequence[str]
title: Optional[str] = None
indicator: str = "*"
default_index: int = 0
multiselect: bool = False
min_selection_count: int = 0
options_map_func: Callable[[OPTIONS_MAP_VALUE_T], Optional[str]] = str
selected_indexes: List[int] = field(init=False, default_factory=list)
index: int = field(init=False, default=0)
scroll_top: int = field(init=False, default=0)
Expand All @@ -52,9 +41,6 @@ def __post_init__(self) -> None:
"min_selection_count is bigger than the available options, you will not be able to make any selection"
)

if not callable(self.options_map_func):
raise ValueError("options_map_func must be a callable function")

self.index = self.default_index

def move_up(self) -> None:
Expand Down Expand Up @@ -107,7 +93,7 @@ def get_option_lines(self) -> List[str]:
)
prefix = f"{prefix} {symbol} "

option_as_str = self.options_map_func(option)
option_as_str = str(option)
lines.append(f"{prefix} {option_as_str}")

return lines
Expand Down Expand Up @@ -180,13 +166,12 @@ def start(self):


def pick(
options: Sequence[OPTIONS_MAP_VALUE_T],
options: Sequence[str],
title: Optional[str] = None,
indicator: str = "*",
default_index: int = 0,
multiselect: bool = False,
min_selection_count: int = 0,
options_map_func: Callable[[OPTIONS_MAP_VALUE_T], Optional[str]] = str,
):
picker: Picker = Picker(
options,
Expand All @@ -195,6 +180,5 @@ def pick(
default_index,
multiselect,
min_selection_count,
options_map_func,
)
return picker.start()
29 changes: 5 additions & 24 deletions tests/test_pick.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from typing import Dict, Optional

from pick import Picker


def test_move_up_down():
title = "Please choose an option: "
options = ["option1", "option2", "option3"]
picker: Picker[str] = Picker(options, title)
picker = Picker(options, title)
picker.move_up()
assert picker.get_selected() == ("option3", 2)
picker.move_down()
Expand All @@ -17,50 +15,33 @@ def test_move_up_down():
def test_default_index():
title = "Please choose an option: "
options = ["option1", "option2", "option3"]
picker: Picker[str] = Picker(options, title, default_index=1)
picker = Picker(options, title, default_index=1)
assert picker.get_selected() == ("option2", 1)


def test_get_lines():
title = "Please choose an option: "
options = ["option1", "option2", "option3"]
picker: Picker[str] = Picker(options, title, indicator="*")
picker = Picker(options, title, indicator="*")
lines, current_line = picker.get_lines()
assert lines == [title, "", "* option1", " option2", " option3"]
assert current_line == 3


def test_no_title():
options = ["option1", "option2", "option3"]
picker: Picker[str] = Picker(options)
picker = Picker(options)
lines, current_line = picker.get_lines()
assert current_line == 1


def test_multi_select():
title = "Please choose an option: "
options = ["option1", "option2", "option3"]
picker: Picker[str] = Picker(
options, title, multiselect=True, min_selection_count=1
)
picker = Picker(options, title, multiselect=True, min_selection_count=1)
assert picker.get_selected() == []
picker.mark_index()
assert picker.get_selected() == [("option1", 0)]
picker.move_down()
picker.mark_index()
assert picker.get_selected() == [("option1", 0), ("option2", 1)]


def test_options_map_func():
title = "Please choose an option: "
options = [{"label": "option1"}, {"label": "option2"}, {"label": "option3"}]

def get_label(option: Dict[str, str]) -> Optional[str]:
return option.get("label")

picker: Picker[Dict[str, str]] = Picker(
options, title, indicator="*", options_map_func=get_label
)
lines, current_line = picker.get_lines()
assert lines == [title, "", "* option1", " option2", " option3"]
assert picker.get_selected() == ({"label": "option1"}, 0)

0 comments on commit 5e03169

Please sign in to comment.