# Path: valohai_cli/help_texts.py
# EXECUTION_COUNTER_HELP = 'Execution # (or "latest")'


from typing import Any, Callable, List, Optional, Sequence, Tuple, TypeVar, Union
from valohai_cli.help_texts import EXECUTION_COUNTER_HELP
import click



FuncT = TypeVar('FuncT', bound=Callable[..., Any])


def _default_name_formatter(option: Any) -> str:
    if isinstance(option, dict) and 'name' in option:
        return str(option['name'])
    return str(option)


def prompt_from_list(
    options: Sequence[dict],
    prompt: str,
    nonlist_validator: Optional[Callable[[str], Optional[Any]]] = None,
    name_formatter: Callable[[dict], str] = _default_name_formatter,
) -> Union[Any, dict]:
    for i, option in enumerate(options, 1):
        click.echo('{number} {name} {description}'.format(
            number=click.style(f'[{i:3d}]', fg='cyan'),
            name=name_formatter(option),
            description=(
                click.style(f'({option["description"]})', dim=True)
                if option.get('description')
                else ''

# Path: valohai_cli/help_texts.py
# EXECUTION_COUNTER_HELP = 'Execution # (or "latest")'


# Path: valohai_cli/utils/cli_utils.py
from typing import Any, Callable, List, Optional, Sequence, Tuple, TypeVar, Union
from valohai_cli.help_texts import EXECUTION_COUNTER_HELP
import click




FuncT = TypeVar('FuncT', bound=Callable[..., Any])


def _default_name_formatter(option: Any) -> str:
    if isinstance(option, dict) and 'name' in option:
        return str(option['name'])
    return str(option)


def prompt_from_list(
    options: Sequence[dict],
    prompt: str,
    nonlist_validator: Optional[Callable[[str], Optional[Any]]] = None,
    name_formatter: Callable[[dict], str] = _default_name_formatter,
) -> Union[Any, dict]:
    for i, option in enumerate(options, 1):
        click.echo('{number} {name} {description}'.format(
            number=click.style(f'[{i:3d}]', fg='cyan'),
            name=name_formatter(option),
            description=(
                click.style(f'({option["description"]})', dim=True)
                if option.get('description')
                else ''

            ),