U
    祡c                  !   @   s"  U d dl Z d dlZd dlmZmZmZmZmZmZ e j	dkrJd dlm
Z
 nd dlm
Z
 ddlmZmZmZ erddlmZmZmZ dd	d
ddgZe
ed< dd eD Ze
ed< ddddddZe
ed< ejdd ejdd ejdd ejdd ejdd ejdd ejdd ejd d ej d!d ej!d"d ej"d#d ej#d$d ej$d%d ej%d&d ej&d'd ej'd(d iZ(ee)ed)e*f f ed*< G d+d, d,Z+efe*ee)df e*d-d.d/Z,efe*ee)e*f e*d-d0d1Z-e.d2krd d3l/mZ e Ze0d4 e1d5D ]"Z2e3d6d7e2   e4d8 qdS )9    N)TYPE_CHECKINGCallableDictIterableListUnion)      )Final   )ControlCodeControlTypeSegment)ConsoleConsoleOptionsRenderResult   r	            STRIP_CONTROL_CODESc                 C   s   i | ]
}|d qS N ).0Z
_codepointr   r   0/tmp/pip-unpacked-wheel-qkhzhrww/rich/control.py
<dictcomp>   s     r   _CONTROL_STRIP_TRANSLATEz\az\bz\vz\fz\r)r   r	   r   r   r   CONTROL_ESCAPEc                   C   s   dS )Nr   r   r   r   r   <lambda>#       r   c                   C   s   dS )Nr   r   r   r   r   r   $   r    c                   C   s   dS )Nz[Hr   r   r   r   r   r   %   r    c                   C   s   dS )Nz[2Jr   r   r   r   r   r   &   r    c                   C   s   dS )Nz[?1049hr   r   r   r   r   r   '   r    c                   C   s   dS )Nz[?1049lr   r   r   r   r   r   (   r    c                   C   s   dS )Nz[?25hr   r   r   r   r   r   )   r    c                   C   s   dS )Nz[?25lr   r   r   r   r   r   *   r    c                 C   s   d|  dS )N[Ar   paramr   r   r   r   +   r    c                 C   s   d|  dS )Nr"   Br   r$   r   r   r   r   ,   r    c                 C   s   d|  dS )Nr"   Cr   r$   r   r   r   r   -   r    c                 C   s   d|  dS )Nr"   Dr   r$   r   r   r   r   .   r    c                 C   s   d| d  dS )Nr"   r   Gr   r$   r   r   r   r   /   r    c                 C   s   d|  dS )Nr"   Kr   r$   r   r   r   r   0   r    c                 C   s   d|d  d| d  dS )Nr"   r   ;Hr   xyr   r   r   r   1   r    c                 C   s   d|  dS )Nz]0;r   r   )titler   r   r   r   2   r    .CONTROL_CODES_FORMATc                   @   s  e Zd ZdZdgZeeef ddddZe	d ddd	Z
e	d dd
dZe	d'eed dddZe	d(eed dddZe	eed dddZe	d dddZe	ed dddZe	ed dddZe	ed dddZeddd Zd!d"d#d$d%d&ZdS ))ControlzA renderable that inserts a control code (non printable but may move cursor).

    Args:
        *codes (str): Positional arguments are either a :class:`~rich.segment.ControlType` enum or a
            tuple of ControlType and an integer parameter
    segmentN)codesreturnc                    s<   dd |D }t  d fdd|D }t|d || _d S )Nc                 S   s    g | ]}t |tr|fn|qS r   )
