| 36 | |
| 37 | |
| 38 | def handle_input( |
| 39 | message: str = "", |
| 40 | check_type=False, |
| 41 | match: str = "", |
| 42 | err_message: str = "", |
| 43 | nmin=None, |
| 44 | nmax=None, |
| 45 | oob_error="", |
| 46 | extra_info="", |
| 47 | options: list = None, |
| 48 | default=NotImplemented, |
| 49 | optional=False, |
| 50 | ): |
| 51 | if optional: |
| 52 | console.print(message + "\n[green]This is an optional value. Do you want to skip it? (y/n)") |
| 53 | if input().casefold().startswith("y"): |
| 54 | return default if default is not NotImplemented else "" |
| 55 | if default is not NotImplemented: |
| 56 | console.print( |
| 57 | "[green]" |
| 58 | + message |
| 59 | + '\n[blue bold]The default value is "' |
| 60 | + str(default) |
| 61 | + '"\nDo you want to use it?(y/n)' |
| 62 | ) |
| 63 | if input().casefold().startswith("y"): |
| 64 | return default |
| 65 | if options is None: |
| 66 | match = re.compile(match) |
| 67 | console.print("[green bold]" + extra_info, no_wrap=True) |
| 68 | while True: |
| 69 | console.print(message, end="") |
| 70 | user_input = input("").strip() |
| 71 | if check_type is not False: |
| 72 | try: |
| 73 | user_input = check_type(user_input) |
| 74 | if (nmin is not None and user_input < nmin) or ( |
| 75 | nmax is not None and user_input > nmax |
| 76 | ): |
| 77 | # FAILSTATE Input out of bounds |
| 78 | console.print("[red]" + oob_error) |
| 79 | continue |
| 80 | break # Successful type conversion and number in bounds |
| 81 | except ValueError: |
| 82 | # Type conversion failed |
| 83 | console.print("[red]" + err_message) |
| 84 | continue |
| 85 | elif match != "" and re.match(match, user_input) is None: |
| 86 | console.print("[red]" + err_message + "\nAre you absolutely sure it's correct?(y/n)") |
| 87 | if input().casefold().startswith("y"): |
| 88 | break |
| 89 | continue |
| 90 | else: |
| 91 | # FAILSTATE Input STRING out of bounds |
| 92 | if (nmin is not None and len(user_input) < nmin) or ( |
| 93 | nmax is not None and len(user_input) > nmax |
| 94 | ): |
| 95 | console.print("[red bold]" + oob_error) |