o
    NEgM1                     @   s.  d Z ddlZddlZddlmZ ddlmZmZmZm	Z	 ddl
Z
ddl
mZmZmZ ddlmZ e
jdd d	Z	 d
Z	 dZ	 eZ	 edZG dd deZG dd deZG dd deZejjZ	 ejjZ	 ejZ	 ejZ 	 ej!Z"	 ej#Z$	 e
j%&dZ'	 e
j%&dZ(	 de)de)fddZ*de)de+fddZ,dede)ddfddZ-de	e)ef de)fd d!Z.d"e	e)ef de)fd#d$Z/d%d%dddd&dede	e)ef de	e)ef d'e0d(e0d)e0de)fd*d+Z1ej2e1ej3d,Z4	 ej2e1ej5d,Z6	 ej2e1ej7d,Z8	 d-e+d.e)d/e)d0e+d1e)de)fd2d3Z9d4e)de)fd5d6Z:dS )7z
Support for ANSI escape sequences which are used for things like applying style to text,
setting the window title, and asynchronous alerts.
     N)Enum)IOAnyListUnion)BackForeStyle)wcswidthF)stripNeverTerminalAlwaysz\x1b\[[^m]*mc                   @   sX   e Zd ZdZdefddZdedefddZdedefdd	Ze	de
e fd
dZdS )	ColorBasea
  
    Base class used for defining color enums. See fg and bg classes for examples.

    Child classes should define enums in the follow structure:

        key: color name (e.g. black)

        value: anything that when cast to a string returns an ANSI sequence
    returnc                 C   s
   t | jS )z
        Return ANSI color sequence instead of enum name
        This is helpful when using a ColorBase in an f-string or format() call
        e.g. my_str = "{}hello{}".format(fg.blue, fg.reset)
        )strvalue)self r   c/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Puru/venv/lib/python3.10/site-packages/cmd2/ansi.py__str__K   s   
zColorBase.__str__otherc                 C   s   t | | S )zn
        Support building a color string when self is the left operand
        e.g. fg.blue + "hello"
        r   r   r   r   r   r   __add__S      zColorBase.__add__c                 C   s   |t |  S )zp
        Support building a color string when self is the right operand
        e.g. "hello" + fg.reset
        r   r   r   r   r   __radd__Z   r   zColorBase.__radd__c                 C   s   dd | j D S )zReturn a list of color names.c                 S   s   g | ]}|qS r   r   ).0colorr   r   r   
<listcomp>e   s    z$ColorBase.colors.<locals>.<listcomp>)__members__)clsr   r   r   colorsa   s   zColorBase.colorsN)__name__
__module____qualname____doc__r   r   r   r   r   classmethodr   r"   r   r   r   r   r   A   s    	r   c                   @   v   e Zd ZdZejZejZej	Z
ejZejZejZejZejZejZejZejZejZejZejZ ej!Z"ej#Z$ej%Z&dS )fgz Enum class for foreground colorsN)'r#   r$   r%   r&   r   BLACKblackREDredGREENgreenYELLOWyellowBLUEblueMAGENTAmagentaCYANcyanWHITEwhiteLIGHTBLACK_EXbright_blackLIGHTRED_EX
bright_redLIGHTGREEN_EXbright_greenLIGHTYELLOW_EXbright_yellowLIGHTBLUE_EXbright_blueLIGHTMAGENTA_EXbright_magentaLIGHTCYAN_EXbright_cyanLIGHTWHITE_EXbright_whiteRESETresetr   r   r   r   r)   j   &    
r)   c                   @   r(   )bgz Enum class for background colorsN)'r#   r$   r%   r&   r   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   r   r   r   r   rM      rL   rM         textr   c                 C   s   t d| S )z
    Strip ANSI style sequences from a string.

    :param text: string which may contain ANSI style sequences
    :return: the same string with any ANSI style sequences removed
     )ANSI_STYLE_REsubrP   r   r   r   strip_style   s   rU   c                 C   s   t t| S )z
    Wrap wcswidth to make it compatible with strings that contains ANSI style sequences

    :param text: the string being measured
    :return: the width of the string when printed to the terminal
    )r
   rU   rT   r   r   r   style_aware_wcswidth   s   rV   fileobjmsgc                 C   s>   t  t kst  t kr|  st|}| | dS )z
    Write a string to a fileobject and strip its ANSI style sequences if required by allow_style setting

    :param fileobj: the file object being written to
    :param msg: the string being written
    N)allow_stylelowerSTYLE_NEVERSTYLE_TERMINALisattyrU   write)rW   rX   r   r   r   style_aware_write   s   r_   fg_namec              	   C   H   t | tr| jS z
