Package: inputtr
Inputter - Just getting inputA small library to get user input in a quick and easy way.Feel free to submit pull requests.UsageUse Inputter in your codeClone or download the Inputter.py file or install via pip.pip install inputtrfrominputtrimportinputter# Inputter.get_input(prompt, f_constraint: callable = not_empty, f_additional_params=None, max_tries: int = -1) -> Optional:in_str=inputter.get_input("Prompt: ",constraint_function,[additional,parameters],max_tries=5)# prompt: The prompt which is shown when input is required.# f_constraint: Constraint function to check the input against,#               this function is also allowed to transform the output.# f_additional_params: List of parameters to supply to the constraint function.# max_tries: Negative for no limit, otherwise cancel after x and show error.Flags# Available flags are:frominputtrimportinputterinputter.format_prompt=(True/False)# Format the input prompt with badge and color (Default: True)inputter.silent=(True/False)# Suppresses or enables all output except prompting (Default: False)inputter.disable_colors=(True/False)# Enables / Disables colored output but keeps badges  (Default: False)inputter.disable_badges=(True/False)# Enables / Disables badges in output  (Default: False)inputter.throw_on_constraint_func_error=(True/False)# Changes constraint function error behaviour  (Default: False)Creating new constraint functionsTo create a custom constraint function, your function should follow some simple rulesAccept str as first parameter, this will be the user inputTo generate warnings without crashes your function should supply parameter typesThe function should return an Optional type and return None if checking was not successfulYour function will be checked before trying to execute it, any minor errors will be output as warnings.Errors which would crash execution will be output as errors and theget_input()returnsNone, this behaviour
can be changed with the flag mentioned above in which case a RuntimeError is also thrown.The only exception to this, is the case where you do not supply enough parameters, and the constraint function
does not have sufficient default parameters in which case a TypeError is always thrown.Example:frominputtrimportinputterdefis_integer_in_range(input_str:str,min_val:int,max_val:int)->Optional[int]:try:int_val=int(input_str)ifint_val<min_valorint_val>max_val:print_warning(f"Value should be in range{min_val}-{max_val}")returnNonereturnint_valexceptValueError:print_warning("Input is not an integer")returnNoneuser_input=Inputter.get_input("Input: ",is_integer_in_range,[0,100])As shown in the example to keep the look of printed text the same,
you should use the print_error and print_warning function of Inputter.Changing colors and formattingInputter has a selection of colors and modifiers to choose from,
the ones available can be accessed via the TermColors class.Since we are dealing with console output the colors are determined by ANSI escape sequences,
to get more information about this read:https://en.wikipedia.org/wiki/ANSI_escape_codefrominputtrimportinputterinputter.info_color=inputter.TermColors.OKBLUEinputter.error_color=inputter.TermColors.REDinputter.prompt_color=inputter.TermColors.OKGREENinputter.warning_color=inputter.TermColors.YELLOWAll of these colors can of course also be combined with formatting changes.frominputtrimportinputterInputter.prompt_color=inputter.TermColors.OKGREEN+inputter.TermColors.BOLD