Files
d-crawler/src/elironwars/menu.py
T
2026-07-19 00:50:29 +02:00

56 lines
1.4 KiB
Python

import logging
import os
from enum import Enum
logger = logging.getLogger(__name__)
class Action(Enum):
PIECE_SUIVANTE = (1, "Pièce suivante")
COMBAT = (2, "Combat")
FUIR = (3, "Fuir")
QUITTER = (4, "Quitter")
@property
def code(self):
return self.value[0]
@property
def libelle(self):
return self.value[1]
def clear_console() -> None:
os.system('cls' if os.name == 'nt' else 'clear')
def action_menu(actions_autorisees: list[Action] = None) -> Action:
"""Affiche le menu et retourne une action valide."""
actions_autorisees = list(Action) if actions_autorisees is None else actions_autorisees
logger.debug('Show action menu')
for action in actions_autorisees:
logger.debug('Action %s - "%s"', action.code, action.libelle)
while True:
print("\n------ Menu Action ------\n")
for action in actions_autorisees:
print(f"{action.libelle:<20} : {action.code}")
try:
choix = int(input("\nChoix : "))
clear_console()
for action in actions_autorisees:
if action.code == choix:
logger.info('User selected action %s - "%s"', action.code, action.libelle)
return action
print("Mauvais choix")
except ValueError:
clear_console()
print("Veuillez saisir un nombre.")
logger.info('Bad input')