-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(main): refactor main not to violate DRY
- Loading branch information
Showing
2 changed files
with
53 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Module with utilities supporting creation of CLI menus.""" | ||
|
||
from typing import Any | ||
from knowledge_verificator.io_handler import console | ||
from knowledge_verificator.utils.string import clip_text | ||
|
||
|
||
def choose_from_menu( | ||
menu_elements: list[str], plural_name: str, max_line_width: int = 40 | ||
) -> Any | None: | ||
""" | ||
Prompt a user to choose an element from a list via terminal. | ||
Args: | ||
menu_elements (list[str]): List of elements to choose from. | ||
Elements should be convertible to `str` (implement `__str__` method). | ||
plural_name (str): Plural name of the elements. For example: options, paragraphs or names. | ||
max_line_width (int): Maximum line width in number of columns. By default: 40. | ||
Returns: | ||
any | None: Element of a list or None if a user provided incorrect | ||
value via a terminal. | ||
""" | ||
console.print(f'Available {plural_name}:') | ||
for i, element in enumerate(menu_elements): | ||
console.print(f'[{i+1}] {clip_text(element, max_line_width)}') | ||
material_choice = input('Your choice: ') | ||
console.print() | ||
|
||
incorrect_choice_warning = ( | ||
'This is incorrect choice. Next time, provide a number ' | ||
'next to a element from the list of available ones.' | ||
) | ||
if not material_choice.isnumeric(): | ||
console.print(incorrect_choice_warning) | ||
return None | ||
|
||
chosen_index = int(material_choice) - 1 | ||
if chosen_index < 0 or chosen_index >= len(menu_elements): | ||
console.print(incorrect_choice_warning) | ||
return None | ||
|
||
return menu_elements[chosen_index] |