feat: reworked the genre select
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Hazel 2024-05-27 14:13:18 +02:00
parent 850c68f3e5
commit 71ec309953
2 changed files with 40 additions and 9 deletions

View File

@ -27,17 +27,28 @@ PAGE_NAME_FILL = "-"
MAX_PAGE_LEN = 21 MAX_PAGE_LEN = 21
class GenreIO(components.HumanIO):
@staticmethod
def ask_to_create(option: components.Option) -> bool:
output()
return ask_for_bool(f"create the genre {BColors.OKBLUE.value}{option.value}{BColors.ENDC.value}")
@staticmethod
def not_found(key: str) -> None:
output(f"\ngenre {BColors.BOLD.value}{key}{BColors.ENDC.value} not found\n", color=BColors.FAIL)
def get_genre(): def get_genre():
select_genre = components.GenreSelect() select_genre = components.GenreSelect()
select_genre._ask_for_creating_option = lambda key: ask_for_bool(f"Create the genre \"{key}\"") select_genre.human_io = GenreIO
genre: Optional[components.Option] = None genre: Optional[components.Option] = None
while genre is None: while genre is None:
for genre in select_genre: print(select_genre.pprint())
print(genre) print()
genre = select_genre.choose(input("Id or new genre: ")) genre = select_genre.choose(input("> "))
return genre.value return genre.value

View File

@ -1,12 +1,25 @@
from __future__ import annotations
import re import re
from pathlib import Path from pathlib import Path
from typing import Any, Callable, Dict, Generator, List, Optional from typing import Any, Callable, Dict, Generator, List, Optional
from ..utils import BColors
from ..utils.config import main_settings from ..utils.config import main_settings
from ..utils.exception import MKComposeException from ..utils.exception import MKComposeException
from ..utils.string_processing import unify from ..utils.string_processing import unify
class HumanIO:
@staticmethod
def ask_to_create(option: Option) -> bool:
return True
@staticmethod
def not_found(key: Any) -> None:
return None
class Option: class Option:
""" """
This could represent a data object, a string or a page. This could represent a data object, a string or a page.
@ -56,12 +69,12 @@ class Select:
option_factory: Callable[[Any], Option] = None, option_factory: Callable[[Any], Option] = None,
raw_options: List[Any] = None, raw_options: List[Any] = None,
parse_option_key: Callable[[Any], Any] = lambda x: x, parse_option_key: Callable[[Any], Any] = lambda x: x,
ask_for_creating_option: Callable[[Option], bool] = lambda x: True, human_io: HumanIO = HumanIO,
sort: bool = False, sort: bool = False,
**kwargs **kwargs
): ):
self._parse_option_key: Callable[[Any], Any] = parse_option_key self._parse_option_key: Callable[[Any], Any] = parse_option_key
self._ask_for_creating_option: Callable[[Option], bool] = ask_for_creating_option self.human_io: HumanIO = human_io
self._key_to_option: Dict[Any, Option] = dict() self._key_to_option: Dict[Any, Option] = dict()
self._options: List[Option] = [] self._options: List[Option] = []
@ -117,12 +130,19 @@ class Select:
def choose(self, key: Any) -> Optional[Option]: def choose(self, key: Any) -> Optional[Option]:
if key not in self: if key not in self:
if self.can_create_options and self._ask_for_creating_option(key): if self.can_create_options:
return self.create_option(key) c = self.create_option(key)
if self.human_io.ask_to_create(c):
return c
self.human_io.not_found(key)
return None return None
return self[key] return self[key]
def pprint(self) -> str:
return "\n".join(str(option) for option in self)
class StringSelect(Select): class StringSelect(Select):
@ -134,7 +154,7 @@ class StringSelect(Select):
super().__init__(**kwargs) super().__init__(**kwargs)
def next_option(self, value: Any) -> Optional[Option]: def next_option(self, value: Any) -> Optional[Option]:
o = Option(value=value, keys=[self._current_index], text=f"{self._current_index:0>2}: {value}") o = Option(value=value, keys=[self._current_index], text=f"{BColors.BOLD.value}{self._current_index: >2}{BColors.ENDC.value}: {value}")
self._current_index += 1 self._current_index += 1
return o return o