feat: implemented better components
This commit is contained in:
45
music_kraken/cli/genre.py
Normal file
45
music_kraken/cli/genre.py
Normal file
@@ -0,0 +1,45 @@
|
||||
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 _genre_generator() -> Generator[str, None, None]:
|
||||
def is_valid_genre(genre: Path) -> bool:
|
||||
"""
|
||||
gets the name of all subdirectories of shared.MUSIC_DIR,
|
||||
but filters out all directories, where the name matches with any Patern
|
||||
from shared.NOT_A_GENRE_REGEX.
|
||||
"""
|
||||
if not genre.is_dir():
|
||||
return False
|
||||
|
||||
if any(re.match(regex_pattern, genre.name) for regex_pattern in main_settings["not_a_genre_regex"]):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
for genre in filter(is_valid_genre, main_settings["music_directory"].iterdir()):
|
||||
yield genre.name
|
||||
|
||||
def get_genre() -> str:
|
||||
select_genre = components.Select(
|
||||
human_io=GenreIO,
|
||||
can_create_options=True,
|
||||
data=_genre_generator(),
|
||||
)
|
||||
genre: Optional[components.Option[str]] = None
|
||||
|
||||
while genre is None:
|
||||
print(select_genre.pprint())
|
||||
print()
|
||||
|
||||
genre = select_genre.choose(input("> "))
|
||||
|
||||
return genre.value
|
||||
|
||||
@@ -18,6 +18,7 @@ from ..utils.shared import HELP_MESSAGE, URL_PATTERN
|
||||
from ..utils.string_processing import fit_to_file_system
|
||||
from ..utils.support_classes.download_result import DownloadResult
|
||||
from ..utils.support_classes.query import Query
|
||||
from .genre import get_genre
|
||||
from .options.first_config import initial_config
|
||||
from .utils import ask_for_bool, cli_function
|
||||
|
||||
@@ -27,30 +28,7 @@ PAGE_NAME_FILL = "-"
|
||||
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():
|
||||
select_genre = components.GenreSelect()
|
||||
select_genre.human_io = GenreIO
|
||||
|
||||
genre: Optional[components.Option] = None
|
||||
|
||||
while genre is None:
|
||||
print(select_genre.pprint())
|
||||
print()
|
||||
|
||||
genre = select_genre.choose(input("> "))
|
||||
|
||||
return genre.value
|
||||
|
||||
|
||||
def help_message():
|
||||
|
||||
Reference in New Issue
Block a user