isinstancer   )r   coder   r   r   
<listcomp>A   s    z$Control.__init__.<locals>.<listcomp> c                 3   s   | ]^}} | | V  qd S r   r   )r   r7   
parametersZ_format_mapr   r   	<genexpr>E   s    z#Control.__init__.<locals>.<genexpr>)r1   joinr   r3   )selfr4   Zcontrol_codesZrendered_codesr   r;   r   __init__@   s    zControl.__init__r5   c                 C   s
   | t jS )zRing the 'bell'.)r   BELLclsr   r   r   bellJ   s    zControl.bellc                 C   s
   | t jS )zMove cursor to 'home' position.)r   HOMErB   r   r   r   homeO   s    zControl.homer   )r.   r/   r5   c                    s&   t t d fdd}| |  }|S )zMove cursor relative to current position.

        Args:
            x (int): X offset.
            y (int): Y offset.

        Returns:
            ~Control: Control object.

        r@   c                  3   sL   t }  r& dkr| jn| jt fV  rHdkr8| jn| jtfV  d S )Nr   )r   CURSOR_FORWARDCURSOR_BACKWARDabsCURSOR_DOWN	CURSOR_UP)controlr-   r   r   	get_codesa   s    zControl.move.<locals>.get_codes)r   r   )rC   r.   r/   rM   rL   r   r-   r   moveT   s    
zControl.movec                 C   s:   |r,| t j|f|dkrt jnt jt|fS | t j|fS )zMove to the given column, optionally add offset to row.

        Returns:
            x (int): absolute x (column)
            y (int): optional y offset (row)

        Returns:
            ~Control: Control object.
        r   )r   CURSOR_MOVE_TO_COLUMNrJ   rK   rI   rC   r.   r/   r   r   r   move_to_columnq   s    zControl.move_to_columnc                 C   s   | t j||fS )zMove cursor to absolute position.

        Args:
            x (int): x offset (column)
            y (int): y offset (row)

        Returns:
            ~Control: Control object.
        )r   CURSOR_MOVE_TOrP   r   r   r   move_to   s    zControl.move_toc                 C   s
   | t jS )zClear the screen.)r   CLEARrB   r   r   r   clear   s    zControl.clear)showr5   c                 C   s   | |rt jnt jS )zShow or hide the cursor.)r   SHOW_CURSORHIDE_CURSOR)rC   rV   r   r   r   show_cursor   s    zControl.show_cursor)enabler5   c                 C   s    |r| t jt jS | t jS dS )zEnable or disable alt screen.N)r   ENABLE_ALT_SCREENrE   DISABLE_ALT_SCREEN)rC   rZ   r   r   r   
alt_screen   s    zControl.alt_screen)r0   r5   c                 C   s   | t j|fS )zlSet the terminal window title

        Args:
            title (str): The new terminal window title
        )r   SET_WINDOW_TITLE)rC   r0   r   r   r   r0      s    zControl.titlec                 C   s   | j jS r   r3   text)r>   r   r   r   __str__   s    zControl.__str__r   r   r   )consoleoptionsr5   c                 c   s   | j jr| j V  d S r   r_   )r>   rb   rc   r   r   r   __rich_console__   s    zControl.__rich_console__)r   r   )r   )__name__
__module____qualname____doc__	__slots__r   r   r   r?   classmethodrD   rF   intrN   rQ   rS   rU   boolrY   r]   strr0   ra   rd   r   r   r   r   r2   6   s4   
 r2   )r`   _translate_tabler5   c                 C   s
   |  |S )zRemove control codes from text.

    Args:
        text (str): A string possibly contain control codes.

    Returns:
        str: String with control codes removed.
    	translater`   rn   r   r   r   strip_control_codes   s    rr   c                 C   s
   |  |S )a  Replace control codes with their "escaped" equivalent in the given text.
    (e.g. "" becomes "\b")

    Args:
        text (str): A string possibly containing control codes.

    Returns:
        str: String with control codes replaced with their escaped version.
    ro   rq   r   r   r   escape_control_codes   s    rs   __main__)r   z+Look at the title of your terminal window ^
   u   🚀 Loading.g      ?)5systimetypingr   r   r   r   r   r   version_infor
   Ztyping_extensionsr3   r   r   r   rb   r   r   r   r   __annotations__r   r   rA   ZCARRIAGE_RETURNrE   rT   r[   r\   rW   rX   rK   rJ   rG   rH   rO   ZERASE_IN_LINErR   r^   r1   rk   rm   r2   rr   rs   re   Zrich.consoleprintrangeiZset_window_titlesleepr   r   r   r   <module>   s     
	                  



