U cf(,@sddlmZmZmZmZmZmZmZmZddl m Z ddl m Z ddl mZmZedZedZGdd d eZGd d d eZGd d d eeZGdddeeZGdddeeZGdddeeZGdddeeZedkrddlm Z ej!dddrej!dddZ"e"dkr0e"dkr0q||_||_ ||_ dS)Nr+style) r r' isinstancestrr from_markupr+r(r$r)r*)rr+r'r(r$r)r*rrrr5s zPromptBase.__init__)r'r(r$r)r*stream) r+r'r(r$r)r*defaultr1rc CsdSrr) clsr+r'r(r$r)r*r2r1rrraskKszPromptBase.ask)r+r'r(r$r)r*r1rcCsdSrr)r3r+r'r(r$r)r*r1rrrr4[s .)r'r(r$r)r*r2r1c Cs |||||||d} | ||dS)aShortcut to construct and run a prompt loop and return the result. Example: >>> filename = Prompt.ask("Enter a filename") Args: prompt (TextType, optional): Prompt text. Defaults to "". console (Console, optional): A Console instance or None to use global console. Defaults to None. password (bool, optional): Enable password input. Defaults to False. choices (List[str], optional): A list of valid choices. Defaults to None. show_default (bool, optional): Show default in prompt. Defaults to True. show_choices (bool, optional): Show choices in prompt. Defaults to True. stream (TextIO, optional): Optional text file open for reading to get input. Defaults to None. r&r2r1r) r3r+r'r(r$r)r*r2r1Z_promptrrrr4jsr2rcCstd|ddS)zTurn the supplied default in to a Text instance. Args: default (DefaultType): Default value. Returns: Text: Text containing rendering of default value. ()prompt.default)r )rr2rrrrender_defaults zPromptBase.render_defaultcCs|j}d|_|jrJ|jrJd|j}d|d}|d||d|dkr|jrt|t |j fr|d| |}||||j |S)zMake prompt text. Args: default (DefaultType): Default value. Returns: Text: Text to display in prompt. r%/[] zprompt.choices.) r+copyendr*r$joinappendr)r.r/r#r: prompt_suffix)rr2r+Z_choicesr$_defaultrrr make_prompts$          zPromptBase.make_prompt)r'r+r(r1rcCs|j|||dS)zGet input from user. Args: console (Console): Console instance. prompt (TextType): Prompt text. password (bool): Enable password entry. Returns: str: String from user. )r(r1)input)r3r'r+r(r1rrr get_inputszPromptBase.get_inputvaluercCs|jdk st||jkS)zCheck value is in the list of valid choices. Args: value (str): Value entered by user. Returns: bool: True if choice was valid, otherwise False. N)r$AssertionErrorstriprrIrrr check_choices zPromptBase.check_choicecCsX|}z||}Wntk r4t|jYnX|jdk rT||sTt|j|S)aProcess response from user, convert to prompt type. Args: value (str): String typed by user. Raises: InvalidResponse: If ``value`` is invalid. Returns: PromptType: The value to be returned from ask method. N)rKr# ValueErrorrvalidate_error_messager$rMillegal_choice_message)rrI return_valuerrrprocess_responses  zPromptBase.process_response)rIerrorrcCs|j|dS)zCalled to handle validation error. Args: value (str): String entered by user. error (InvalidResponse): Exception instance the initiated the error. N)r'print)rrIrSrrron_validate_errorszPromptBase.on_validate_errorrcCsdS)z,Hook to display something before the prompt.Nrr rrr pre_promptszPromptBase.pre_promptr1)r1rcCsdSrr)rr1rrr__call__szPromptBase.__call__)r2r1rcCsdSrr)rr2r1rrrrXsr5c Cs|||}|j|j||j|d}|dkr<|dkr<|Sz||}Wn6tk r}z|||WYqW5d}~XYqX|SqdS)zRun the prompt loop. Args: default (Any, optional): Optional default value. Returns: PromptType: Processed value. rWr%.N)rVrErGr'r(rRrrU)rr2r1r+rIrQrSrrrrXs   )r%)r%)r%)r%)N)$rrrrr/r#type__annotations__rOrPrCr$rrrr boolr classmethodr rrrrr4rr r:rErGrMrRrrUrVrXrrrrr"s         $    r"c@seZdZdZeZdS)PromptzbA prompt that returns a str. Example: >>> name = Prompt.ask("Enter your name") N)rrrrr/r#rrrrr]sr]c@seZdZdZeZdZdS) IntPromptzA prompt that returns an integer. Example: >>> burrito_count = IntPrompt.ask("How many burritos do you want to order") z3[prompt.invalid]Please enter a valid integer numberN)rrrrintr#rOrrrrr^*sr^c@seZdZdZeZdZdS) FloatPromptzyA prompt that returns a float. Example: >>> temperature = FloatPrompt.ask("Enter desired temperature") z%[prompt.invalid]Please enter a numberN)rrrrfloatr#rOrrrrr`6sr`c@sNeZdZUdZeZdZddgZee e d<e e dddZ e ed d d Zd S) ConfirmzuA yes / no confirmation prompt. Example: >>> if Confirm.ask("Continue"): run_job() z#[prompt.invalid]Please enter Y or Nynr$r6cCs.|j\}}t|rd|dn d|dddS)z8Render the default as (y) or (n) rather than True/False.r7r8r9r,)r$r )rr2yesnorrrr:Os zConfirm.render_defaultrHcCs.|}||jkr t|j||jdkS)zConvert choices to a bool.r)rKlowerr$rrOrLrrrrRTs   zConfirm.process_responseN)rrrrr[r#rOr$rr/rZrr r:rRrrrrrbBs rb__main__)rTzRun [i]prompt[/i] tests?T)r2z6:rocket: Enter a number between [b]1[/b] and [b]10[/b] z=:pile_of_poo: [prompt.invalid]Number must be between 1 and 10znumber=z=Please enter a password [cyan](must be at least 5 characters))r(z"[prompt.invalid]password too shortz password=z Enter a fruitappleZorangepear)r$zfruit=z[b]OK :loudly_crying_face:N)&typingrrrrrrrr r%r r'r textr rrr Exceptionrrr"r/r]r_r^rar`r[rbrpip._vendor.richrTr4resultr(lenZfruitrrrrsH(