t|   j}W |S  ty#   td| t w )a  
    Look up ANSI escape codes based on foreground color name.

    :param fg_name: foreground color name or enum to look up ANSI escape code(s) for
    :return: ANSI escape code(s) associated with this color
    :raises: ValueError: if the color cannot be found
    z8Foreground color {!r} does not exist; must be one of: {})
isinstancer)   r   rZ   KeyError
ValueErrorformatr"   )r`   ansi_escaper   r   r   	fg_lookup      
rg   bg_namec              	   C   ra   )a  
    Look up ANSI escape codes based on background color name.

    :param bg_name: background color name or enum to look up ANSI escape code(s) for
    :return: ANSI escape code(s) associated with this color
    :raises: ValueError: if the color cannot be found
    z8Background color {!r} does not exist; must be one of: {})rb   rM   r   rZ   rc   rd   re   r"   )ri   rf   r   r   r   	bg_lookup   rh   rj   rQ   )r)   rM   bolddim	underlinerk   rl   rm   c                C   s   g }g }d | } |r|t| |t |r%|t| |t |r1|t |t |r=|t |t |rI|t	 |t
 d||  d| S )aY  
    Apply ANSI colors and/or styles to a string and return it.
    The styling is self contained which means that at the end of the string reset code(s) are issued
    to undo whatever styling was done at the beginning.

    :param text: Any object compatible with str.format()
    :param fg: foreground color. Relies on `fg_lookup()` to retrieve ANSI escape based on name or enum.
               Defaults to no color.
    :param bg: background color. Relies on `bg_lookup()` to retrieve ANSI escape based on name or enum.
               Defaults to no color.
    :param bold: apply the bold style if True. Can be combined with dim. Defaults to False.
    :param dim: apply the dim style if True. Can be combined with bold. Defaults to False.
    :param underline: apply the underline style if True. Defaults to False.
    :return: the stylized string
    z{}rQ   )re   appendrg   FG_RESETrj   BG_RESETINTENSITY_BRIGHTINTENSITY_NORMALINTENSITY_DIMUNDERLINE_ENABLEUNDERLINE_DISABLEjoin)rP   r)   rM   rk   rl   rm   	additionsremovalsr   r   r   style   s&   








ry   )r)   terminal_columnspromptlinecursor_offset	alert_msgc                 C   s   ddl m} | }d}|dd D ]}t|}|t||  d 7 }q|d }	t|	}
|
t| }t||  d }|
| }t||  d }d}||krT|||| 7 }|| }|t j |d |d  7 }|t j 7 }|d| 7 }|S )a)  Calculate the desired string, including ANSI escape codes, for displaying an asynchronous alert message.

    :param terminal_columns: terminal width (number of columns)
    :param prompt: prompt that is displayed on the current line
    :param line: current contents of the Readline line buffer
    :param cursor_offset: the offset of the current cursor position within line
    :param alert_msg: the message to display to the user
    :return: the correct string so that the alert message appears to the user to be printed above the current line.
    r   )CursorN   rQ   )	coloramar   
splitlinesrV   intDOWNansi
clear_lineUP)rz   r{   r|   r}   r~   r   prompt_linesnum_prompt_terminal_lines
line_widthlast_prompt_linelast_prompt_line_widthinput_widthnum_input_terminal_linescursor_input_offsetcursor_input_lineterminal_strtotal_linesr   r   r   async_alert_str3  s(   
 r   titlec                 C   s   t j| S )zGet the required string, including ANSI escape codes, for setting window title for the terminal.

    :param title: new title for the window
    :return: string to write to sys.stderr in order to set the window title to the desired test
    )r   r   	set_title)r   r   r   r   set_title_stri  s   r   );r&   	functoolsreenumr   typingr   r   r   r   r   r   r   r	   wcwidthr
   initr[   r\   STYLE_ALWAYSrY   compilerR   r   r)   rM   rK   r   ro   rp   	RESET_ALLBRIGHTrq   DIMrs   NORMALrr   r   code_to_charsrt   ru   r   rU   r   rV   r_   rg   rj   boolry   partialr/   style_successrA   style_warningr=   style_errorr   r   r   r   r   r   <module>   sv   
)
&
6"6